2 * Why use strlcpy()/strlcat() instead of standard strncpy()/strncat()?
3 * To reduce likelihood of bugs and avoid wasteful zero fills. See:
4 * http://www.gratisoft.us/todd/papers/strlcpy.html
7 /* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
10 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
12 * Permission to use, copy, modify, and distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #include <config.h> /* + marks local changes */
26 #ifdef HAVE_SYS_TYPES_H /* + */
27 #include <sys/types.h>
31 #include "ntp_stdlib.h" /* + strlcpy, strlcat prototypes */
33 #ifndef HAVE_STRLCPY /* + */
35 * Copy src to string dst of size siz. At most siz-1 characters
36 * will be copied. Always NUL terminates (unless siz == 0).
37 * Returns strlen(src); if retval >= siz, truncation occurred.
40 strlcpy(char *dst, const char *src, size_t siz)
46 /* Copy as many bytes as will fit */
49 if ((*d++ = *s++) == '\0')
54 /* Not enough room in dst, add NUL and traverse rest of src */
57 *d = '\0'; /* NUL-terminate dst */
62 return(s - src - 1); /* count does not include NUL */
67 /* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
70 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
72 * Permission to use, copy, modify, and distribute this software for any
73 * purpose with or without fee is hereby granted, provided that the above
74 * copyright notice and this permission notice appear in all copies.
76 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
77 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
78 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
79 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
80 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
81 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
82 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
85 /* #include <sys/types.h> */ /* + */
86 /* #include <string.h> */ /* + */
88 #ifndef HAVE_STRLCAT /* + */
90 * Appends src to string dst of size siz (unlike strncat, siz is the
91 * full size of dst, not space left). At most siz-1 characters
92 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
93 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
94 * If retval >= siz, truncation occurred.
97 strlcat(char *dst, const char *src, size_t siz)
104 /* Find the end of dst and adjust bytes left but don't go past end */
105 while (n-- != 0 && *d != '\0')
111 return(dlen + strlen(s));
121 return(dlen + (s - src)); /* count does not include NUL */