diff options
| author | itojun <itojun@NetBSD.org> | 2003-05-15 13:50:35 +0000 |
|---|---|---|
| committer | itojun <itojun@NetBSD.org> | 2003-05-15 13:50:35 +0000 |
| commit | 99c5869d566e2ffec9ea02432d1753c992d41d45 (patch) | |
| tree | a9f8127d0ccbd7cf41eab26d6fb5f731675e31d7 /sys | |
| parent | a5d8a0a4f615800ad28e398f0001adaa4fd6b396 (diff) | |
add strl{cpy,cat} to libkern. code from lib/libc/string (originally from openbsd).
Diffstat (limited to 'sys')
| -rw-r--r-- | sys/lib/libkern/Makefile | 4 | ||||
| -rw-r--r-- | sys/lib/libkern/libkern.h | 4 | ||||
| -rw-r--r-- | sys/lib/libkern/strlcat.c | 75 | ||||
| -rw-r--r-- | sys/lib/libkern/strlcpy.c | 71 |
4 files changed, 151 insertions, 3 deletions
diff --git a/sys/lib/libkern/Makefile b/sys/lib/libkern/Makefile index b110012479d..870a4a3954f 100644 --- a/sys/lib/libkern/Makefile +++ b/sys/lib/libkern/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.69 2002/11/23 23:35:50 fvdl Exp $ +# $NetBSD: Makefile,v 1.70 2003/05/15 13:50:35 itojun Exp $ LIB= kern NOPIC= # defined @@ -43,7 +43,7 @@ SRCS+= adddi3.c anddi3.c ashldi3.c ashrdi3.c cmpdi2.c divdi3.c iordi3.c \ SRCS+= __cmsg_alignbytes.c inet_addr.c intoa.c md4c.c md5c.c sha1.c pmatch.c SRCS+= _que.c arc4random.c -SRCS+= strstr.c +SRCS+= strstr.c strlcpy.c strlcat.c # Files to clean up CLEANFILES+= lib${LIB}.o lib${LIB}.po diff --git a/sys/lib/libkern/libkern.h b/sys/lib/libkern/libkern.h index 5ef950e2d9d..937d8779635 100644 --- a/sys/lib/libkern/libkern.h +++ b/sys/lib/libkern/libkern.h @@ -1,4 +1,4 @@ -/* $NetBSD: libkern.h,v 1.47 2002/10/24 20:53:50 christos Exp $ */ +/* $NetBSD: libkern.h,v 1.48 2003/05/15 13:50:35 itojun Exp $ */ /*- * Copyright (c) 1992, 1993 @@ -285,6 +285,8 @@ u_long random __P((void)); int scanc __P((u_int, const u_char *, const u_char *, int)); int skpc __P((int, size_t, u_char *)); int strcasecmp __P((const char *, const char *)); +size_t strlcpy __P((char *, const char *, size_t)); +size_t strlcat __P((char *, const char *, size_t)); int strncasecmp __P((const char *, const char *, size_t)); u_long strtoul __P((const char *, char **, int)); #endif /* !_LIB_LIBKERN_LIBKERN_H_ */ diff --git a/sys/lib/libkern/strlcat.c b/sys/lib/libkern/strlcat.c new file mode 100644 index 00000000000..ca6fa1cf23e --- /dev/null +++ b/sys/lib/libkern/strlcat.c @@ -0,0 +1,75 @@ +/* $NetBSD: strlcat.c,v 1.1 2003/05/15 13:50:35 itojun Exp $ */ +/* $OpenBSD: strlcat.c,v 1.4 2001/01/12 22:55:23 millert Exp $ */ + +/* + * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: strlcat.c,v 1.1 2003/05/15 13:50:35 itojun Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <lib/libkern/libkern.h> + +/* + * Appends src to string dst of size siz (unlike strncat, siz is the + * full size of dst, not space left). At most siz-1 characters + * will be copied. Always NUL terminates (unless siz <= strlen(dst)). + * Returns strlen(src) + MIN(siz, strlen(initial dst)). + * If retval >= siz, truncation occurred. + */ +size_t +strlcat(dst, src, siz) + char *dst; + const char *src; + size_t siz; +{ + char *d = dst; + const char *s = src; + size_t n = siz; + size_t dlen; + + /* Find the end of dst and adjust bytes left but don't go past end */ + while (n-- != 0 && *d != '\0') + d++; + dlen = d - dst; + n = siz - dlen; + + if (n == 0) + return(dlen + strlen(s)); + while (*s != '\0') { + if (n != 1) { + *d++ = *s; + n--; + } + s++; + } + *d = '\0'; + + return(dlen + (s - src)); /* count does not include NUL */ +} diff --git a/sys/lib/libkern/strlcpy.c b/sys/lib/libkern/strlcpy.c new file mode 100644 index 00000000000..63595569138 --- /dev/null +++ b/sys/lib/libkern/strlcpy.c @@ -0,0 +1,71 @@ +/* $NetBSD: strlcpy.c,v 1.1 2003/05/15 13:50:35 itojun Exp $ */ +/* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ */ + +/* + * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: strlcpy.c,v 1.1 2003/05/15 13:50:35 itojun Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <lib/libkern/libkern.h> + +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +size_t +strlcpy(dst, src, siz) + char *dst; + const char *src; + size_t siz; +{ + char *d = dst; + const char *s = src; + size_t n = siz; + + /* Copy as many bytes as will fit */ + if (n != 0 && --n != 0) { + do { + if ((*d++ = *s++) == 0) + break; + } while (--n != 0); + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + + return(s - src - 1); /* count does not include NUL */ +} |
