xref: /freebsd/contrib/ntp/libntp/strl_obsd.c (revision 2b15cb3d0922bd70ea592f0da9b4a5b167f4d53f)
1*2b15cb3dSCy Schubert /*
2*2b15cb3dSCy Schubert  * Why use strlcpy()/strlcat() instead of standard strncpy()/strncat()?
3*2b15cb3dSCy Schubert  * To reduce likelihood of bugs and avoid wasteful zero fills.  See:
4*2b15cb3dSCy Schubert  * http://www.gratisoft.us/todd/papers/strlcpy.html
5*2b15cb3dSCy Schubert  */
6*2b15cb3dSCy Schubert 
7*2b15cb3dSCy Schubert /*	$OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $	*/
8*2b15cb3dSCy Schubert 
9*2b15cb3dSCy Schubert /*
10*2b15cb3dSCy Schubert  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
11*2b15cb3dSCy Schubert  *
12*2b15cb3dSCy Schubert  * Permission to use, copy, modify, and distribute this software for any
13*2b15cb3dSCy Schubert  * purpose with or without fee is hereby granted, provided that the above
14*2b15cb3dSCy Schubert  * copyright notice and this permission notice appear in all copies.
15*2b15cb3dSCy Schubert  *
16*2b15cb3dSCy Schubert  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17*2b15cb3dSCy Schubert  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18*2b15cb3dSCy Schubert  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19*2b15cb3dSCy Schubert  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20*2b15cb3dSCy Schubert  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21*2b15cb3dSCy Schubert  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22*2b15cb3dSCy Schubert  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23*2b15cb3dSCy Schubert  */
24*2b15cb3dSCy Schubert 
25*2b15cb3dSCy Schubert #include <config.h>		/* + marks local changes */
26*2b15cb3dSCy Schubert #ifdef HAVE_SYS_TYPES_H		/* + */
27*2b15cb3dSCy Schubert #include <sys/types.h>
28*2b15cb3dSCy Schubert #endif				/* + */
29*2b15cb3dSCy Schubert #include <string.h>
30*2b15cb3dSCy Schubert 
31*2b15cb3dSCy Schubert #include "ntp_stdlib.h"		/* + strlcpy, strlcat prototypes */
32*2b15cb3dSCy Schubert 
33*2b15cb3dSCy Schubert #ifndef HAVE_STRLCPY		/* + */
34*2b15cb3dSCy Schubert /*
35*2b15cb3dSCy Schubert  * Copy src to string dst of size siz.  At most siz-1 characters
36*2b15cb3dSCy Schubert  * will be copied.  Always NUL terminates (unless siz == 0).
37*2b15cb3dSCy Schubert  * Returns strlen(src); if retval >= siz, truncation occurred.
38*2b15cb3dSCy Schubert  */
39*2b15cb3dSCy Schubert size_t
40*2b15cb3dSCy Schubert strlcpy(char *dst, const char *src, size_t siz)
41*2b15cb3dSCy Schubert {
42*2b15cb3dSCy Schubert 	char *d = dst;
43*2b15cb3dSCy Schubert 	const char *s = src;
44*2b15cb3dSCy Schubert 	size_t n = siz;
45*2b15cb3dSCy Schubert 
46*2b15cb3dSCy Schubert 	/* Copy as many bytes as will fit */
47*2b15cb3dSCy Schubert 	if (n != 0) {
48*2b15cb3dSCy Schubert 		while (--n != 0) {
49*2b15cb3dSCy Schubert 			if ((*d++ = *s++) == '\0')
50*2b15cb3dSCy Schubert 				break;
51*2b15cb3dSCy Schubert 		}
52*2b15cb3dSCy Schubert 	}
53*2b15cb3dSCy Schubert 
54*2b15cb3dSCy Schubert 	/* Not enough room in dst, add NUL and traverse rest of src */
55*2b15cb3dSCy Schubert 	if (n == 0) {
56*2b15cb3dSCy Schubert 		if (siz != 0)
57*2b15cb3dSCy Schubert 			*d = '\0';		/* NUL-terminate dst */
58*2b15cb3dSCy Schubert 		while (*s++)
59*2b15cb3dSCy Schubert 			;
60*2b15cb3dSCy Schubert 	}
61*2b15cb3dSCy Schubert 
62*2b15cb3dSCy Schubert 	return(s - src - 1);	/* count does not include NUL */
63*2b15cb3dSCy Schubert }
64*2b15cb3dSCy Schubert #endif				/* + */
65*2b15cb3dSCy Schubert 
66*2b15cb3dSCy Schubert 
67*2b15cb3dSCy Schubert /*	$OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $	*/
68*2b15cb3dSCy Schubert 
69*2b15cb3dSCy Schubert /*
70*2b15cb3dSCy Schubert  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
71*2b15cb3dSCy Schubert  *
72*2b15cb3dSCy Schubert  * Permission to use, copy, modify, and distribute this software for any
73*2b15cb3dSCy Schubert  * purpose with or without fee is hereby granted, provided that the above
74*2b15cb3dSCy Schubert  * copyright notice and this permission notice appear in all copies.
75*2b15cb3dSCy Schubert  *
76*2b15cb3dSCy Schubert  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
77*2b15cb3dSCy Schubert  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
78*2b15cb3dSCy Schubert  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
79*2b15cb3dSCy Schubert  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
80*2b15cb3dSCy Schubert  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
81*2b15cb3dSCy Schubert  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
82*2b15cb3dSCy Schubert  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
83*2b15cb3dSCy Schubert  */
84*2b15cb3dSCy Schubert 
85*2b15cb3dSCy Schubert /* #include <sys/types.h> */	/* + */
86*2b15cb3dSCy Schubert /* #include <string.h> */	/* + */
87*2b15cb3dSCy Schubert 
88*2b15cb3dSCy Schubert #ifndef HAVE_STRLCAT		/* + */
89*2b15cb3dSCy Schubert /*
90*2b15cb3dSCy Schubert  * Appends src to string dst of size siz (unlike strncat, siz is the
91*2b15cb3dSCy Schubert  * full size of dst, not space left).  At most siz-1 characters
92*2b15cb3dSCy Schubert  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
93*2b15cb3dSCy Schubert  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
94*2b15cb3dSCy Schubert  * If retval >= siz, truncation occurred.
95*2b15cb3dSCy Schubert  */
96*2b15cb3dSCy Schubert size_t
97*2b15cb3dSCy Schubert strlcat(char *dst, const char *src, size_t siz)
98*2b15cb3dSCy Schubert {
99*2b15cb3dSCy Schubert 	char *d = dst;
100*2b15cb3dSCy Schubert 	const char *s = src;
101*2b15cb3dSCy Schubert 	size_t n = siz;
102*2b15cb3dSCy Schubert 	size_t dlen;
103*2b15cb3dSCy Schubert 
104*2b15cb3dSCy Schubert 	/* Find the end of dst and adjust bytes left but don't go past end */
105*2b15cb3dSCy Schubert 	while (n-- != 0 && *d != '\0')
106*2b15cb3dSCy Schubert 		d++;
107*2b15cb3dSCy Schubert 	dlen = d - dst;
108*2b15cb3dSCy Schubert 	n = siz - dlen;
109*2b15cb3dSCy Schubert 
110*2b15cb3dSCy Schubert 	if (n == 0)
111*2b15cb3dSCy Schubert 		return(dlen + strlen(s));
112*2b15cb3dSCy Schubert 	while (*s != '\0') {
113*2b15cb3dSCy Schubert 		if (n != 1) {
114*2b15cb3dSCy Schubert 			*d++ = *s;
115*2b15cb3dSCy Schubert 			n--;
116*2b15cb3dSCy Schubert 		}
117*2b15cb3dSCy Schubert 		s++;
118*2b15cb3dSCy Schubert 	}
119*2b15cb3dSCy Schubert 	*d = '\0';
120*2b15cb3dSCy Schubert 
121*2b15cb3dSCy Schubert 	return(dlen + (s - src));	/* count does not include NUL */
122*2b15cb3dSCy Schubert }
123*2b15cb3dSCy Schubert #endif				/* + */
124