62b0fdc7ad88a3279f565bac3a740633fb1f0d94
[reactos.git] / lib / crtdll / string / strtok.c
1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
2 #include <crtdll/string.h>
3
4 char *
5 strtok(char *s, const char *delim)
6 {
7   const char *spanp;
8   int c, sc;
9   char *tok;
10   static char *last;
11
12
13   if (s == NULL && (s = last) == NULL)
14     return (NULL);
15
16   /*
17    * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
18    */
19  cont:
20   c = *s++;
21   for (spanp = delim; (sc = *spanp++) != 0;) {
22     if (c == sc)
23       goto cont;
24   }
25
26   if (c == 0) {                 /* no non-delimiter characters */
27     last = NULL;
28     return (NULL);
29   }
30   tok = s - 1;
31
32   /*
33    * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
34    * Note that delim must have one NUL; we stop if we see that, too.
35    */
36   for (;;) {
37     c = *s++;
38     spanp = delim;
39     do {
40       if ((sc = *spanp++) == c) {
41         if (c == 0)
42           s = NULL;
43         else
44           s[-1] = 0;
45         last = s;
46         return (tok);
47       }
48     } while (sc != 0);
49   }
50   /* NOTREACHED */
51 }