diff -Nur libc.org/string/Makefile.inc libc/string/Makefile.inc --- libc.org/string/Makefile.inc 2008-12-04 13:40:22.000000000 +0100 +++ libc/string/Makefile.inc 2008-12-04 13:47:32.000000000 +0100 @@ -9,9 +9,9 @@ MISRCS+=bcmp.c bcopy.c bzero.c ffs.c ffsl.c ffsll.c fls.c flsl.c flsll.c \ index.c memccpy.c memchr.c memrchr.c memcmp.c \ memcpy.c memmem.c memmove.c memset.c rindex.c stpcpy.c strcasecmp.c \ - strcat.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 strcasestr.c strnstr.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 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 \ @@ -48,6 +48,7 @@ MLINKS+=strcmp.3 strncmp.3 MLINKS+=strcpy.3 stpcpy.3 MLINKS+=strcpy.3 strncpy.3 +MLINKS+=strdup.3 strndup.3 MLINKS+=strerror.3 perror.3 strerror.3 sys_errlist.3 strerror.3 sys_nerr.3 MLINKS+=strerror.3 strerror_r.3 MLINKS+=strlcpy.3 strlcat.3 diff -Nur libc.org/string/Symbol.map libc/string/Symbol.map --- libc.org/string/Symbol.map 2008-12-04 13:40:22.000000000 +0100 +++ libc/string/Symbol.map 2008-12-04 13:41:31.000000000 +0100 @@ -81,6 +81,7 @@ ffsll; flsll; memrchr; + strndup; }; FBSDprivate_1.0 { diff -Nur libc.org/string/strdup.3 libc/string/strdup.3 --- libc.org/string/strdup.3 2008-12-04 13:40:22.000000000 +0100 +++ libc/string/strdup.3 2008-12-04 15:01:24.000000000 +0100 @@ -28,11 +28,12 @@ .\" @(#)strdup.3 8.1 (Berkeley) 6/9/93 .\" $FreeBSD: src/lib/libc/string/strdup.3,v 1.11 2007/01/09 00:28:12 imp Exp $ .\" -.Dd June 9, 1993 +.Dd Dec 4, 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 @@ .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 @@ .Fn strdup function first appeared in .Bx 4.4 . +The +.Fn strndup +function was added in +.Fx 8.0 . diff -Nur libc.org/string/strndup.c libc/string/strndup.c --- libc.org/string/strndup.c 1970-01-01 01:00:00.000000000 +0100 +++ libc/string/strndup.c 2008-12-04 14:58:01.000000000 +0100 @@ -0,0 +1,54 @@ +/* + * 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)strdup.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include + +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); +}