#include #include const char *my_strtok(const char *haystack, const char *needle) { static const char *_haystack; static char _buffer[1024]; char *pos; unsigned int needlelen; _buffer[1023] = '\0'; if (haystack) { _haystack = haystack; } if (!_haystack || !*_haystack) { return NULL; } if (!needle || !*needle) { return _haystack; } needlelen = strlen(needle); pos = strstr(_haystack, needle); while (pos == _haystack) { _haystack += needlelen; pos = strstr(_haystack, needle); } if (!*_haystack) { _haystack = NULL; return NULL; } if (!pos) { strncpy(_buffer, _haystack, 1023); _haystack = NULL; } else { unsigned int length = pos - _haystack; if (length > 1023) length = 1023; strncpy(_buffer, _haystack, length); _haystack += length + needlelen; } return _buffer; } int main(int argc, char **argv) { char *plop = argv[1]; const char *bar; while ((bar = my_strtok(plop, "a"))) { plop = NULL; printf("%s\n", bar); } plop = argv[1]; while ((bar = strtok(plop, "a"))) { plop = NULL; printf("%s\n", bar); } return 0; }