diff options
author | kib <kib@FreeBSD.org> | 2008-12-06 09:37:54 +0000 |
---|---|---|
committer | kib <kib@FreeBSD.org> | 2008-12-06 09:37:54 +0000 |
commit | 723ad6dc01479a43dfb77d3c94e81e41c181a99f (patch) | |
tree | c1149b45a13e1314f8a9f8b52878247f9d99a859 /lib/libc/string | |
parent | 55095b18c6876d38831d4517ad0485562d76f7bb (diff) | |
download | FreeBSD-src-723ad6dc01479a43dfb77d3c94e81e41c181a99f.zip FreeBSD-src-723ad6dc01479a43dfb77d3c94e81e41c181a99f.tar.gz |
Import the strndup(3) function.
Copyright attribution is kept the same as in original NetBSD source.
Submitted by: Florian Smeets <flo kasimir com>
Obtained from: NetBSD
MFC after: 2 weeks
Diffstat (limited to 'lib/libc/string')
-rw-r--r-- | lib/libc/string/Makefile.inc | 3 | ||||
-rw-r--r-- | lib/libc/string/Symbol.map | 1 | ||||
-rw-r--r-- | lib/libc/string/strdup.3 | 21 | ||||
-rw-r--r-- | lib/libc/string/strndup.c | 53 |
4 files changed, 75 insertions, 3 deletions
diff --git a/lib/libc/string/Makefile.inc b/lib/libc/string/Makefile.inc index ab14b42..7fddae4 100644 --- a/lib/libc/string/Makefile.inc +++ b/lib/libc/string/Makefile.inc @@ -11,7 +11,7 @@ MISRCS+=bcmp.c bcopy.c bzero.c ffs.c ffsl.c ffsll.c fls.c flsl.c flsll.c \ memcpy.c memmem.c memmove.c memset.c rindex.c stpcpy.c strcasecmp.c \ strcat.c strcasestr.c strchr.c strcmp.c strcoll.c strcpy.c strcspn.c \ strdup.c strerror.c strlcat.c strlcpy.c strlen.c strmode.c strncat.c \ - strncmp.c strncpy.c strnstr.c \ + strncmp.c strncpy.c strndup.c strnstr.c \ strpbrk.c strrchr.c strsep.c strsignal.c strspn.c strstr.c strtok.c \ strxfrm.c swab.c wcscat.c wcschr.c wcscmp.c wcscoll.c wcscpy.c \ wcscspn.c wcsdup.c \ @@ -47,6 +47,7 @@ MLINKS+=strcat.3 strncat.3 MLINKS+=strchr.3 strrchr.3 MLINKS+=strcmp.3 strncmp.3 MLINKS+=strcpy.3 stpcpy.3 +MLINKS+=strdup.3 strndup.3 MLINKS+=strcpy.3 strncpy.3 MLINKS+=strerror.3 perror.3 strerror.3 sys_errlist.3 strerror.3 sys_nerr.3 MLINKS+=strerror.3 strerror_r.3 diff --git a/lib/libc/string/Symbol.map b/lib/libc/string/Symbol.map index e307c7f..c69f0b5 100644 --- a/lib/libc/string/Symbol.map +++ b/lib/libc/string/Symbol.map @@ -81,6 +81,7 @@ FBSD_1.1 { ffsll; flsll; memrchr; + strndup; }; FBSDprivate_1.0 { diff --git a/lib/libc/string/strdup.3 b/lib/libc/string/strdup.3 index 818be57..c6b2ad0 100644 --- a/lib/libc/string/strdup.3 +++ b/lib/libc/string/strdup.3 @@ -28,11 +28,12 @@ .\" @(#)strdup.3 8.1 (Berkeley) 6/9/93 .\" $FreeBSD$ .\" -.Dd June 9, 1993 +.Dd Dec 5, 2008 .Dt STRDUP 3 .Os .Sh NAME -.Nm strdup +.Nm strdup , +.Nm strndup .Nd save a copy of a string .Sh LIBRARY .Lb libc @@ -40,6 +41,8 @@ .In string.h .Ft char * .Fn strdup "const char *str" +.Ft char * +.Fn strndup "const char *str" "size_t len" .Sh DESCRIPTION The .Fn strdup @@ -56,6 +59,16 @@ If insufficient memory is available, NULL is returned and .Va errno is set to .Er ENOMEM . +.Pp +The +.Fn strndup +function copies at most +.Fa len +characters from the string +.Fa str +always +.Dv NUL +terminating the copied string. .Sh SEE ALSO .Xr free 3 , .Xr malloc 3 @@ -64,3 +77,7 @@ The .Fn strdup function first appeared in .Bx 4.4 . +The +.Fn strndup +function was added in +.Fx 8.0 . diff --git a/lib/libc/string/strndup.c b/lib/libc/string/strndup.c new file mode 100644 index 0000000..56aa6a8 --- /dev/null +++ b/lib/libc/string/strndup.c @@ -0,0 +1,53 @@ +/* $NetBSD: strndup.c,v 1.3 2007/01/14 23:41:24 cbiere Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. 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. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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> +__FBSDID("$FreeBSD$"); + +#include <stddef.h> +#include <stdlib.h> +#include <string.h> + +char * +strndup(const char *str, size_t n) +{ + size_t len; + char *copy; + + for (len = 0; len < n && str[len]; len++) + continue; + + if ((copy = malloc(len + 1)) == NULL) + return (NULL); + memcpy(copy, str, len); + copy[len] = '\0'; + return (copy); +} |