strtok.c 826 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #define N 256
  12. char*
  13. strtok(char *s, char *b)
  14. {
  15. static char *under_rock;
  16. char map[N], *os;
  17. memset(map, 0, N);
  18. while(*b)
  19. map[*(uint8_t*)b++] = 1;
  20. if(s == 0)
  21. s = under_rock;
  22. while(map[*(uint8_t*)s++])
  23. ;
  24. if(*--s == 0)
  25. return 0;
  26. os = s;
  27. while(map[*(uint8_t*)s] == 0)
  28. if(*s++ == 0) {
  29. under_rock = s-1;
  30. return os;
  31. }
  32. *s++ = 0;
  33. under_rock = s;
  34. return os;
  35. }