libroot: Replace some more string functions with musl's.

This commit is contained in:
Augustin Cavalier
2025-02-24 15:18:44 -05:00
parent ce7c098db2
commit 979d83f98b
23 changed files with 136 additions and 246 deletions
@@ -8,7 +8,6 @@ UseHeaders [ FDirName $(HAIKU_TOP) src add-ons kernel network ppp shared
libkernelppp headers ] : true ;
StaticLibrary libppp.a :
strlcat.c
driver_settings.cpp
settings_tools.cpp
KPPPUtils.cpp
@@ -20,8 +19,6 @@ StaticLibrary libppp.a :
: libnetwork.so libbnetapi.so
;
SEARCH on [ FGristFiles strlcat.c ] = [ FDirName $(HAIKU_TOP) src system
libroot posix string ] ;
SEARCH on [ FGristFiles driver_settings.cpp ] = [ FDirName $(HAIKU_TOP) src
system libroot os ] ;
SEARCH on [ FGristFiles settings_tools.cpp ] = [ FDirName $(HAIKU_TOP) src
+3 -7
View File
@@ -41,10 +41,6 @@ if $(HOST_PLATFORM) != darwin && $(HOST_PLATFORM) != haiku_host {
strlSources = strlcpy.c strlcat.c ;
}
if $(HOST_PLATFORM) = mingw {
strlSources += stpcpy.c strcasestr.c ;
}
local hostPlatformSources ;
if $(HOST_PLATFORM) = freebsd {
hostPlatformSources = fs_freebsd.cpp ;
@@ -72,7 +68,7 @@ local librootSources =
driver_settings.cpp
$(strlSources)
strnlen.cpp
strnlen.c
KMessage.cpp
;
@@ -108,8 +104,8 @@ BuildPlatformStaticLibraryPIC libroot_build_function_remapper.a :
SEARCH on [ FGristFiles driver_settings.cpp ]
= [ FDirName $(HAIKU_TOP) src system libroot os ] ;
SEARCH on [ FGristFiles $(strlSources) strnlen.cpp ]
= [ FDirName $(HAIKU_TOP) src system libroot posix string ] ;
SEARCH on [ FGristFiles $(strlSources) strnlen.c ]
= [ FDirName $(HAIKU_TOP) src system libroot posix musl string ] ;
SEARCH on [ FGristFiles SHA256.cpp ]
= [ FDirName $(HAIKU_TOP) src system libroot posix crypt ] ;
SEARCH on [ FGristFiles KMessage.cpp ]
+2 -2
View File
@@ -318,8 +318,8 @@ for platform in [ MultiBootSubDirSetup ] {
memmove.c
strdup.cpp
strndup.cpp
strlen.cpp
strnlen.cpp
strlen.c
strnlen.c
strcmp.c
strcasecmp.c
strncmp.c
+2 -2
View File
@@ -135,12 +135,12 @@ KernelMergeObject kernel_lib_posix.o :
strerror.c
strlcat.c
strlcpy.c
strlen.cpp
strlen.c
strncat.c
strncmp.c
strncpy.cpp
strndup.cpp
strnlen.cpp
strnlen.c
strpbrk.c
strrchr.c
strspn.c
@@ -10,13 +10,20 @@ for architectureObject in [ MultiArchSubDirSetup ] {
MergeObject <$(architecture)>posix_musl_string.o :
memccpy.c
memchr.c
memmem.c
memmove.c
memrchr.c
stpcpy.c
stpncpy.c
strcat.c
strchrnul.c
strcspn.c
strlcat.c
strlcpy.c
strlen.c
strncat.c
strnlen.c
strpbrk.c
strspn.c
strstr.c
@@ -0,0 +1,27 @@
#include <string.h>
#include <stdint.h>
#include <limits.h>
#define SS (sizeof(size_t))
#define ALIGN (sizeof(size_t)-1)
#define ONES ((size_t)-1/UCHAR_MAX)
#define HIGHS (ONES * (UCHAR_MAX/2+1))
#define HASZERO(x) (((x)-ONES) & ~(x) & HIGHS)
void *memchr(const void *src, int c, size_t n)
{
const unsigned char *s = src;
c = (unsigned char)c;
#if defined(__GNUC__) && __GNUC__ >= 4
for (; ((uintptr_t)s & ALIGN) && n && *s != c; s++, n--);
if (n && *s != c) {
typedef size_t __attribute__((__may_alias__)) word;
const word *w;
size_t k = ONES * c;
for (w = (const void *)s; n>=SS && !HASZERO(*w^k); w++, n-=SS);
s = (const void *)w;
}
#endif
for (; n && *s != c; s++, n--);
return n ? (void *)s : 0;
}
@@ -0,0 +1,7 @@
#include <string.h>
char *strcat(char *restrict dest, const char *restrict src)
{
strcpy(dest + strlen(dest), src);
return dest;
}
@@ -0,0 +1,9 @@
#define _BSD_SOURCE
#include <string.h>
size_t strlcat(char *d, const char *s, size_t n)
{
size_t l = strnlen(d, n);
if (l == n) return l + strlen(s);
return l + strlcpy(d+l, s, n-l);
}
@@ -0,0 +1,34 @@
#define _BSD_SOURCE
#include <string.h>
#include <stdint.h>
#include <limits.h>
#define ALIGN (sizeof(size_t)-1)
#define ONES ((size_t)-1/UCHAR_MAX)
#define HIGHS (ONES * (UCHAR_MAX/2+1))
#define HASZERO(x) (((x)-ONES) & ~(x) & HIGHS)
size_t strlcpy(char *d, const char *s, size_t n)
{
char *d0 = d;
size_t *wd;
if (!n--) goto finish;
#if defined(__GNUC__) && __GNUC__ >= 4
typedef size_t __attribute__((__may_alias__)) word;
const word *ws;
if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) {
for (; ((uintptr_t)s & ALIGN) && n && (*d=*s); n--, s++, d++);
if (n && *s) {
wd=(void *)d; ws=(const void *)s;
for (; n>=sizeof(size_t) && !HASZERO(*ws);
n-=sizeof(size_t), ws++, wd++) *wd = *ws;
d=(void *)wd; s=(const void *)ws;
}
}
#endif
for (; n && (*d=*s); n--, s++, d++);
*d = 0;
finish:
return d-d0 + strlen(s);
}
@@ -0,0 +1,22 @@
#include <string.h>
#include <stdint.h>
#include <limits.h>
#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)
size_t strlen(const char *s)
{
const char *a = s;
#if defined(__GNUC__) && __GNUC__ >= 4
typedef size_t __attribute__((__may_alias__)) word;
const word *w;
for (; (uintptr_t)s % ALIGN; s++) if (!*s) return s-a;
for (w = (const void *)s; !HASZERO(*w); w++);
s = (const void *)w;
#endif
for (; *s; s++);
return s-a;
}
@@ -0,0 +1,10 @@
#include <string.h>
char *strncat(char *restrict d, const char *restrict s, size_t n)
{
char *a = d;
d += strlen(d);
while (n && *s) n--, *d++ = *s++;
*d++ = 0;
return a;
}
@@ -0,0 +1,7 @@
#include <string.h>
size_t strnlen(const char *s, size_t n)
{
const char *p = memchr(s, 0, n);
return p ? (size_t)(p-s) : n;
}
-7
View File
@@ -20,26 +20,19 @@ for architectureObject in [ MultiArchSubDirSetup ] {
bcmp.c
bcopy.c
bzero.c
memchr.c
memcmp.c
strcasecmp.c
strcasestr.c
strcat.c
strchr.c
strcmp.c
strcoll.cpp
strcpy.c
strdup.cpp
strerror.c
strlcat.c
strlcpy.c
strlen.cpp
strlwr.c
strncat.c
strncmp.c
strncpy.cpp
strndup.cpp
strnlen.cpp
strrchr.c
strtok.c
strupr.c
-24
View File
@@ -1,24 +0,0 @@
/*
** Copyright 2001, Manuel J. Petit. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <sys/types.h>
#include <string.h>
void *
memchr(void const *buf, int c, size_t len)
{
unsigned char const *b = buf;
unsigned char x = (c&0xff);
size_t i;
for (i = 0; i < len; i++) {
if (b[i] == x)
return (void*)(b + i);
}
return NULL;
}
-22
View File
@@ -1,22 +0,0 @@
/*
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <sys/types.h>
#include <string.h>
char *
strcat(char *dest, char const*src)
{
char *tmp = dest;
while(*dest)
dest++;
while((*dest++ = *src++) != '\0')
;
return tmp;
}
-38
View File
@@ -1,38 +0,0 @@
/*
** Copyright 2002, Manuel J. Petit. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <sys/types.h>
#include <string.h>
/** Concatenates the source string to the destination, writes
* as much as "maxLength" bytes to the dest string.
* Always null terminates the string as long as maxLength is
* larger than the dest string.
* Returns the length of the string that it tried to create
* to be able to easily detect string truncation.
*/
size_t
strlcat(char *dest, const char *source, size_t maxLength)
{
size_t destLength = strnlen(dest, maxLength), i;
// This returns the wrong size, but it's all we can do
if (maxLength == destLength)
return destLength + strlen(source);
dest += destLength;
maxLength -= destLength;
for (i = 0; i < maxLength - 1 && source[i]; i++) {
dest[i] = source[i];
}
dest[i] = '\0';
return destLength + i + strlen(source + i);
}
-25
View File
@@ -1,25 +0,0 @@
/*
** Copyright 2002, Manuel J. Petit. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <sys/types.h>
#include <string.h>
size_t
strlcpy(char *dst, char const *src, size_t s)
{
size_t i= 0;
if (!s)
return strlen(src);
for (i = 0; ((i < s - 1) && src[i]); i++) {
dst[i] = src[i];
}
dst[i] = 0;
return i + strlen(src + i);
}
@@ -1,38 +0,0 @@
/*
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <string.h>
#include <SupportDefs.h>
/* From Bit twiddling hacks:
http://graphics.stanford.edu/~seander/bithacks.html */
#define LACKS_ZERO_BYTE(value) \
(((value - 0x01010101) & ~value & 0x80808080) == 0)
size_t
strlen(const char* string)
{
size_t length = 0;
/* Align access for four byte reads */
for (; (((addr_t)string + length) & 3) != 0; length++) {
if (string[length] == '\0')
return length;
}
/* Check four bytes for zero char */
uint32* valuePointer = (uint32*)(string + length);
for (; LACKS_ZERO_BYTE(*valuePointer); valuePointer++)
;
/* Find the exact length */
for (length = ((char*)valuePointer) - string; string[length] != '\0';
length++)
;
return length;
}
-28
View File
@@ -1,28 +0,0 @@
/*
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <sys/types.h>
#include <string.h>
char *
strncat(char *dest, char const *src, size_t count)
{
char *tmp = dest;
if (count > 0) {
while (*dest)
dest++;
while ((*dest++ = *src++)) {
if (--count == 0) {
*dest = '\0';
break;
}
}
}
return tmp;
}
@@ -1,39 +0,0 @@
/*
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <string.h>
#include <SupportDefs.h>
/* From Bit twiddling hacks:
http://graphics.stanford.edu/~seander/bithacks.html */
#define LACKS_ZERO_BYTE(value) \
(((value - 0x01010101) & ~value & 0x80808080) == 0)
size_t
strnlen(const char* string, size_t count)
{
size_t length = 0;
/* Align access for four byte reads */
for (; (((addr_t)string + length) & 3) != 0; length++) {
if (length == count || string[length] == '\0')
return length;
}
/* Check four bytes for zero char */
const uint32* kMaxScanPosition = (uint32*)(string + count - 4);
uint32* valuePointer = (uint32*)(string + length);
for (; valuePointer <= kMaxScanPosition && LACKS_ZERO_BYTE(*valuePointer);
valuePointer++)
;
/* Find the exact length */
for (length = ((char*)valuePointer) - string; length < count
&& string[length] != '\0'; length++)
;
return length;
}
+6 -6
View File
@@ -52,27 +52,27 @@ for architectureObject in [ MultiArchSubDirSetup ] {
<src!system!libroot!posix!locale!$(architecture)>ctype_loc.o
<src!system!libroot!posix!locale!$(architecture)>LocaleData.o
<src!system!libroot!posix!musl!string!$(architecture)>memchr.o
<src!system!libroot!posix!musl!string!$(architecture)>memmove.o
<src!system!libroot!posix!musl!string!$(architecture)>strcat.o
<src!system!libroot!posix!musl!string!$(architecture)>strchrnul.o
<src!system!libroot!posix!musl!string!$(architecture)>strcspn.o
<src!system!libroot!posix!musl!string!$(architecture)>strlcat.o
<src!system!libroot!posix!musl!string!$(architecture)>strlcpy.o
<src!system!libroot!posix!musl!string!$(architecture)>strlen.o
<src!system!libroot!posix!musl!string!$(architecture)>strnlen.o
<src!system!libroot!posix!musl!string!$(architecture)>strpbrk.o
<src!system!libroot!posix!musl!string!$(architecture)>strspn.o
<src!system!libroot!posix!musl!string!$(architecture)>strstr.o
<src!system!libroot!posix!string!$(architecture)>memchr.o
<src!system!libroot!posix!string!$(architecture)>memcmp.o
<src!system!libroot!posix!string!$(architecture)>strcasecmp.o
<src!system!libroot!posix!string!$(architecture)>strcat.o
<src!system!libroot!posix!string!$(architecture)>strchr.o
<src!system!libroot!posix!string!$(architecture)>strcmp.o
<src!system!libroot!posix!string!$(architecture)>strcpy.o
<src!system!libroot!posix!string!$(architecture)>strdup.o
<src!system!libroot!posix!string!$(architecture)>strerror.o
<src!system!libroot!posix!string!$(architecture)>strlcat.o
<src!system!libroot!posix!string!$(architecture)>strlcpy.o
<src!system!libroot!posix!string!$(architecture)>strlen.o
<src!system!libroot!posix!string!$(architecture)>strncmp.o
<src!system!libroot!posix!string!$(architecture)>strnlen.o
<src!system!libroot!posix!string!$(architecture)>strrchr.o
;
@@ -25,9 +25,7 @@ StaticLibrary <userland>libkernelppp.a :
_KPPPPFCHandler.cpp
# for driver_settings
strlcat.c
driver_settings.cpp
;
SEARCH on [ FGristFiles strlcat.c ] = [ FDirName $(HAIKU_TOP) src system libroot posix string ] ;
SEARCH on [ FGristFiles driver_settings.cpp ] = [ FDirName $(HAIKU_TOP) src system libroot os ] ;
-3
View File
@@ -131,9 +131,6 @@ SEARCH on [ FGristFiles list.cpp ring_buffer.cpp ]
SEARCH on [ FGristFiles KMessage.cpp ]
= [ FDirName $(HAIKU_TOP) src system kernel messaging ] ;
SEARCH on [ FGristFiles strlcat.c strlcpy.c ]
= [ FDirName $(HAIKU_TOP) src system libroot posix string ] ;
SEARCH on [ FGristFiles driver_settings.cpp ]
= [ FDirName $(HAIKU_TOP) src system libroot os ] ;