strtok.c 408 B

123456789101112131415161718192021222324252627282930
  1. #include <u.h>
  2. #include <libc.h>
  3. #define N 256
  4. char*
  5. strtok(char *s, char *b)
  6. {
  7. static char *under_rock;
  8. char map[N], *os;
  9. memset(map, 0, N);
  10. while(*b)
  11. map[*(uchar*)b++] = 1;
  12. if(s == 0)
  13. s = under_rock;
  14. while(map[*(uchar*)s++])
  15. ;
  16. if(*--s == 0)
  17. return 0;
  18. os = s;
  19. while(map[*(uchar*)s] == 0)
  20. if(*s++ == 0) {
  21. under_rock = s-1;
  22. return os;
  23. }
  24. *s++ = 0;
  25. under_rock = s;
  26. return os;
  27. }