xref: /reactos/sdk/lib/crt/mbstring/mbstok.c (revision c2c66aff)
1 #include <stdlib.h>
2 #include <mbstring.h>
3 
4 /*
5  * @implemented
6  */
_mbstok(unsigned char * s,const unsigned char * delim)7 unsigned char * _mbstok(unsigned char *s, const unsigned char *delim)
8 {
9   const unsigned char *spanp;
10   int c, sc;
11   unsigned char *tok;
12   static unsigned char *last;
13 
14 
15   if (s == NULL && (s = last) == NULL)
16     return (NULL);
17 
18   /*
19    * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
20    */
21  cont:
22   c = *s;
23   s = _mbsinc(s);
24 
25   for (spanp = delim; (sc = *spanp) != 0; spanp = _mbsinc(spanp)) {
26     if (c == sc)
27       goto cont;
28   }
29 
30   if (c == 0) {			/* no non-delimiter characters */
31     last = NULL;
32     return (NULL);
33   }
34   tok = s - 1;
35 
36   /*
37    * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
38    * Note that delim must have one NUL; we stop if we see that, too.
39    */
40   for (;;) {
41     c = *s;
42     s = _mbsinc(s);
43     spanp = delim;
44     do {
45       if ((sc = *spanp) == c) {
46 	if (c == 0)
47 	  s = NULL;
48 	else
49 	  s[-1] = 0;
50 	last = s;
51 	return (tok);
52       }
53       spanp = _mbsinc(spanp);
54     } while (sc != 0);
55   }
56   /* NOTREACHED */
57 }
58