X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f From: Message-Id: <200501031642.j03GgKJ9026830@speedy.ludd.ltu.se> Subject: strtok_r() patch To: DJGPP-WORKERS Date: Mon, 3 Jan 2005 17:42:20 +0100 (CET) X-Mailer: ELM [version 2.4ME+ PL78 (25)] MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII X-ltu-MailScanner-Information: Please contact the ISP for more information X-ltu-MailScanner: Found to be clean X-MailScanner-From: ams AT ludd DOT ltu DOT se Reply-To: djgpp-workers AT delorie DOT com Hello. Here's my patch that adds strtok_r(). There will be some fuzz if somebody tries to apply it as I've had to remove some lines containing other changes. It doesn't contain a test case for it, but I made one for strtok() at least. Right, MartinS Index: djgpp/include/string.h =================================================================== RCS file: /cvs/djgpp/djgpp/include/string.h,v retrieving revision 1.7 diff -p -u -r1.7 string.h --- djgpp/include/string.h 4 Feb 2003 20:24:20 -0000 1.7 +++ djgpp/include/string.h 3 Jan 2005 16:28:47 -0000 @@ -54,6 +54,9 @@ size_t strxfrm(char *_s1, const char *_s #ifndef __STRICT_ANSI__ +char * strtok_r(char * _s1, const char * _s2, char ** _s3); + #ifndef _POSIX_SOURCE #include Index: djgpp/include/libc/stubs.h =================================================================== RCS file: /cvs/djgpp/djgpp/include/libc/stubs.h,v retrieving revision 1.15 diff -p -u -r1.15 stubs.h --- djgpp/include/libc/stubs.h 7 May 2004 08:10:30 -0000 1.15 +++ djgpp/include/libc/stubs.h 3 Jan 2005 16:28:47 -0000 @@ -27,12 +27,15 @@ extern "C" { #define mkdir __mkdir #define open __open #define putenv __putenv #define read __read #define statvfs __statvfs +#define strtok_r __strtok_r #define tzset __tzset #define write __write Index: djgpp/src/libc/ansi/string/strtok.c =================================================================== RCS file: /cvs/djgpp/djgpp/src/libc/ansi/string/strtok.c,v retrieving revision 1.1 diff -p -u -r1.1 strtok.c --- djgpp/src/libc/ansi/string/strtok.c 29 Nov 1994 09:40:46 -0000 1.1 +++ djgpp/src/libc/ansi/string/strtok.c 3 Jan 2005 16:28:47 -0000 @@ -1,51 +1,12 @@ +/* Copyright (C) 2005 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ +#include #include char * strtok(char *s, const char *delim) { - const char *spanp; - int c, sc; - char *tok; static char *last; - - if (s == NULL && (s = last) == NULL) - return (NULL); - - /* - * Skip (span) leading delimiters (s += strspn(s, delim), sort of). - */ - cont: - c = *s++; - for (spanp = delim; (sc = *spanp++) != 0;) { - if (c == sc) - goto cont; - } - - if (c == 0) { /* no non-delimiter characters */ - last = NULL; - return (NULL); - } - tok = s - 1; - - /* - * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). - * Note that delim must have one NUL; we stop if we see that, too. - */ - for (;;) { - c = *s++; - spanp = delim; - do { - if ((sc = *spanp++) == c) { - if (c == 0) - s = NULL; - else - s[-1] = 0; - last = s; - return (tok); - } - } while (sc != 0); - } - /* NOTREACHED */ + return strtok_r(s, delim, &last); } Index: djgpp/src/libc/posix/string/makefile =================================================================== RCS file: djgpp/src/libc/posix/string/makefile diff -N djgpp/src/libc/posix/string/makefile --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ djgpp/src/libc/posix/string/makefile 3 Jan 2005 16:28:59 -0000 @@ -0,0 +1,7 @@ +# Copyright (C) 2005 DJ Delorie, see COPYING.DJ for details +TOP=../.. + +SRC += strtok_r.c + +include $(TOP)/../makefile.inc Index: djgpp/src/libc/posix/string/strtok_r.c =================================================================== RCS file: djgpp/src/libc/posix/string/strtok_r.c diff -N djgpp/src/libc/posix/string/strtok_r.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ djgpp/src/libc/posix/string/strtok_r.c 3 Jan 2005 16:28:59 -0000 @@ -0,0 +1,51 @@ +/* Copyright (C) 2005 DJ Delorie, see COPYING.DJ for details */ +/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ +#include +#include + +char * +strtok_r(char *s, const char *delim, char **last) +{ + const char *spanp; + int c, sc; + char *tok; + + if (s == NULL && (s = *last) == NULL) + return (NULL); + + /* + * Skip (span) leading delimiters (s += strspn(s, delim), sort of). + */ + cont: + c = *s++; + for (spanp = delim; (sc = *spanp++) != 0;) { + if (c == sc) + goto cont; + } + + if (c == 0) { /* no non-delimiter characters */ + *last = NULL; + return (NULL); + } + tok = s - 1; + + /* + * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). + * Note that delim must have one NUL; we stop if we see that, too. + */ + for (;;) { + c = *s++; + spanp = delim; + do { + if ((sc = *spanp++) == c) { + if (c == 0) + s = NULL; + else + s[-1] = 0; + *last = s; + return (tok); + } + } while (sc != 0); + } + /* NOTREACHED */ +} Index: djgpp/src/libc/posix/string/strtok_r.txh =================================================================== RCS file: djgpp/src/libc/posix/string/strtok_r.txh diff -N djgpp/src/libc/posix/string/strtok_r.txh --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ djgpp/src/libc/posix/string/strtok_r.txh 3 Jan 2005 16:28:59 -0000 @@ -0,0 +1,50 @@ +@node strtok_r, string +@findex strtok_r +@subheading Syntax + +@example +#include + +char *strtok_r(char *s1, const char *s2, char **s3); +@end example + +@subheading Description + +This function retrieves tokens from @var{s1} which are delimited by +characters from @var{s2}. It uses @var{s3} to track how far in +@var{s1} the search has progressed. + +To initiate the search, pass the string to be searched as @var{s1}. For +the remaining tokens, pass @code{NULL} instead. + +@subheading Return Value + +A pointer to the token, or @code{NULL} if no more are found. + +@subheading Portability + +@portability posix + +@subheading Example + +@example +#include +#include + +int main(void) +@{ + char buf[] = "Hello there, stranger"; + char *tok, *where; + for (tok = strtok(buf, " ,", &where); + tok; + tok=strtok(NULL, " ,", &where)) + printf("tok = `%s'\n", tok); + + return 0; +@} + +tok = `Hello' +tok = `there' +tok = `stranger' +@end example + Index: djgpp/tests/libc/ansi/string/makefile =================================================================== RCS file: /cvs/djgpp/djgpp/tests/libc/ansi/string/makefile,v retrieving revision 1.3 diff -p -u -r1.3 makefile --- djgpp/tests/libc/ansi/string/makefile 8 Nov 2003 12:19:41 -0000 1.3 +++ djgpp/tests/libc/ansi/string/makefile 3 Jan 2005 16:29:00 -0000 @@ -3,5 +3,6 @@ TOP=../.. SRC += collate.c SRC += memmove.c SRC += strcspn.c +SRC += strtok.c include $(TOP)/../makefile.inc Index: djgpp/tests/libc/ansi/string/strtok.c =================================================================== RCS file: djgpp/tests/libc/ansi/string/strtok.c diff -N djgpp/tests/libc/ansi/string/strtok.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ djgpp/tests/libc/ansi/string/strtok.c 3 Jan 2005 16:29:00 -0000 @@ -0,0 +1,14 @@ +#include +#include + +int main(void) +{ + char buf[] = "Hello there, stranger"; + char *tok; + for (tok = strtok(buf, " ,"); + tok; + tok=strtok(0, " ,")) + printf("tok = `%s'\n", tok); + + return 0; +}