1240075Sdes/*	$OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $	*/
2126274Sdes
398937Sdes/*
498937Sdes * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
598937Sdes *
6124208Sdes * Permission to use, copy, modify, and distribute this software for any
7124208Sdes * purpose with or without fee is hereby granted, provided that the above
8124208Sdes * copyright notice and this permission notice appear in all copies.
998937Sdes *
10124208Sdes * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11124208Sdes * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12124208Sdes * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13124208Sdes * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14124208Sdes * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15124208Sdes * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16124208Sdes * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1798937Sdes */
1898937Sdes
19157016Sdes/* OPENBSD ORIGINAL: lib/libc/string/strlcpy.c */
20157016Sdes
21106121Sdes#include "includes.h"
2298937Sdes#ifndef HAVE_STRLCPY
2398937Sdes
2498937Sdes#include <sys/types.h>
2598937Sdes#include <string.h>
2698937Sdes
2798937Sdes/*
2898937Sdes * Copy src to string dst of size siz.  At most siz-1 characters
2998937Sdes * will be copied.  Always NUL terminates (unless siz == 0).
3098937Sdes * Returns strlen(src); if retval >= siz, truncation occurred.
3198937Sdes */
3298937Sdessize_t
33124208Sdesstrlcpy(char *dst, const char *src, size_t siz)
3498937Sdes{
35157016Sdes	char *d = dst;
36157016Sdes	const char *s = src;
37157016Sdes	size_t n = siz;
3898937Sdes
3998937Sdes	/* Copy as many bytes as will fit */
40240075Sdes	if (n != 0) {
41240075Sdes		while (--n != 0) {
42240075Sdes			if ((*d++ = *s++) == '\0')
4398937Sdes				break;
44240075Sdes		}
4598937Sdes	}
4698937Sdes
4798937Sdes	/* Not enough room in dst, add NUL and traverse rest of src */
4898937Sdes	if (n == 0) {
4998937Sdes		if (siz != 0)
5098937Sdes			*d = '\0';		/* NUL-terminate dst */
5198937Sdes		while (*s++)
5298937Sdes			;
5398937Sdes	}
5498937Sdes
5598937Sdes	return(s - src - 1);	/* count does not include NUL */
5698937Sdes}
5798937Sdes
5898937Sdes#endif /* !HAVE_STRLCPY */
59