]> pd.if.org Git - zos/blob - klib/strtok.c
klib and makefile
[zos] / klib / strtok.c
1 #include <string.h>
2
3 char *strtok(char *s1, const char *s2) {
4         static char *tmp = NULL;
5         const char *p = s2;
6
7         if (s1 != NULL) {
8                 /* new string */
9                 tmp = s1;
10         } else {
11                 /* old string continued */
12                 if (tmp == NULL) {
13                         /* No old string, no new string, nothing to do */
14                         return NULL;
15                 }
16                 s1 = tmp;
17         }
18
19         /* skipping leading s2 characters */
20         while (*p && *s1) {
21                 if ( *s1 == *p ) {
22                         /* found seperator; skip and start over */
23                         ++s1;
24                         p = s2;
25                         continue;
26                 }
27                 ++p;
28         }
29
30         if (!*s1) {
31                 /* no more to parse */
32                 return ( tmp = NULL );
33         }
34
35         /* skipping non-s2 characters */
36         tmp = s1;
37         while (*tmp) {
38                 p = s2;
39                 while (*p) {
40                         if (*tmp == *p++) {
41                                 /* found seperator; overwrite with '\0', position tmp, return */
42                                 *tmp++ = '\0';
43                                 return s1;
44                         }
45                 }
46                 ++tmp;
47         }
48
49         /* parsed to end of string */
50         tmp = NULL;
51         return s1;
52 }