Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/lib/libkern add strl{cpy, cat} to libkern. code from lib...



details:   https://anonhg.NetBSD.org/src/rev/a4825e09d843
branches:  trunk
changeset: 547228:a4825e09d843
user:      itojun <itojun%NetBSD.org@localhost>
date:      Thu May 15 13:50:35 2003 +0000

description:
add strl{cpy,cat} to libkern.  code from lib/libc/string (originally from openbsd).

diffstat:

 sys/lib/libkern/Makefile  |   4 +-
 sys/lib/libkern/libkern.h |   4 +-
 sys/lib/libkern/strlcat.c |  75 +++++++++++++++++++++++++++++++++++++++++++++++
 sys/lib/libkern/strlcpy.c |  71 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 151 insertions(+), 3 deletions(-)

diffs (190 lines):

diff -r b3815e4664b7 -r a4825e09d843 sys/lib/libkern/Makefile
--- a/sys/lib/libkern/Makefile  Thu May 15 13:46:15 2003 +0000
+++ b/sys/lib/libkern/Makefile  Thu May 15 13:50:35 2003 +0000
@@ -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+= __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 -r b3815e4664b7 -r a4825e09d843 sys/lib/libkern/libkern.h
--- a/sys/lib/libkern/libkern.h Thu May 15 13:46:15 2003 +0000
+++ b/sys/lib/libkern/libkern.h Thu May 15 13:50:35 2003 +0000
@@ -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 @@
 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 -r b3815e4664b7 -r a4825e09d843 sys/lib/libkern/strlcat.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/lib/libkern/strlcat.c Thu May 15 13:50:35 2003 +0000
@@ -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@localhost>
+ * 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 -r b3815e4664b7 -r a4825e09d843 sys/lib/libkern/strlcpy.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/lib/libkern/strlcpy.c Thu May 15 13:50:35 2003 +0000
@@ -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@localhost>
+ * 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 */
+}



Home | Main Index | Thread Index | Old Index