1 /* 2 ** Copyright 2001, Travis Geiselbrecht. All rights reserved. 3 ** Distributed under the terms of the NewOS License. 4 */ 5 6 7 #include <sys/types.h> 8 #include <string.h> 9 10 11 static char *___strtok = NULL; 12 13 14 char * 15 strtok_r(char *s, char const *ct, char **save_ptr) 16 { 17 char *sbegin, *send; 18 19 if (!s && !save_ptr) 20 return NULL; 21 22 sbegin = s ? s : *save_ptr; 23 if (!sbegin) 24 return NULL; 25 26 sbegin += strspn(sbegin, ct); 27 if (*sbegin == '\0') { 28 if (save_ptr) 29 *save_ptr = NULL; 30 return NULL; 31 } 32 33 send = strpbrk(sbegin, ct); 34 if (send && *send != '\0') 35 *send++ = '\0'; 36 if (save_ptr) 37 *save_ptr = send; 38 39 return sbegin; 40 } 41 42 43 char * 44 strtok(char *s, char const *ct) 45 { 46 return strtok_r(s, ct, &___strtok); 47 } 48 49