From 46a6070b5792be7cb677dcda67400127b3c921c3 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Thu, 31 Aug 2023 15:08:48 -0400 Subject: [PATCH] libroot: Replace strcspn and strchrnul with musl versions. Removes the last BSD advertising clause from the "string" directory. --- src/system/kernel/lib/Jamfile | 5 +- src/system/libroot/posix/musl/string/Jamfile | 2 + .../libroot/posix/musl/string/strchrnul.c | 27 ++++++++ .../libroot/posix/musl/string/strcspn.c | 18 ++++++ src/system/libroot/posix/string/Jamfile | 2 - src/system/libroot/posix/string/strchrnul.c | 18 ------ src/system/libroot/posix/string/strcspn.c | 64 ------------------- src/system/runtime_loader/Jamfile | 4 +- 8 files changed, 54 insertions(+), 86 deletions(-) create mode 100644 src/system/libroot/posix/musl/string/strchrnul.c create mode 100644 src/system/libroot/posix/musl/string/strcspn.c delete mode 100644 src/system/libroot/posix/string/strchrnul.c delete mode 100644 src/system/libroot/posix/string/strcspn.c diff --git a/src/system/kernel/lib/Jamfile b/src/system/kernel/lib/Jamfile index 33f333500b..237fe6d917 100644 --- a/src/system/kernel/lib/Jamfile +++ b/src/system/kernel/lib/Jamfile @@ -51,6 +51,9 @@ local muslSources = ffs.c rand.c rand_r.c + + strchrnul.c + strcspn.c ; SourceHdrs $(muslSources) : @@ -122,7 +125,6 @@ KernelMergeObject kernel_lib_posix.o : strchr.c strcmp.c strcpy.c - strcspn.c strdup.cpp strerror.c strlcat.c @@ -147,6 +149,7 @@ KernelMergeObject kernel_lib_posix.o : SEARCH on [ FGristFiles $(muslSources) ] += [ FDirName $(posixSources) musl misc ] ; SEARCH on [ FGristFiles $(muslSources) ] += [ FDirName $(posixSources) musl prng ] ; +SEARCH on [ FGristFiles $(muslSources) ] += [ FDirName $(posixSources) musl string ] ; # misc diff --git a/src/system/libroot/posix/musl/string/Jamfile b/src/system/libroot/posix/musl/string/Jamfile index 157ab76686..ea1e43bc8f 100644 --- a/src/system/libroot/posix/musl/string/Jamfile +++ b/src/system/libroot/posix/musl/string/Jamfile @@ -10,6 +10,8 @@ for architectureObject in [ MultiArchSubDirSetup ] { MergeObject <$(architecture)>posix_musl_string.o : memrchr.c + strchrnul.c + strcspn.c swab.c ; } diff --git a/src/system/libroot/posix/musl/string/strchrnul.c b/src/system/libroot/posix/musl/string/strchrnul.c new file mode 100644 index 0000000000..60a34255d8 --- /dev/null +++ b/src/system/libroot/posix/musl/string/strchrnul.c @@ -0,0 +1,27 @@ +#define _GNU_SOURCE +#include +#include +#include + +#define ALIGN (sizeof(size_t)) +#define ONES ((size_t)-1/UCHAR_MAX) +#define HIGHS (ONES * (UCHAR_MAX/2+1)) +#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) + +char *strchrnul(const char *s, int c) +{ + c = (unsigned char)c; + if (!c) return (char *)s + strlen(s); + +#if 0 + typedef size_t __attribute__((__may_alias__)) word; + const word *w; + for (; (uintptr_t)s % ALIGN; s++) + if (!*s || *(unsigned char *)s == c) return (char *)s; + size_t k = ONES * c; + for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++); + s = (void *)w; +#endif + for (; *s && *(unsigned char *)s != c; s++); + return (char *)s; +} diff --git a/src/system/libroot/posix/musl/string/strcspn.c b/src/system/libroot/posix/musl/string/strcspn.c new file mode 100644 index 0000000000..18c870ba71 --- /dev/null +++ b/src/system/libroot/posix/musl/string/strcspn.c @@ -0,0 +1,18 @@ +#define _GNU_SOURCE +#include + +#define BITOP(a,b,op) \ + ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) + +size_t strcspn(const char *s, const char *c) +{ + const char *a = s; + size_t byteset[32/sizeof(size_t)]; + + if (!c[0] || !c[1]) return strchrnul(s, *c)-a; + + memset(byteset, 0, sizeof byteset); + for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++); + for (; *s && !BITOP(byteset, *(unsigned char *)s, &); s++); + return s-a; +} diff --git a/src/system/libroot/posix/string/Jamfile b/src/system/libroot/posix/string/Jamfile index 105ff21c63..4f67a2c887 100644 --- a/src/system/libroot/posix/string/Jamfile +++ b/src/system/libroot/posix/string/Jamfile @@ -29,11 +29,9 @@ for architectureObject in [ MultiArchSubDirSetup ] { strcasestr.c strcat.c strchr.c - strchrnul.c strcmp.c strcoll.cpp strcpy.c - strcspn.c strdup.cpp strerror.c strlcat.c diff --git a/src/system/libroot/posix/string/strchrnul.c b/src/system/libroot/posix/string/strchrnul.c deleted file mode 100644 index a83745c13e..0000000000 --- a/src/system/libroot/posix/string/strchrnul.c +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright 2003-2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved. - * Distributed under the terms of the MIT License. - */ - - -#include -#include - - -char* -strchrnul(const char* string, int c) -{ - while (string[0] != (char)c && string[0]) - string++; - - return (char*)string; -} diff --git a/src/system/libroot/posix/string/strcspn.c b/src/system/libroot/posix/string/strcspn.c deleted file mode 100644 index c052485b80..0000000000 --- a/src/system/libroot/posix/string/strcspn.c +++ /dev/null @@ -1,64 +0,0 @@ -/*- - * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Chris Torek. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - - -/* - * Span the complement of string s2. - */ - -size_t -strcspn(const char *s1, const char *s2) -{ - const char *p, *spanp; - char c, sc; - - /* - * Stop as soon as we find any character from s2. Note that there - * must be a NUL in s2; it suffices to stop when we find that, too. - */ - for (p = s1;;) { - c = *p++; - spanp = s2; - do { - if ((sc = *spanp++) == c) - return (p - 1 - s1); - } while (sc != 0); - } - /* NOTREACHED */ -} - diff --git a/src/system/runtime_loader/Jamfile b/src/system/runtime_loader/Jamfile index 372e9e3988..26907b76a0 100644 --- a/src/system/runtime_loader/Jamfile +++ b/src/system/runtime_loader/Jamfile @@ -54,6 +54,9 @@ for architectureObject in [ MultiArchSubDirSetup ] { ctype_loc.o LocaleData.o + strchrnul.o + strcspn.o + memchr.o memcmp.o memmove.o @@ -62,7 +65,6 @@ for architectureObject in [ MultiArchSubDirSetup ] { strchr.o strcmp.o strcpy.o - strcspn.o strdup.o strerror.o strlcat.o