strtok.c (765B)
1 /* 2 * strtok.c - extract tokens from string 3 * Copyright (C) 2026. All rights reserved. 4 */ 5 6 #include "libc.h" 7 8 char* strtok(char *str, const char *delim) { 9 static char *next_token = NULL; 10 11 if (str != NULL) { 12 next_token = str; 13 } 14 15 if (next_token == NULL) { 16 return NULL; 17 } 18 19 char *token_start = next_token; 20 while (*token_start != '\0' && strchr(delim, *token_start) != NULL) { 21 token_start++; 22 } 23 24 if (*token_start == '\0') { 25 next_token = NULL; 26 return NULL; 27 } 28 29 char *token_end = token_start; 30 while (*token_end != '\0' && strchr(delim, *token_end) == NULL) { 31 token_end++; 32 } 33 34 if (*token_end != '\0') { 35 *token_end = '\0'; 36 next_token = token_end + 1; 37 } else { 38 next_token = NULL; 39 } 40 41 return token_start; 42 }