From 72d00d114d3eef62a99112550aa3878f1d910e0c Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 31 Aug 2004 00:59:23 +0000 Subject: [PATCH] Add strtok_r(). --- src/lib/lib.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/lib/lib.h | 1 + 2 files changed, 44 insertions(+) diff --git a/src/lib/lib.c b/src/lib/lib.c index e443be0..25b4cc1 100644 --- a/src/lib/lib.c +++ b/src/lib/lib.c @@ -155,6 +155,49 @@ strcmp (const char *a_, const char *b_) return *a < *b ? -1 : *a > *b; } + +char * +strtok_r (char *s, const char *delimiters, char **save_ptr) +{ + char *token; + + ASSERT (delimiters != NULL); + ASSERT (save_ptr != NULL); + + /* If S is nonnull, start from it. + If S is null, start from saved position. */ + if (s == NULL) + s = *save_ptr; + ASSERT (s != NULL); + + /* Skip any DELIMITERS at our current position. */ + while (strchr (delimiters, *s) != NULL) + { + /* strchr() will always return nonnull if we're searching + for a null byte, because every string contains a null + byte (at the end). */ + if (*s == '\0') + { + *save_ptr = s; + return NULL; + } + + s++; + } + + /* Skip any non-DELIMITERS up to the end of the string. */ + token = s; + while (strchr (delimiters, *s) == NULL) + s++; + if (*s != '\0') + { + *s = '\0'; + *save_ptr = s + 1; + } + else + *save_ptr = s; + return token; +} static void vprintf_core (const char *format, va_list args, diff --git a/src/lib/lib.h b/src/lib/lib.h index 260c367..e163d87 100644 --- a/src/lib/lib.h +++ b/src/lib/lib.h @@ -19,6 +19,7 @@ char *strchr (const char *, int); size_t strlcpy (char *, const char *, size_t); size_t strlen (const char *); int strcmp (const char *, const char *); +char *strtok_r (char *, const char *, char **); void vprintk (const char *, va_list); void printk (const char *, ...) PRINTF_FORMAT (1, 2); -- 2.30.2