1 /* Safe string copying and concatenation
2 * These routines are originally distributed in two separate files. I have
3 * copied the files verbatim in this single source file, including all comments.
4 * The only change is that the second set of include files is commented out
5 * (there is no need to include the same files twice).
10 #if !defined HAVE_SAFESTR
12 /* $OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */
17 * Permission to use, copy, modify, and distribute this software for any
18 * purpose with or without fee is hereby granted, provided that the above
19 * copyright notice and this permission notice appear in all copies.
21 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
22 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
24 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
25 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
26 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
27 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 #include <sys/types.h> already included through lstring.h
33 #include <string.h> /* for strlen() */
36 * Copy src to string dst of size siz. At most siz-1 characters
37 * will be copied. Always NUL terminates (unless siz == 0).
38 * Returns strlen(src); if retval >= siz, truncation occurred.
41 strlcpy(char *dst, const char *src, size_t siz)
47 /* Copy as many bytes as will fit */
48 if (n != 0 && --n != 0) {
50 if ((*d++ = *s++) == 0)
55 /* Not enough room in dst, add NUL and traverse rest of src */
58 *d = '\0'; /* NUL-terminate dst */
63 return(s - src - 1); /* count does not include NUL */
66 /* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
71 * Permission to use, copy, modify, and distribute this software for any
72 * purpose with or without fee is hereby granted, provided that the above
73 * copyright notice and this permission notice appear in all copies.
75 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
76 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
77 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
78 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
79 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
80 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
81 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
85 #include <sys/types.h> already included
86 #include <string.h> already included
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 */
124 #endif /* #if !defined HAVE_SAFESTR */