meta: move photon/libc to root
This commit is contained in:
62
libc/string/strtok.c
Normal file
62
libc/string/strtok.c
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <string.h>
|
||||
|
||||
static char *global_sp = NULL;
|
||||
|
||||
char *strtok(char *str, const char *delim)
|
||||
{
|
||||
return strtok_r(str, delim, &global_sp);
|
||||
}
|
||||
|
||||
char *strtok_r(char *str, const char *delim, char **sp)
|
||||
{
|
||||
int i = 0;
|
||||
int len = strlen(delim);
|
||||
|
||||
if (len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!str && !sp) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (str) {
|
||||
*sp = str;
|
||||
}
|
||||
|
||||
char *p_start = *sp;
|
||||
while (1) {
|
||||
for (i = 0; i < len; i++) {
|
||||
if (*p_start == delim[i]) {
|
||||
p_start++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == len) {
|
||||
*sp = p_start;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (**sp == '\0') {
|
||||
*sp = NULL;
|
||||
return *sp;
|
||||
}
|
||||
|
||||
while (**sp != '\0') {
|
||||
for (i = 0; i < len; i++) {
|
||||
if (**sp == delim[i]) {
|
||||
**sp = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
(*sp)++;
|
||||
if (i < len) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return p_start;
|
||||
}
|
||||
Reference in New Issue
Block a user