summaryrefslogtreecommitdiff
path: root/userland/libc/string/strtok.c
blob: a4a9d0fde7dc2419940660a21e8655a8d87d0f24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <string.h>

char *strtok_s;

char *strtok(char *restrict s, const char *restrict sep) {
  if (s) {
    strtok_s = s;
    return strtok(NULL, sep);
  }
  if (!strtok_s) {
    return NULL;
  }
  char *e = strpbrk(strtok_s, sep);
  if (!e) {
    char *r = strtok_s;
    strtok_s = NULL;
    return r;
  }
  *e = '\0';
  e--;
  for (; *sep; sep++) {
    e++;
  }
  e++;
  char *r = strtok_s;
  strtok_s = e;
  return r;
}