#include #include static int is_delim(char c, const char* delim) { while (*delim) { if (c == *delim) { return 1; } delim++; } return 0; } char* strtok_r(char* str, const char* delim, char** saveptr) { if (saveptr == NULL) { return NULL; } char* next_token = (str != NULL) ? str : *saveptr; if (next_token == NULL || *next_token == '\0') { *saveptr = NULL; return NULL; } while (*next_token && is_delim(*next_token, delim)) { next_token++; } if (*next_token == '\0') { *saveptr = next_token; return NULL; } char* token_start = next_token; while (*next_token) { if (is_delim(*next_token, delim)) { *next_token = '\0'; // Destructively null-terminate the token *saveptr = next_token + 1; // Save the position right after the null-terminator return token_start; } next_token++; } *saveptr = next_token; return token_start; }