From 7a364eb36494dcac90196db3d93d15086d6c1bc1 Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Fri, 24 May 2002 00:13:57 +0000 Subject: [PATCH] 2002-05-23 Jeff Johnston * libc/string/Makefile.am: Add support for strsep.c. * libc/string/Makefile.in: Regenerated. * libc/string/strsep.c: New file. * libc/string/strtok.c: Change to call __strtok_r service routine. * libc/string/strtok_r.c: Add __strtok_r routine which takes additional flag parameter regarding whether to skip leading delimeters. Change strtok_r to call __strtok_r. --- newlib/ChangeLog | 10 ++++++++++ newlib/libc/string/Makefile.am | 1 + newlib/libc/string/Makefile.in | 12 +++++++----- newlib/libc/string/strsep.c | 19 +++++++++++++++++++ newlib/libc/string/strtok.c | 34 ++++++++++++++++++++++++++++------ newlib/libc/string/strtok_r.c | 26 ++++++++++++++++++++++---- 6 files changed, 87 insertions(+), 15 deletions(-) create mode 100644 newlib/libc/string/strsep.c diff --git a/newlib/ChangeLog b/newlib/ChangeLog index 3aa09e495..32bf90f04 100644 --- a/newlib/ChangeLog +++ b/newlib/ChangeLog @@ -1,3 +1,13 @@ +2002-05-23 Jeff Johnston + + * libc/string/Makefile.am: Add support for strsep.c. + * libc/string/Makefile.in: Regenerated. + * libc/string/strsep.c: New file. + * libc/string/strtok.c: Change to call __strtok_r service routine. + * libc/string/strtok_r.c: Add __strtok_r routine which takes + additional flag parameter regarding whether to skip leading delimeters. + Change strtok_r to call __strtok_r. + 2002-05-23 Gareth Pearce * libc/stdio/Makefile.am: Modify to add setbuffer.c and setlinebuf.c. diff --git a/newlib/libc/string/Makefile.am b/newlib/libc/string/Makefile.am index d97c1ff67..a56522121 100644 --- a/newlib/libc/string/Makefile.am +++ b/newlib/libc/string/Makefile.am @@ -33,6 +33,7 @@ LIB_SOURCES = \ strncpy.c \ strpbrk.c \ strrchr.c \ + strsep.c \ strspn.c \ strtok.c \ strtok_r.c \ diff --git a/newlib/libc/string/Makefile.in b/newlib/libc/string/Makefile.in index b3c41e496..c27d17aad 100644 --- a/newlib/libc/string/Makefile.in +++ b/newlib/libc/string/Makefile.in @@ -131,6 +131,7 @@ LIB_SOURCES = \ strncpy.c \ strpbrk.c \ strrchr.c \ + strsep.c \ strspn.c \ strtok.c \ strtok_r.c \ @@ -181,9 +182,9 @@ lib_a_LIBADD = @USE_LIBTOOL_FALSE@rindex.o strcat.o strchr.o strcmp.o strcasecmp.o \ @USE_LIBTOOL_FALSE@strcoll.o strcpy.o strcspn.o strerror.o strlcat.o \ @USE_LIBTOOL_FALSE@strlcpy.o strlen.o strlwr.o strncat.o strncmp.o \ -@USE_LIBTOOL_FALSE@strncasecmp.o strncpy.o strpbrk.o strrchr.o strspn.o \ -@USE_LIBTOOL_FALSE@strtok.o strtok_r.o strupr.o strxfrm.o strstr.o \ -@USE_LIBTOOL_FALSE@swab.o u_strerr.o +@USE_LIBTOOL_FALSE@strncasecmp.o strncpy.o strpbrk.o strrchr.o strsep.o \ +@USE_LIBTOOL_FALSE@strspn.o strtok.o strtok_r.o strupr.o strxfrm.o \ +@USE_LIBTOOL_FALSE@strstr.o swab.o u_strerr.o LTLIBRARIES = $(noinst_LTLIBRARIES) libstring_la_LIBADD = @@ -193,8 +194,9 @@ libstring_la_LIBADD = @USE_LIBTOOL_TRUE@strcasecmp.lo strcoll.lo strcpy.lo strcspn.lo \ @USE_LIBTOOL_TRUE@strerror.lo strlcat.lo strlcpy.lo strlen.lo strlwr.lo \ @USE_LIBTOOL_TRUE@strncat.lo strncmp.lo strncasecmp.lo strncpy.lo \ -@USE_LIBTOOL_TRUE@strpbrk.lo strrchr.lo strspn.lo strtok.lo strtok_r.lo \ -@USE_LIBTOOL_TRUE@strupr.lo strxfrm.lo strstr.lo swab.lo u_strerr.lo +@USE_LIBTOOL_TRUE@strpbrk.lo strrchr.lo strsep.lo strspn.lo strtok.lo \ +@USE_LIBTOOL_TRUE@strtok_r.lo strupr.lo strxfrm.lo strstr.lo swab.lo \ +@USE_LIBTOOL_TRUE@u_strerr.lo CFLAGS = @CFLAGS@ COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) diff --git a/newlib/libc/string/strsep.c b/newlib/libc/string/strsep.c new file mode 100644 index 000000000..d3d0ad0a9 --- /dev/null +++ b/newlib/libc/string/strsep.c @@ -0,0 +1,19 @@ +/* BSD strsep function */ + +/* Copyright 2002, Red Hat Inc. */ + +/* undef STRICT_ANSI so that strsep prototype will be defined */ +#undef __STRICT_ANSI__ +#include +#include <_ansi.h> +#include + +extern char *__strtok_r (char *, const char *, char **, int); + +char * +_DEFUN (strsep, (source_ptr, delim), + register char **source_ptr _AND + register const char *delim) +{ + return __strtok_r (*source_ptr, delim, source_ptr, 0); +} diff --git a/newlib/libc/string/strtok.c b/newlib/libc/string/strtok.c index 292c16743..8d53290c7 100644 --- a/newlib/libc/string/strtok.c +++ b/newlib/libc/string/strtok.c @@ -1,6 +1,6 @@ /* FUNCTION - <>---get next token from a string + <>,<>,<>---get next token from a string INDEX strtok @@ -8,11 +8,15 @@ INDEX INDEX strtok_r +INDEX + strsep + ANSI_SYNOPSIS #include char *strtok(char *<[source]>, const char *<[delimiters]>) char *strtok_r(char *<[source]>, const char *<[delimiters]>, char **<[lasts]>) + char *strsep(char **<[source_ptr]>, const char *<[delimiters]>) TRAD_SYNOPSIS #include @@ -25,6 +29,10 @@ TRAD_SYNOPSIS char *<[delimiters]>; char **<[lasts]>; + char *strsep(<[source_ptr]>, <[delimiters]>) + char **<[source_ptr]>; + char *<[delimiters]>; + DESCRIPTION The <> function is used to isolate sequential tokens in a null-terminated string, <<*<[source]>>>. These tokens are delimited @@ -43,18 +51,30 @@ DESCRIPTION The <> function has the same behavior as <>, except a pointer to placeholder <<*[lasts]>> must be supplied by the caller. + The <> function is similar in behavior to <>, except + a pointer to the string pointer must be supplied <<[source_ptr]>> and + the function does not skip leading delimeters. When the string starts + with a delimeter, the delimeter is changed to the NUL character and + the empty string is returned. Like <> and <>, the + <<*[source_ptr]>> is updated to the next character following the + last delimeter found or NULL if the end of string is reached with + no more delimeters. + RETURNS - <> returns a pointer to the next token, or <> if - no more tokens can be found. + <>, <>, and <> all return a pointer to the + next token, or <> if no more tokens can be found. For + <>, a token may be the empty string. NOTES <> is unsafe for multi-thread applications. <> - is MT-Safe and should be used instead. + and <> are MT-Safe and should be used instead. PORTABILITY <> is ANSI C. +<> is POSIX. +<> is a BSD-extension. -<> requires no supporting OS subroutines. +<>, <>, and <> require no supporting OS subroutines. QUICKREF strtok ansi impure @@ -68,12 +88,14 @@ QUICKREF #ifndef _REENT_ONLY +extern char *__strtok_r (char *, const char *, char **, int); + char * _DEFUN (strtok, (s, delim), register char *s _AND register const char *delim) { _REENT_CHECK_MISC(_REENT); - return strtok_r (s, delim, &(_REENT_STRTOK_LAST(_REENT))); + return __strtok_r (s, delim, &(_REENT_STRTOK_LAST(_REENT)), 1); } #endif diff --git a/newlib/libc/string/strtok_r.c b/newlib/libc/string/strtok_r.c index ed323369e..c4aecc811 100644 --- a/newlib/libc/string/strtok_r.c +++ b/newlib/libc/string/strtok_r.c @@ -34,10 +34,11 @@ #include char * -_DEFUN (strtok_r, (s, delim, lasts), +_DEFUN (__strtok_r, (s, delim, lasts, skip_leading_delim), register char *s _AND register const char *delim _AND - char **lasts) + char **lasts _AND + int skip_leading_delim) { register char *spanp; register int c, sc; @@ -53,8 +54,16 @@ _DEFUN (strtok_r, (s, delim, lasts), cont: c = *s++; for (spanp = (char *)delim; (sc = *spanp++) != 0;) { - if (c == sc) - goto cont; + if (c == sc) { + if (skip_leading_delim) { + goto cont; + } + else { + *lasts = s; + s[-1] = 0; + return (s - 1); + } + } } if (c == 0) { /* no non-delimiter characters */ @@ -83,3 +92,12 @@ cont: } /* NOTREACHED */ } + +char * +_DEFUN (strtok_r, (s, delim, lasts), + register char *s _AND + register const char *delim _AND + char **lasts) +{ + return __strtok_r (s, delim, lasts, 1); +}