- Add strndup and stresep

- Use stresep so in fstab so that we can mount paths with white space in them.
This commit is contained in:
christos 2006-08-12 23:49:53 +00:00
parent 448cb4aa28
commit cbfb283c65
8 changed files with 210 additions and 26 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: fstab.c,v 1.27 2005/12/24 21:11:16 perry Exp $ */ /* $NetBSD: fstab.c,v 1.28 2006/08/12 23:49:54 christos Exp $ */
/* /*
* Copyright (c) 1980, 1988, 1993 * Copyright (c) 1980, 1988, 1993
@ -34,7 +34,7 @@
#if 0 #if 0
static char sccsid[] = "@(#)fstab.c 8.1 (Berkeley) 6/4/93"; static char sccsid[] = "@(#)fstab.c 8.1 (Berkeley) 6/4/93";
#else #else
__RCSID("$NetBSD: fstab.c,v 1.27 2005/12/24 21:11:16 perry Exp $"); __RCSID("$NetBSD: fstab.c,v 1.28 2006/08/12 23:49:54 christos Exp $");
#endif #endif
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
@ -65,27 +65,26 @@ static struct fstab _fs_fstab;
static int fstabscan __P((void)); static int fstabscan __P((void));
static inline char *nextfld __P((char **, const char *)); static char *nextfld(char **, const char *);
static int fstabscan(void);
static inline char * static char *
nextfld(str, sep) nextfld(char **str, const char *sep)
char **str;
const char *sep;
{ {
char *ret; char *ret;
_DIAGASSERT(str != NULL); _DIAGASSERT(str != NULL);
_DIAGASSERT(sep != NULL); _DIAGASSERT(sep != NULL);
while ((ret = strsep(str, sep)) != NULL && *ret == '\0') while ((ret = stresep(str, sep, '\\')) != NULL && *ret == '\0')
continue; continue;
return ret; return ret;
} }
static int static int
fstabscan() fstabscan(void)
{ {
char *cp, *lp, *sp; char *cp, *lp, *sp;
#define MAXLINELENGTH 1024 #define MAXLINELENGTH 1024
@ -176,7 +175,7 @@ bad:
} }
struct fstab * struct fstab *
getfsent() getfsent(void)
{ {
if ((!_fs_fp && !setfsent()) || !fstabscan()) if ((!_fs_fp && !setfsent()) || !fstabscan())
return NULL; return NULL;
@ -184,8 +183,7 @@ getfsent()
} }
struct fstab * struct fstab *
getfsspec(name) getfsspec(const char *name)
const char *name;
{ {
_DIAGASSERT(name != NULL); _DIAGASSERT(name != NULL);
@ -198,8 +196,7 @@ getfsspec(name)
} }
struct fstab * struct fstab *
getfsfile(name) getfsfile(const char *name)
const char *name;
{ {
_DIAGASSERT(name != NULL); _DIAGASSERT(name != NULL);
@ -212,7 +209,7 @@ getfsfile(name)
} }
int int
setfsent() setfsent(void)
{ {
_fs_lineno = 0; _fs_lineno = 0;
if (_fs_fp) { if (_fs_fp) {
@ -227,7 +224,7 @@ setfsent()
} }
void void
endfsent() endfsent(void)
{ {
if (_fs_fp) { if (_fs_fp) {
(void)fclose(_fs_fp); (void)fclose(_fs_fp);

View File

@ -1,4 +1,4 @@
/* $NetBSD: namespace.h,v 1.117 2006/06/23 17:15:18 christos Exp $ */ /* $NetBSD: namespace.h,v 1.118 2006/08/12 23:49:54 christos Exp $ */
/*- /*-
* Copyright (c) 1997-2004 The NetBSD Foundation, Inc. * Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
@ -534,6 +534,8 @@
#define statvfs(a, b) _statvfs(a, b) #define statvfs(a, b) _statvfs(a, b)
#define strcasecmp _strcasecmp #define strcasecmp _strcasecmp
#define strdup _strdup #define strdup _strdup
#define stresep _stresep
#define strndup _strndup
#define strncasecmp _strncasecmp #define strncasecmp _strncasecmp
#define strptime _strptime #define strptime _strptime
#define strsep _strsep #define strsep _strsep

View File

@ -1,4 +1,4 @@
# $NetBSD: shlib_version,v 1.180 2006/07/31 16:39:23 martin Exp $ # $NetBSD: shlib_version,v 1.181 2006/08/12 23:49:53 christos Exp $
# Remember to update distrib/sets/lists/base/shl.* when changing # Remember to update distrib/sets/lists/base/shl.* when changing
# #
# things we wish to do on next major version bump: # things we wish to do on next major version bump:
@ -17,4 +17,4 @@
# - libc/net/getnet{ent,namadr}.c, netdb.h: remove __n_pad0 # - libc/net/getnet{ent,namadr}.c, netdb.h: remove __n_pad0
# #
major=12 major=12
minor=145 minor=146

View File

@ -1,12 +1,13 @@
# from: @(#)Makefile.inc 8.1 (Berkeley) 6/4/93 # 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 # string sources
.PATH: ${ARCHDIR}/string ${.CURDIR}/string .PATH: ${ARCHDIR}/string ${.CURDIR}/string
SRCS+= bm.c strcasecmp.c strncasecmp.c strcasestr.c strcoll.c strdup.c \ 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 \ 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 # wide char
SRCS+= wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ 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+=strtok.3 strtok_r.3
MLINKS+=strerror.3 strerror_r.3 strerror.3 perror.3 \ MLINKS+=strerror.3 strerror_r.3 strerror.3 perror.3 \
strerror.3 sys_errlist.3 strerror.3 sys_nerr.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 \ MLINKS+=wmemchr.3 wmemcmp.3 wmemchr.3 wmemcpy.3 \
wmemchr.3 wmemmove.3 wmemchr.3 wmemset.3 \ wmemchr.3 wmemmove.3 wmemchr.3 wmemset.3 \
wmemchr.3 wcscat.3 wmemchr.3 wcschr.3 \ wmemchr.3 wcscat.3 wmemchr.3 wcschr.3 \

View File

@ -26,13 +26,14 @@
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" from: @(#)strdup.3 8.1 (Berkeley) 6/9/93 .\" 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 .Dt STRDUP 3
.Os .Os
.Sh NAME .Sh NAME
.Nm strdup .Nm strdup ,
.Nm strndup
.Nd save a copy of a string .Nd save a copy of a string
.Sh LIBRARY .Sh LIBRARY
.Lb libc .Lb libc
@ -40,6 +41,8 @@
.In string.h .In string.h
.Ft char * .Ft char *
.Fn strdup "const char *str" .Fn strdup "const char *str"
.Ft char *
.Fn strndup "const char *str" "size_t len"
.Sh DESCRIPTION .Sh DESCRIPTION
The The
.Fn strdup .Fn strdup
@ -53,6 +56,16 @@ argument to the function
.Xr free 3 . .Xr free 3 .
.Pp .Pp
If insufficient memory is available, NULL is returned. 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 .Sh EXAMPLES
The following will point The following will point
.Va p .Va p

94
lib/libc/string/stresep.c Normal file
View File

@ -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);
}
}

68
lib/libc/string/strndup.c Normal file
View File

@ -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);
}

View File

@ -29,13 +29,14 @@
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" from: @(#)strsep.3 8.1 (Berkeley) 6/9/93 .\" 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 .Dd August 11, 2002
.Dt STRSEP 3 .Dt STRSEP 3
.Os .Os
.Sh NAME .Sh NAME
.Nm strsep .Nm strsep ,
.Nm stresep
.Nd separate strings .Nd separate strings
.Sh LIBRARY .Sh LIBRARY
.Lb libc .Lb libc
@ -43,6 +44,8 @@
.In string.h .In string.h
.Ft char * .Ft char *
.Fn strsep "char **stringp" "const char *delim" .Fn strsep "char **stringp" "const char *delim"
.Ft char *
.Fn stresep "char **stringp" "const char *delim" "int escape"
.Sh DESCRIPTION .Sh DESCRIPTION
The The
.Fn strsep .Fn strsep
@ -79,6 +82,10 @@ is initially
.Fn strsep .Fn strsep
returns returns
.Dv NULL . .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 .Sh EXAMPLES
The following uses The following uses
.Fn strsep .Fn strsep