2 * copyright (c) 2011 Emil Mikulic.
4 * bsd.c: *BSD compatibility.
6 * Permission to use, copy, modify, and distribute this file for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #include <string.h> /* for strlen */
25 /* strlcpy() and strlcat() are derived from:
27 * $OpenBSD: strlcpy.c,v 1.4
28 * $FreeBSD: src/lib/libc/string/strlcpy.c,v 1.8
30 * $OpenBSD: strlcat.c,v 1.2
31 * $FreeBSD: src/lib/libc/string/strlcat.c,v 1.10
33 * under the following license:
35 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. The name of the author may not be used to endorse or promote products
47 * derived from this software without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
50 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
51 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
52 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
55 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
56 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
57 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
58 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 * Copy src to string dst of size siz. At most siz-1 characters
64 * will be copied. Always NUL terminates (unless siz == 0).
65 * Returns strlen(src); if retval >= siz, truncation occurred.
68 strlcpy(char * restrict dst
, const char * restrict src
, const size_t siz
)
74 /* Copy as many bytes as will fit */
75 if (n
!= 0 && --n
!= 0) {
77 if ((*d
++ = *s
++) == 0)
82 /* Not enough room in dst, add NUL and traverse rest of src */
85 *d
= '\0'; /* NUL-terminate dst */
90 return (size_t)(s
- src
- 1); /* count does not include NUL */
96 * Appends src to string dst of size siz (unlike strncat, siz is the
97 * full size of dst, not space left). At most siz-1 characters
98 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
99 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
100 * If retval >= siz, truncation occurred.
103 strlcat(char * restrict dst
, const char * restrict src
, const size_t siz
)
110 /* Find the end of dst and adjust bytes left but don't go past end */
111 while (n
-- != 0 && *d
!= '\0')
113 dlen
= (size_t)(d
- dst
);
117 return(dlen
+ strlen(s
));
127 return (dlen
+ (size_t)(s
- src
)); /* count does not include NUL */
131 /* vim:set ts=3 sw=3 tw=78 expandtab: */