diff options
| author | christos <christos@NetBSD.org> | 2006-08-12 23:49:53 +0000 |
|---|---|---|
| committer | christos <christos@NetBSD.org> | 2006-08-12 23:49:53 +0000 |
| commit | cbfb283c6503db5eeb83d9482cdc38463b6f8477 (patch) | |
| tree | 56a41e5ee895b61386403ee79b9e26b7a5932531 /lib/libc/string | |
| parent | 448cb4aa28f4f44961d567175b49589307ef6ea5 (diff) | |
- Add strndup and stresep
- Use stresep so in fstab so that we can mount paths with white space in them.
Diffstat (limited to 'lib/libc/string')
| -rw-r--r-- | lib/libc/string/Makefile.inc | 7 | ||||
| -rw-r--r-- | lib/libc/string/strdup.3 | 19 | ||||
| -rw-r--r-- | lib/libc/string/stresep.c | 94 | ||||
| -rw-r--r-- | lib/libc/string/strndup.c | 68 | ||||
| -rw-r--r-- | lib/libc/string/strsep.3 | 11 |
5 files changed, 192 insertions, 7 deletions
diff --git a/lib/libc/string/Makefile.inc b/lib/libc/string/Makefile.inc index eabab7a2ba4..201386088bf 100644 --- a/lib/libc/string/Makefile.inc +++ b/lib/libc/string/Makefile.inc @@ -1,12 +1,13 @@ # from: @(#)Makefile.inc 8.1 (Berkeley) 6/4/93 -# $NetBSD: Makefile.inc,v 1.60 2006/06/03 04:36:45 tnozaki Exp $ +# $NetBSD: Makefile.inc,v 1.61 2006/08/12 23:49:54 christos Exp $ # string sources .PATH: ${ARCHDIR}/string ${.CURDIR}/string SRCS+= bm.c strcasecmp.c strncasecmp.c strcasestr.c strcoll.c strdup.c \ strerror.c strlcat.c strlcpy.c strmode.c strsignal.c strtok.c \ - strtok_r.c strxfrm.c __strsignal.c strerror_r.c + strtok_r.c strxfrm.c __strsignal.c strerror_r.c strndup.c \ + stresep.c # wide char SRCS+= wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ @@ -69,6 +70,8 @@ MLINKS+=strstr.3 strcasestr.3 MLINKS+=strtok.3 strtok_r.3 MLINKS+=strerror.3 strerror_r.3 strerror.3 perror.3 \ strerror.3 sys_errlist.3 strerror.3 sys_nerr.3 +MLINKS+=strdup.3 strndup.3 +MLINKS+=strsep.3 stresep.3 MLINKS+=wmemchr.3 wmemcmp.3 wmemchr.3 wmemcpy.3 \ wmemchr.3 wmemmove.3 wmemchr.3 wmemset.3 \ wmemchr.3 wcscat.3 wmemchr.3 wcschr.3 \ diff --git a/lib/libc/string/strdup.3 b/lib/libc/string/strdup.3 index 6f6c61b2db6..e8145194da9 100644 --- a/lib/libc/string/strdup.3 +++ b/lib/libc/string/strdup.3 @@ -26,13 +26,14 @@ .\" SUCH DAMAGE. .\" .\" from: @(#)strdup.3 8.1 (Berkeley) 6/9/93 -.\" $NetBSD: strdup.3,v 1.12 2003/08/07 16:43:50 agc Exp $ +.\" $NetBSD: strdup.3,v 1.13 2006/08/12 23:49:54 christos Exp $ .\" -.Dd August 11, 2002 +.Dd August 12, 2006 .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 @@ -53,6 +56,16 @@ argument to the function .Xr free 3 . .Pp If insufficient memory is available, NULL is returned. +.Pp +The +.Fn strndup +function copies at most +.Fa len +characters from the string +.Fa str +always +.Dv NUL +terminating the copied string. .Sh EXAMPLES The following will point .Va p diff --git a/lib/libc/string/stresep.c b/lib/libc/string/stresep.c new file mode 100644 index 00000000000..6593e0fb402 --- /dev/null +++ b/lib/libc/string/stresep.c @@ -0,0 +1,94 @@ +/* $NetBSD: stresep.c,v 1.1 2006/08/12 23:49:54 christos Exp $ */ + +/*- + * Copyright (c) 1990, 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. + * 3. 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> +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)strsep.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: stresep.c,v 1.1 2006/08/12 23:49:54 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include <assert.h> +#include <string.h> + +#ifdef __weak_alias +__weak_alias(stresep,_stresep) +#endif + +/* + * Get next token from string *stringp, where tokens are possibly-empty + * strings separated by characters from delim. If esc is not NUL, then + * the characters followed by esc are ignored and are not taken into account + * when splitting the string. + * + * Writes NULs into the string at *stringp to end tokens. + * delim need not remain constant from call to call. + * On return, *stringp points past the last NUL written (if there might + * be further tokens), or is NULL (if there are definitely no more tokens). + * + * If *stringp is NULL, stresep returns NULL. + */ +char * +stresep(char **stringp, const char *delim, int esc) +{ + char *s; + const char *spanp; + int c, sc; + char *tok; + + _DIAGASSERT(stringp != NULL); + _DIAGASSERT(delim != NULL); + + if ((s = *stringp) == NULL) + return NULL; + for (tok = s;;) { + c = *s++; + if (esc != '\0' && c == esc) { + (void)strcpy(s - 1, s); + c = *s++; + } + spanp = delim; + do { + if ((sc = *spanp++) == c) { + if (c == 0) + s = NULL; + else + s[-1] = 0; + *stringp = s; + return tok; + } + } while (sc != 0); + } +} diff --git a/lib/libc/string/strndup.c b/lib/libc/string/strndup.c new file mode 100644 index 00000000000..3d23db478b0 --- /dev/null +++ b/lib/libc/string/strndup.c @@ -0,0 +1,68 @@ +/* $NetBSD: strndup.c,v 1.1 2006/08/12 23:49:54 christos 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. + * 3. 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> +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)strdup.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: strndup.c,v 1.1 2006/08/12 23:49:54 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include <assert.h> +#include <errno.h> +#include <stdlib.h> +#include <string.h> + +#ifdef __weak_alias +__weak_alias(strndup,_strndup) +#endif + +char * +strndup(const char *str, size_t n) +{ + size_t len; + char *copy; + + _DIAGASSERT(str != NULL); + + len = strlen(str); + if (len > n) + len = n; + if (!(copy = malloc(len + 1))) + return (NULL); + memcpy(copy, str, len + 1); + copy[len] = '\0'; + return (copy); +} diff --git a/lib/libc/string/strsep.3 b/lib/libc/string/strsep.3 index 02e7089c437..c27a972c89e 100644 --- a/lib/libc/string/strsep.3 +++ b/lib/libc/string/strsep.3 @@ -29,13 +29,14 @@ .\" SUCH DAMAGE. .\" .\" from: @(#)strsep.3 8.1 (Berkeley) 6/9/93 -.\" $NetBSD: strsep.3,v 1.16 2004/04/13 23:03:22 wiz Exp $ +.\" $NetBSD: strsep.3,v 1.17 2006/08/12 23:49:54 christos Exp $ .\" .Dd August 11, 2002 .Dt STRSEP 3 .Os .Sh NAME -.Nm strsep +.Nm strsep , +.Nm stresep .Nd separate strings .Sh LIBRARY .Lb libc @@ -43,6 +44,8 @@ .In string.h .Ft char * .Fn strsep "char **stringp" "const char *delim" +.Ft char * +.Fn stresep "char **stringp" "const char *delim" "int escape" .Sh DESCRIPTION The .Fn strsep @@ -79,6 +82,10 @@ is initially .Fn strsep returns .Dv NULL . +The +.Fn strnsep +function also takes an escape character that allows quoting the the delimiter +character so that it can be part of the source string. .Sh EXAMPLES The following uses .Fn strsep |
