From 5ab34eb11b980a122b4358997119c99156702389 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Wed, 18 Oct 2023 20:46:41 -0400 Subject: [PATCH] string: add strtok --- include/string.h | 4 +++- lib/string.c | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/include/string.h b/include/string.h index 65d6556..a0a0236 100644 --- a/include/string.h +++ b/include/string.h @@ -4,12 +4,14 @@ #ifndef __ASM__ -#include +#include +#include void *memcpy(void *dest, const void *src, size_t num); int memcmp(const void *aa, const void *bb, size_t num); void *memset(void *str, int c, size_t n); char *strcpy(char *dest, const char *src); +char *strtok(char *str, const char *delim); char **strsplit(const char *str, char delim, int *num); size_t strlen(const char *c); int atoi(const char *a); diff --git a/lib/string.c b/lib/string.c index 5c8e414..2b5dcc8 100644 --- a/lib/string.c +++ b/lib/string.c @@ -86,6 +86,27 @@ char *strchr(const char *s, int c) return NULL; } +char *strtok(char *str, const char *delim) +{ + static char *last = NULL; + if (str) + last = str; + + if (!last) + return NULL; + + char *ret = last; + while (*last) { + if (strchr(delim, *last)) { + *last = 0; + last++; + return ret; + } + last++; + } + return ret; +} + char **strsplit(const char *s, char delim, int *n) { int num = 0;