From 95b41d5986bfd5068c174d8b3b26b49a0237e02e Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Wed, 7 Jan 2026 16:04:18 -0500 Subject: [PATCH] libroot/posix: Replace strtok_r with musl's. And rewrite/cleanup strtok.c also. --- src/system/libroot/posix/musl/string/Jamfile | 1 + .../libroot/posix/musl/string/strtok_r.c | 12 +++++ src/system/libroot/posix/string/strtok.c | 48 +++---------------- 3 files changed, 20 insertions(+), 41 deletions(-) create mode 100644 src/system/libroot/posix/musl/string/strtok_r.c diff --git a/src/system/libroot/posix/musl/string/Jamfile b/src/system/libroot/posix/musl/string/Jamfile index 4ef9c52b80..c6a5478f8e 100644 --- a/src/system/libroot/posix/musl/string/Jamfile +++ b/src/system/libroot/posix/musl/string/Jamfile @@ -27,6 +27,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { strpbrk.c strspn.c strstr.c + strtok_r.c swab.c ; } diff --git a/src/system/libroot/posix/musl/string/strtok_r.c b/src/system/libroot/posix/musl/string/strtok_r.c new file mode 100644 index 0000000000..862d4fe485 --- /dev/null +++ b/src/system/libroot/posix/musl/string/strtok_r.c @@ -0,0 +1,12 @@ +#include + +char *strtok_r(char *restrict s, const char *restrict sep, char **restrict p) +{ + if (!s && !(s = *p)) return NULL; + s += strspn(s, sep); + if (!*s) return *p = 0; + *p = s + strcspn(s, sep); + if (**p) *(*p)++ = 0; + else *p = 0; + return s; +} diff --git a/src/system/libroot/posix/string/strtok.c b/src/system/libroot/posix/string/strtok.c index 8c50f3393c..35f6a73145 100644 --- a/src/system/libroot/posix/string/strtok.c +++ b/src/system/libroot/posix/string/strtok.c @@ -1,48 +1,14 @@ /* -** Copyright 2001, Travis Geiselbrecht. All rights reserved. -** Distributed under the terms of the NewOS License. -*/ + * Copyright 2026, Haiku, Inc. All rights reserved. + * Distributed under the terms of the MIT License. + */ - -#include #include -static char *___strtok = NULL; - - -char * -strtok_r(char *s, char const *ct, char **save_ptr) +char* +strtok(char* str, char const* sep) { - char *sbegin, *send; - - if (!s && !save_ptr) - return NULL; - - sbegin = s ? s : *save_ptr; - if (!sbegin) - return NULL; - - sbegin += strspn(sbegin, ct); - if (*sbegin == '\0') { - if (save_ptr) - *save_ptr = NULL; - return NULL; - } - - send = strpbrk(sbegin, ct); - if (send && *send != '\0') - *send++ = '\0'; - if (save_ptr) - *save_ptr = send; - - return sbegin; + static char *p = NULL; + return strtok_r(str, sep, &p); } - - -char * -strtok(char *s, char const *ct) -{ - return strtok_r(s, ct, &___strtok); -} -