xref: /freebsd/contrib/unbound/compat/strlcpy.c (revision 40f65a4df509c6b29b9e72407c7afefe28838844)
1*b7579f77SDag-Erling Smørgrav /* from openssh 4.3p2 compat/strlcpy.c */
2*b7579f77SDag-Erling Smørgrav /*
3*b7579f77SDag-Erling Smørgrav  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
4*b7579f77SDag-Erling Smørgrav  *
5*b7579f77SDag-Erling Smørgrav  * Permission to use, copy, modify, and distribute this software for any
6*b7579f77SDag-Erling Smørgrav  * purpose with or without fee is hereby granted, provided that the above
7*b7579f77SDag-Erling Smørgrav  * copyright notice and this permission notice appear in all copies.
8*b7579f77SDag-Erling Smørgrav  *
9*b7579f77SDag-Erling Smørgrav  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*b7579f77SDag-Erling Smørgrav  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*b7579f77SDag-Erling Smørgrav  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*b7579f77SDag-Erling Smørgrav  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*b7579f77SDag-Erling Smørgrav  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*b7579f77SDag-Erling Smørgrav  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*b7579f77SDag-Erling Smørgrav  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*b7579f77SDag-Erling Smørgrav  */
17*b7579f77SDag-Erling Smørgrav 
18*b7579f77SDag-Erling Smørgrav /* OPENBSD ORIGINAL: lib/libc/string/strlcpy.c */
19*b7579f77SDag-Erling Smørgrav 
20*b7579f77SDag-Erling Smørgrav #include <config.h>
21*b7579f77SDag-Erling Smørgrav #ifndef HAVE_STRLCPY
22*b7579f77SDag-Erling Smørgrav 
23*b7579f77SDag-Erling Smørgrav #include <sys/types.h>
24*b7579f77SDag-Erling Smørgrav #include <string.h>
25*b7579f77SDag-Erling Smørgrav 
26*b7579f77SDag-Erling Smørgrav /*
27*b7579f77SDag-Erling Smørgrav  * Copy src to string dst of size siz.  At most siz-1 characters
28*b7579f77SDag-Erling Smørgrav  * will be copied.  Always NUL terminates (unless siz == 0).
29*b7579f77SDag-Erling Smørgrav  * Returns strlen(src); if retval >= siz, truncation occurred.
30*b7579f77SDag-Erling Smørgrav  */
31*b7579f77SDag-Erling Smørgrav size_t
strlcpy(char * dst,const char * src,size_t siz)32*b7579f77SDag-Erling Smørgrav strlcpy(char *dst, const char *src, size_t siz)
33*b7579f77SDag-Erling Smørgrav {
34*b7579f77SDag-Erling Smørgrav 	char *d = dst;
35*b7579f77SDag-Erling Smørgrav 	const char *s = src;
36*b7579f77SDag-Erling Smørgrav 	size_t n = siz;
37*b7579f77SDag-Erling Smørgrav 
38*b7579f77SDag-Erling Smørgrav 	/* Copy as many bytes as will fit */
39*b7579f77SDag-Erling Smørgrav 	if (n != 0 && --n != 0) {
40*b7579f77SDag-Erling Smørgrav 		do {
41*b7579f77SDag-Erling Smørgrav 			if ((*d++ = *s++) == 0)
42*b7579f77SDag-Erling Smørgrav 				break;
43*b7579f77SDag-Erling Smørgrav 		} while (--n != 0);
44*b7579f77SDag-Erling Smørgrav 	}
45*b7579f77SDag-Erling Smørgrav 
46*b7579f77SDag-Erling Smørgrav 	/* Not enough room in dst, add NUL and traverse rest of src */
47*b7579f77SDag-Erling Smørgrav 	if (n == 0) {
48*b7579f77SDag-Erling Smørgrav 		if (siz != 0)
49*b7579f77SDag-Erling Smørgrav 			*d = '\0';		/* NUL-terminate dst */
50*b7579f77SDag-Erling Smørgrav 		while (*s++)
51*b7579f77SDag-Erling Smørgrav 			;
52*b7579f77SDag-Erling Smørgrav 	}
53*b7579f77SDag-Erling Smørgrav 
54*b7579f77SDag-Erling Smørgrav 	return(s - src - 1);	/* count does not include NUL */
55*b7579f77SDag-Erling Smørgrav }
56*b7579f77SDag-Erling Smørgrav 
57*b7579f77SDag-Erling Smørgrav #endif /* !HAVE_STRLCPY */
58