diff options
Diffstat (limited to 'main/glib/strtok.c')
-rw-r--r-- | main/glib/strtok.c | 133 |
1 files changed, 69 insertions, 64 deletions
diff --git a/main/glib/strtok.c b/main/glib/strtok.c index 063a554..1b4d3a5 100644 --- a/main/glib/strtok.c +++ b/main/glib/strtok.c @@ -5,7 +5,7 @@ * Oct 13, 1998 by Wes Peters <wes@softweyr.com> * * Copyright (c) 1988, 1993 - * The Regents of the University of California. All rights reserved. + * 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 @@ -44,62 +44,65 @@ __FBSDID("$FreeBSD$"); #endif #include <string.h> -char *__strtok_r(char *, const char *, char **); +char *__strtok_r(char *, const char *, char **); __weak_reference(__strtok_r, strtok_r); char * __strtok_r(char *s, const char *delim, char **last) { - char *spanp, *tok; - int c, sc; + char *spanp, *tok; + int c, sc; - if (s == NULL && (s = *last) == NULL) - return (NULL); + if(s == NULL && (s = *last) == NULL) { + return (NULL); + } - /* - * Skip (span) leading delimiters (s += strspn(s, delim), sort of). - */ + /* + * Skip (span) leading delimiters (s += strspn(s, delim), sort of). + */ cont: - c = *s++; - for (spanp = (char *)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 = (char *)delim; - do { - if ((sc = *spanp++) == c) { - if (c == 0) - s = NULL; - else - s[-1] = '\0'; - *last = s; - return (tok); - } - } while (sc != 0); - } - /* NOTREACHED */ + c = *s++; + for(spanp = (char *)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 = (char *)delim; + do { + if((sc = *spanp++) == c) { + if(c == 0) { + s = NULL; + } else { + s[-1] = '\0'; + } + *last = s; + return (tok); + } + } while(sc != 0); + } + /* NOTREACHED */ } char * strtok(char *s, const char *delim) { - static char *last; + static char *last; - return (__strtok_r(s, delim, &last)); + return (__strtok_r(s, delim, &last)); } #ifdef DEBUG_STRTOK @@ -109,28 +112,30 @@ strtok(char *s, const char *delim) int main(void) { - char blah[80], test[80]; - char *brkb, *brkt, *phrase, *sep, *word; - - sep = "\\/:;=-"; - phrase = "foo"; - - printf("String tokenizer test:\n"); - strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function."); - for (word = strtok(test, sep); word; word = strtok(NULL, sep)) - printf("Next word is \"%s\".\n", word); - strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function."); - - for (word = strtok_r(test, sep, &brkt); word; - word = strtok_r(NULL, sep, &brkt)) { - strcpy(blah, "blah:blat:blab:blag"); - - for (phrase = strtok_r(blah, sep, &brkb); phrase; - phrase = strtok_r(NULL, sep, &brkb)) - printf("So far we're at %s:%s\n", word, phrase); - } - - return (0); + char blah[80], test[80]; + char *brkb, *brkt, *phrase, *sep, *word; + + sep = "\\/:;=-"; + phrase = "foo"; + + printf("String tokenizer test:\n"); + strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function."); + for(word = strtok(test, sep); word; word = strtok(NULL, sep)) { + printf("Next word is \"%s\".\n", word); + } + strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function."); + + for(word = strtok_r(test, sep, &brkt); word; + word = strtok_r(NULL, sep, &brkt)) { + strcpy(blah, "blah:blat:blab:blag"); + + for(phrase = strtok_r(blah, sep, &brkb); phrase; + phrase = strtok_r(NULL, sep, &brkb)) { + printf("So far we're at %s:%s\n", word, phrase); + } + } + + return (0); } #endif /* DEBUG_STRTOK */ |