[PATCH] libc: revert some files with "#undef" to point back to musl

21 views
Skip to first unread message

Waldemar Kozaczuk

unread,
Jul 27, 2020, 12:43:56 PM7/27/20
to osv...@googlegroups.com, Waldemar Kozaczuk
Apparently some of the network and string functions are implemented
using macros in glibc and it seems that OSv used to depend on
the glibc headers (maybe) and the commit 0ac3dd155ac0a8f4606fcb9abf3c7785da395214 by Avi Kivity
modified those files to #undef those functions.

It looks like we do not have to do it anymore so this patch
simply removes those files from libc/ folder and changes
Makefile to point back to the musl equivalent ones.

Signed-off-by: Waldemar Kozaczuk <jwkoz...@gmail.com>
---
Makefile | 26 +++++++++++++-------------
libc/network/htonl.c | 9 ---------
libc/network/htons.c | 9 ---------
libc/network/ntohl.c | 9 ---------
libc/network/ntohs.c | 9 ---------
libc/string/strchr.c | 10 ----------
libc/string/strcmp.c | 8 --------
libc/string/strcspn.c | 20 --------------------
libc/string/strdup.c | 14 --------------
libc/string/strncmp.c | 10 ----------
libc/string/strpbrk.c | 8 --------
libc/string/strsep.c | 14 --------------
libc/string/strspn.c | 21 ---------------------
13 files changed, 13 insertions(+), 154 deletions(-)
delete mode 100644 libc/network/htonl.c
delete mode 100644 libc/network/htons.c
delete mode 100644 libc/network/ntohl.c
delete mode 100644 libc/network/ntohs.c
delete mode 100644 libc/string/strchr.c
delete mode 100644 libc/string/strcmp.c
delete mode 100644 libc/string/strcspn.c
delete mode 100644 libc/string/strdup.c
delete mode 100644 libc/string/strncmp.c
delete mode 100644 libc/string/strpbrk.c
delete mode 100644 libc/string/strsep.c
delete mode 100644 libc/string/strspn.c

diff --git a/Makefile b/Makefile
index 0c0f665b..28f4f575 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-# OSv makefile
+# O./libc/string/strdup.cSv makefile
#
# Copyright (C) 2015 Cloudius Systems, Ltd.
# This work is open source software, licensed under the terms of the
@@ -1344,10 +1344,10 @@ musl += multibyte/wctomb.o

$(out)/libc/multibyte/mbsrtowcs.o: CFLAGS += -Imusl/src/multibyte

-libc += network/htonl.o
-libc += network/htons.o
-libc += network/ntohl.o
-libc += network/ntohs.o
+musl += network/htonl.o
+musl += network/htons.o
+musl += network/ntohl.o
+musl += network/ntohs.o
libc += network/gethostbyname_r.o
musl += network/gethostbyname2_r.o
musl += network/gethostbyaddr_r.o
@@ -1576,13 +1576,13 @@ musl += string/strcasecmp.o
musl += string/strcasestr.o
libc += string/strcat.o
libc += string/__strcat_chk.o
-libc += string/strchr.o
+musl += string/strchr.o
libc += string/strchrnul.o
-libc += string/strcmp.o
+musl += string/strcmp.o
libc += string/strcpy.o
libc += string/__strcpy_chk.o
-libc += string/strcspn.o
-libc += string/strdup.o
+musl += string/strcspn.o
+musl += string/strdup.o
libc += string/strerror_r.o
libc += string/strlcat.o
libc += string/strlcpy.o
@@ -1590,18 +1590,18 @@ libc += string/strlen.o
musl += string/strncasecmp.o
libc += string/strncat.o
libc += string/__strncat_chk.o
-libc += string/strncmp.o
+musl += string/strncmp.o
libc += string/strncpy.o
libc += string/__strncpy_chk.o
libc += string/__strndup.o
musl += string/strndup.o
musl += string/strnlen.o
-libc += string/strpbrk.o
+musl += string/strpbrk.o
musl += string/strrchr.o
-libc += string/strsep.o
+musl += string/strsep.o
libc += string/stresep.o
libc += string/strsignal.o
-libc += string/strspn.o
+musl += string/strspn.o
musl += string/strstr.o
libc += string/strtok.o
libc += string/strtok_r.o
diff --git a/libc/network/htonl.c b/libc/network/htonl.c
deleted file mode 100644
index cd8fb684..00000000
--- a/libc/network/htonl.c
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <netinet/in.h>
-#include <byteswap.h>
-
-#undef htonl
-uint32_t htonl(uint32_t n)
-{
- union { int i; char c; } u = { 1 };
- return u.c ? bswap_32(n) : n;
-}
diff --git a/libc/network/htons.c b/libc/network/htons.c
deleted file mode 100644
index 99a064b0..00000000
--- a/libc/network/htons.c
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <netinet/in.h>
-#include <byteswap.h>
-
-#undef htons
-uint16_t htons(uint16_t n)
-{
- union { int i; char c; } u = { 1 };
- return u.c ? bswap_16(n) : n;
-}
diff --git a/libc/network/ntohl.c b/libc/network/ntohl.c
deleted file mode 100644
index 0e5b4ea8..00000000
--- a/libc/network/ntohl.c
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <netinet/in.h>
-#include <byteswap.h>
-
-#undef ntohl
-uint32_t ntohl(uint32_t n)
-{
- union { int i; char c; } u = { 1 };
- return u.c ? bswap_32(n) : n;
-}
diff --git a/libc/network/ntohs.c b/libc/network/ntohs.c
deleted file mode 100644
index 745fe737..00000000
--- a/libc/network/ntohs.c
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <netinet/in.h>
-#include <byteswap.h>
-
-#undef ntohs
-uint16_t ntohs(uint16_t n)
-{
- union { int i; char c; } u = { 1 };
- return u.c ? bswap_16(n) : n;
-}
diff --git a/libc/string/strchr.c b/libc/string/strchr.c
deleted file mode 100644
index fde21673..00000000
--- a/libc/string/strchr.c
+++ /dev/null
@@ -1,10 +0,0 @@
-#include <string.h>
-
-char *__strchrnul(const char *, int);
-
-#undef strchr
-char *strchr(const char *s, int c)
-{
- char *r = __strchrnul(s, c);
- return *(unsigned char *)r == (unsigned char)c ? r : 0;
-}
diff --git a/libc/string/strcmp.c b/libc/string/strcmp.c
deleted file mode 100644
index a4a4b623..00000000
--- a/libc/string/strcmp.c
+++ /dev/null
@@ -1,8 +0,0 @@
-#include <string.h>
-
-#undef strcmp
-int strcmp(const char *l, const char *r)
-{
- for (; *l==*r && *l && *r; l++, r++);
- return *(unsigned char *)l - *(unsigned char *)r;
-}
diff --git a/libc/string/strcspn.c b/libc/string/strcspn.c
deleted file mode 100644
index bcb9dc77..00000000
--- a/libc/string/strcspn.c
+++ /dev/null
@@ -1,20 +0,0 @@
-#include <string.h>
-
-#define BITOP(a,b,op) \
- ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
-
-char *__strchrnul(const char *, int);
-
-#undef strcspn
-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/libc/string/strdup.c b/libc/string/strdup.c
deleted file mode 100644
index ca6a36c7..00000000
--- a/libc/string/strdup.c
+++ /dev/null
@@ -1,14 +0,0 @@
-#include <stdlib.h>
-#include <string.h>
-#include "libc.h"
-
-#undef __strdup
-char *__strdup(const char *s)
-{
- size_t l = strlen(s);
- char *d = malloc(l+1);
- if (!d) return NULL;
- return memcpy(d, s, l+1);
-}
-
-weak_alias(__strdup, strdup);
diff --git a/libc/string/strncmp.c b/libc/string/strncmp.c
deleted file mode 100644
index f1de1539..00000000
--- a/libc/string/strncmp.c
+++ /dev/null
@@ -1,10 +0,0 @@
-#include <string.h>
-
-#undef strncmp
-int strncmp(const char *_l, const char *_r, size_t n)
-{
- const unsigned char *l=(void *)_l, *r=(void *)_r;
- if (!n--) return 0;
- for (; *l && *r && n && *l == *r ; l++, r++, n--);
- return *l - *r;
-}
diff --git a/libc/string/strpbrk.c b/libc/string/strpbrk.c
deleted file mode 100644
index b194ff1d..00000000
--- a/libc/string/strpbrk.c
+++ /dev/null
@@ -1,8 +0,0 @@
-#include <string.h>
-
-#undef strpbrk
-char *strpbrk(const char *s, const char *b)
-{
- s += strcspn(s, b);
- return *s ? (char *)s : 0;
-}
diff --git a/libc/string/strsep.c b/libc/string/strsep.c
deleted file mode 100644
index b64557c4..00000000
--- a/libc/string/strsep.c
+++ /dev/null
@@ -1,14 +0,0 @@
-#define _GNU_SOURCE
-#include <string.h>
-
-#undef strsep
-char *strsep(char **str, const char *sep)
-{
- char *s = *str, *end;
- if (!s) return NULL;
- end = s + strcspn(s, sep);
- if (*end) *end++ = 0;
- else end = 0;
- *str = end;
- return s;
-}
diff --git a/libc/string/strspn.c b/libc/string/strspn.c
deleted file mode 100644
index 10d2c9f0..00000000
--- a/libc/string/strspn.c
+++ /dev/null
@@ -1,21 +0,0 @@
-#include <string.h>
-
-#define BITOP(a,b,op) \
- ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
-
-#undef strspn
-size_t strspn(const char *s, const char *c)
-{
- const char *a = s;
- size_t byteset[32/sizeof(size_t)] = { 0 };
-
- if (!c[0]) return 0;
- if (!c[1]) {
- for (; *s == *c; s++);
- return s-a;
- }
-
- for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++);
- for (; *s && BITOP(byteset, *(unsigned char *)s, &); s++);
- return s-a;
-}
--
2.25.1

Waldemar Kozaczuk

unread,
Jul 27, 2020, 12:44:02 PM7/27/20
to osv...@googlegroups.com, Waldemar Kozaczuk
At some point in the beginning of OSv the musl sources were copied
to libc/ folder and then gradually removed in favor of the original
copies in musl/src/ folder.

This patch removes another 15 files from libc/string/
which only differred by "__reserved" vs "restrict" keyword.
The original musl sources have "restrict" keyword and their
corresponding copies have "__reserved" keyword even though
per this commit log 5cd0168fc566e5f7263b04948b4df6695d8ae721
they seem to have been copied "as is" from musl.

Based on what I have researched the __restrict keyword
is a GCC extension and predates "restrict" and it happens
to have same effect on pointer optimization as the latter one.
We also compile all C source files with "-std=gnu99" option
which adds GCC extensions to c99. So all in all I think
we can drop the difference and simply point to the original
musl sources.

This patch further eliminates unnecessary files in libc/
folder.

Signed-off-by: Waldemar Kozaczuk <jwkoz...@gmail.com>
---
Makefile | 30 ++++++------
libc/string/strcat.c | 7 ---
libc/string/strcpy.c | 16 -------
libc/string/strncat.c | 10 ----
libc/string/strncpy.c | 9 ----
libc/string/strtok.c | 13 -----
libc/string/swab.c | 13 -----
libc/string/wcpcpy.c | 6 ---
libc/string/wcpncpy.c | 6 ---
libc/string/wcscat.c | 7 ---
libc/string/wcscpy.c | 8 ----
libc/string/wcsncat.c | 10 ----
libc/string/wcsncpy.c | 9 ----
libc/string/wcsstr.c | 108 ------------------------------------------
libc/string/wcstok.c | 12 -----
libc/string/wmemcpy.c | 9 ----
16 files changed, 15 insertions(+), 258 deletions(-)
delete mode 100644 libc/string/strcat.c
delete mode 100644 libc/string/strcpy.c
delete mode 100644 libc/string/strncat.c
delete mode 100644 libc/string/strncpy.c
delete mode 100644 libc/string/strtok.c
delete mode 100644 libc/string/swab.c
delete mode 100644 libc/string/wcpcpy.c
delete mode 100644 libc/string/wcpncpy.c
delete mode 100644 libc/string/wcscat.c
delete mode 100644 libc/string/wcscpy.c
delete mode 100644 libc/string/wcsncat.c
delete mode 100644 libc/string/wcsncpy.c
delete mode 100644 libc/string/wcsstr.c
delete mode 100644 libc/string/wcstok.c
delete mode 100644 libc/string/wmemcpy.c

diff --git a/Makefile b/Makefile
index 28f4f575..7c7c0489 100644
--- a/Makefile
+++ b/Makefile
@@ -1574,12 +1574,12 @@ libc += string/__stpcpy_chk.o
libc += string/stpncpy.o
musl += string/strcasecmp.o
musl += string/strcasestr.o
-libc += string/strcat.o
+musl += string/strcat.o
libc += string/__strcat_chk.o
musl += string/strchr.o
libc += string/strchrnul.o
musl += string/strcmp.o
-libc += string/strcpy.o
+musl += string/strcpy.o
libc += string/__strcpy_chk.o
musl += string/strcspn.o
musl += string/strdup.o
@@ -1588,10 +1588,10 @@ libc += string/strlcat.o
libc += string/strlcpy.o
libc += string/strlen.o
musl += string/strncasecmp.o
-libc += string/strncat.o
+musl += string/strncat.o
libc += string/__strncat_chk.o
musl += string/strncmp.o
-libc += string/strncpy.o
+musl += string/strncpy.o
libc += string/__strncpy_chk.o
libc += string/__strndup.o
musl += string/strndup.o
@@ -1603,37 +1603,37 @@ libc += string/stresep.o
libc += string/strsignal.o
musl += string/strspn.o
musl += string/strstr.o
-libc += string/strtok.o
+musl += string/strtok.o
libc += string/strtok_r.o
musl += string/strverscmp.o
-libc += string/swab.o
-libc += string/wcpcpy.o
-libc += string/wcpncpy.o
+musl += string/swab.o
+musl += string/wcpcpy.o
+musl += string/wcpncpy.o
musl += string/wcscasecmp.o
musl += string/wcscasecmp_l.o
-libc += string/wcscat.o
+musl += string/wcscat.o
musl += string/wcschr.o
musl += string/wcscmp.o
-libc += string/wcscpy.o
+musl += string/wcscpy.o
libc += string/__wcscpy_chk.o
musl += string/wcscspn.o
musl += string/wcsdup.o
musl += string/wcslen.o
musl += string/wcsncasecmp.o
musl += string/wcsncasecmp_l.o
-libc += string/wcsncat.o
+musl += string/wcsncat.o
musl += string/wcsncmp.o
-libc += string/wcsncpy.o
+musl += string/wcsncpy.o
musl += string/wcsnlen.o
musl += string/wcspbrk.o
musl += string/wcsrchr.o
musl += string/wcsspn.o
-libc += string/wcsstr.o
-libc += string/wcstok.o
+musl += string/wcsstr.o
+musl += string/wcstok.o
musl += string/wcswcs.o
musl += string/wmemchr.o
musl += string/wmemcmp.o
-libc += string/wmemcpy.o
+musl += string/wmemcpy.o
musl += string/wmemmove.o
musl += string/wmemset.o

diff --git a/libc/string/strcat.c b/libc/string/strcat.c
deleted file mode 100644
index 43b7e254..00000000
--- a/libc/string/strcat.c
+++ /dev/null
@@ -1,7 +0,0 @@
-#include <string.h>
-
-char *strcat(char *__restrict dest, const char *__restrict src)
-{
- strcpy(dest + strlen(dest), src);
- return dest;
-}
diff --git a/libc/string/strcpy.c b/libc/string/strcpy.c
deleted file mode 100644
index 8488f6ee..00000000
--- a/libc/string/strcpy.c
+++ /dev/null
@@ -1,16 +0,0 @@
-#include <string.h>
-
-char *__stpcpy(char *, const char *);
-
-char *strcpy(char *__restrict dest, const char *__restrict src)
-{
-#if 1
- __stpcpy(dest, src);
- return dest;
-#else
- const unsigned char *s = src;
- unsigned char *d = dest;
- while ((*d++ = *s++));
- return dest;
-#endif
-}
diff --git a/libc/string/strncat.c b/libc/string/strncat.c
deleted file mode 100644
index 6f8d8da8..00000000
--- a/libc/string/strncat.c
+++ /dev/null
@@ -1,10 +0,0 @@
-#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;
-}
diff --git a/libc/string/strncpy.c b/libc/string/strncpy.c
deleted file mode 100644
index f43d4d73..00000000
--- a/libc/string/strncpy.c
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <string.h>
-
-char *__stpncpy(char *, const char *, size_t);
-
-char *strncpy(char *__restrict d, const char *__restrict s, size_t n)
-{
- __stpncpy(d, s, n);
- return d;
-}
diff --git a/libc/string/strtok.c b/libc/string/strtok.c
deleted file mode 100644
index e2205c09..00000000
--- a/libc/string/strtok.c
+++ /dev/null
@@ -1,13 +0,0 @@
-#include <string.h>
-
-char *strtok(char *__restrict s, const char *__restrict sep)
-{
- static char *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/libc/string/swab.c b/libc/string/swab.c
deleted file mode 100644
index a4c9286d..00000000
--- a/libc/string/swab.c
+++ /dev/null
@@ -1,13 +0,0 @@
-#include <unistd.h>
-
-void swab(const void *__restrict _src, void *__restrict _dest, ssize_t n)
-{
- const char *src = _src;
- char *dest = _dest;
- for (; n>0; n-=2) {
- dest[0] = src[1];
- dest[1] = src[0];
- dest += 2;
- src += 2;
- }
-}
diff --git a/libc/string/wcpcpy.c b/libc/string/wcpcpy.c
deleted file mode 100644
index b1c20284..00000000
--- a/libc/string/wcpcpy.c
+++ /dev/null
@@ -1,6 +0,0 @@
-#include <wchar.h>
-
-wchar_t *wcpcpy(wchar_t *__restrict d, const wchar_t *__restrict s)
-{
- return wcscpy(d, s) + wcslen(s);
-}
diff --git a/libc/string/wcpncpy.c b/libc/string/wcpncpy.c
deleted file mode 100644
index f6d0add0..00000000
--- a/libc/string/wcpncpy.c
+++ /dev/null
@@ -1,6 +0,0 @@
-#include <wchar.h>
-
-wchar_t *wcpncpy(wchar_t *__restrict d, const wchar_t *__restrict s, size_t n)
-{
- return wcsncpy(d, s, n) + wcsnlen(s, n);
-}
diff --git a/libc/string/wcscat.c b/libc/string/wcscat.c
deleted file mode 100644
index 1c907db4..00000000
--- a/libc/string/wcscat.c
+++ /dev/null
@@ -1,7 +0,0 @@
-#include <wchar.h>
-
-wchar_t *wcscat(wchar_t *__restrict dest, const wchar_t *__restrict src)
-{
- wcscpy(dest + wcslen(dest), src);
- return dest;
-}
diff --git a/libc/string/wcscpy.c b/libc/string/wcscpy.c
deleted file mode 100644
index b09443a2..00000000
--- a/libc/string/wcscpy.c
+++ /dev/null
@@ -1,8 +0,0 @@
-#include <wchar.h>
-
-wchar_t *wcscpy(wchar_t *__restrict d, const wchar_t *__restrict s)
-{
- wchar_t *a = d;
- while ((*d++ = *s++));
- return a;
-}
diff --git a/libc/string/wcsncat.c b/libc/string/wcsncat.c
deleted file mode 100644
index 1ca0288d..00000000
--- a/libc/string/wcsncat.c
+++ /dev/null
@@ -1,10 +0,0 @@
-#include <wchar.h>
-
-wchar_t *wcsncat(wchar_t *__restrict d, const wchar_t *__restrict s, size_t n)
-{
- wchar_t *a = d;
- d += wcslen(d);
- while (n && *s) n--, *d++ = *s++;
- *d++ = 0;
- return a;
-}
diff --git a/libc/string/wcsncpy.c b/libc/string/wcsncpy.c
deleted file mode 100644
index 55903637..00000000
--- a/libc/string/wcsncpy.c
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <wchar.h>
-
-wchar_t *wcsncpy(wchar_t *__restrict d, const wchar_t *__restrict s, size_t n)
-{
- wchar_t *a = d;
- while (n && *s) n--, *d++ = *s++;
- wmemset(d, 0, n);
- return a;
-}
diff --git a/libc/string/wcsstr.c b/libc/string/wcsstr.c
deleted file mode 100644
index e0e8dffb..00000000
--- a/libc/string/wcsstr.c
+++ /dev/null
@@ -1,108 +0,0 @@
-#include <wchar.h>
-#include <string.h>
-#include <stdlib.h>
-#include <stdint.h>
-
-#define MAX(a,b) ((a)>(b)?(a):(b))
-#define MIN(a,b) ((a)<(b)?(a):(b))
-
-static wchar_t *twoway_wcsstr(const wchar_t *h, const wchar_t *n)
-{
- const wchar_t *z;
- size_t l, ip, jp, k, p, ms, p0, mem, mem0;
-
- /* Computing length of needle */
- for (l=0; n[l] && h[l]; l++);
- if (n[l]) return 0; /* hit the end of h */
-
- /* Compute maximal suffix */
- ip = -1; jp = 0; k = p = 1;
- while (jp+k<l) {
- if (n[ip+k] == n[jp+k]) {
- if (k == p) {
- jp += p;
- k = 1;
- } else k++;
- } else if (n[ip+k] > n[jp+k]) {
- jp += k;
- k = 1;
- p = jp - ip;
- } else {
- ip = jp++;
- k = p = 1;
- }
- }
- ms = ip;
- p0 = p;
-
- /* And with the opposite comparison */
- ip = -1; jp = 0; k = p = 1;
- while (jp+k<l) {
- if (n[ip+k] == n[jp+k]) {
- if (k == p) {
- jp += p;
- k = 1;
- } else k++;
- } else if (n[ip+k] < n[jp+k]) {
- jp += k;
- k = 1;
- p = jp - ip;
- } else {
- ip = jp++;
- k = p = 1;
- }
- }
- if (ip+1 > ms+1) ms = ip;
- else p = p0;
-
- /* Periodic needle? */
- if (wmemcmp(n, n+p, ms+1)) {
- mem0 = 0;
- p = MAX(ms, l-ms-1) + 1;
- } else mem0 = l-p;
- mem = 0;
-
- /* Initialize incremental end-of-haystack pointer */
- z = h;
-
- /* Search loop */
- for (;;) {
- /* Update incremental end-of-haystack pointer */
- if (z-h < l) {
- /* Fast estimate for MIN(l,63) */
- size_t grow = l | 63;
- const wchar_t *z2 = wmemchr(z, 0, grow);
- if (z2) {
- z = z2;
- if (z-h < l) return 0;
- } else z += grow;
- }
-
- /* Compare right half */
- for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++);
- if (n[k]) {
- h += k-ms;
- mem = 0;
- continue;
- }
- /* Compare left half */
- for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
- if (k == mem) return (wchar_t *)h;
- h += p;
- mem = mem0;
- }
-}
-
-wchar_t *wcsstr(const wchar_t *__restrict h, const wchar_t *__restrict n)
-{
- /* Return immediately on empty needle or haystack */
- if (!n[0]) return (wchar_t *)h;
- if (!h[0]) return 0;
-
- /* Use faster algorithms for short needles */
- h = wcschr(h, *n);
- if (!h || !n[1]) return (wchar_t *)h;
- if (!h[1]) return 0;
-
- return twoway_wcsstr(h, n);
-}
diff --git a/libc/string/wcstok.c b/libc/string/wcstok.c
deleted file mode 100644
index 15b07cd2..00000000
--- a/libc/string/wcstok.c
+++ /dev/null
@@ -1,12 +0,0 @@
-#include <wchar.h>
-
-wchar_t *wcstok(wchar_t *__restrict s, const wchar_t *__restrict sep, wchar_t **__restrict p)
-{
- if (!s && !(s = *p)) return NULL;
- s += wcsspn(s, sep);
- if (!*s) return *p = 0;
- *p = s + wcscspn(s, sep);
- if (**p) *(*p)++ = 0;
- else *p = 0;
- return s;
-}
diff --git a/libc/string/wmemcpy.c b/libc/string/wmemcpy.c
deleted file mode 100644
index 79606cb8..00000000
--- a/libc/string/wmemcpy.c
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <string.h>
-#include <wchar.h>
-
-wchar_t *wmemcpy(wchar_t *__restrict d, const wchar_t *__restrict s, size_t n)
-{
- wchar_t *a = d;
- while (n--) *d++ = *s++;
- return a;
-}
--
2.25.1

Waldemar Kozaczuk

unread,
Jul 27, 2020, 12:48:36 PM7/27/20
to osv...@googlegroups.com, Waldemar Kozaczuk
At some point in the beginning of OSv the musl sources were copied
to libc/ folder and then gradually removed in favor of the original
copies in musl/src/ folder.

This patch removes another 15 files from libc/string/
which only differred by "__restrict" vs "restrict" keyword.
The original musl sources have "restrict" keyword and their
corresponding copies have "__restrict" keyword even though

Commit Bot

unread,
Jul 29, 2020, 4:40:46 AM7/29/20
to osv...@googlegroups.com, Waldemar Kozaczuk
From: Waldemar Kozaczuk <jwkoz...@gmail.com>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

libc: revert some files with "#undef" to point back to musl

Apparently some of the network and string functions are implemented
using macros in glibc and it seems that OSv used to depend on
the glibc headers (maybe) and the commit 0ac3dd155ac0a8f4606fcb9abf3c7785da395214 by Avi Kivity
modified those files to #undef those functions.

It looks like we do not have to do it anymore so this patch
simply removes those files from libc/ folder and changes
Makefile to point back to the musl equivalent ones.

Signed-off-by: Waldemar Kozaczuk <jwkoz...@gmail.com>
Message-Id: <20200727164350.1...@gmail.com>

---
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-# OSv makefile
+# O./libc/string/strdup.cSv makefile
#
# Copyright (C) 2015 Cloudius Systems, Ltd.
# This work is open source software, licensed under the terms of the
@@ -1344,10 +1344,10 @@ musl += multibyte/wctomb.o

$(out)/libc/multibyte/mbsrtowcs.o: CFLAGS += -Imusl/src/multibyte

-libc += network/htonl.o
-libc += network/htons.o
-libc += network/ntohl.o
-libc += network/ntohs.o
+musl += network/htonl.o
+musl += network/htons.o
+musl += network/ntohl.o
+musl += network/ntohs.o
libc += network/gethostbyname_r.o
musl += network/gethostbyname2_r.o
musl += network/gethostbyaddr_r.o
@@ -1576,32 +1576,32 @@ musl += string/strcasecmp.o
musl += string/strcasestr.o
libc += string/strcat.o
libc += string/__strcat_chk.o
-libc += string/strchr.o
+musl += string/strchr.o
libc += string/strchrnul.o
-libc += string/strcmp.o
+musl += string/strcmp.o
libc += string/strcpy.o
libc += string/__strcpy_chk.o
-libc += string/strcspn.o
-libc += string/strdup.o
+musl += string/strcspn.o
+musl += string/strdup.o
libc += string/strerror_r.o
libc += string/strlcat.o
libc += string/strlcpy.o
libc += string/strlen.o
musl += string/strncasecmp.o
libc += string/strncat.o
libc += string/__strncat_chk.o
-libc += string/strncmp.o
+musl += string/strncmp.o
libc += string/strncpy.o
libc += string/__strncpy_chk.o
libc += string/__strndup.o
musl += string/strndup.o
musl += string/strnlen.o
-libc += string/strpbrk.o
+musl += string/strpbrk.o
musl += string/strrchr.o
-libc += string/strsep.o
+musl += string/strsep.o
libc += string/stresep.o
libc += string/strsignal.o
-libc += string/strspn.o
+musl += string/strspn.o
musl += string/strstr.o
libc += string/strtok.o
libc += string/strtok_r.o
diff --git a/libc/network/htonl.c b/libc/network/htonl.c
--- a/libc/network/htonl.c
+++ b/libc/network/htonl.c
@@ -1,9 +0,0 @@
-#include <netinet/in.h>
-#include <byteswap.h>
-
-#undef htonl
-uint32_t htonl(uint32_t n)
-{
- union { int i; char c; } u = { 1 };
- return u.c ? bswap_32(n) : n;
-}
diff --git a/libc/network/htons.c b/libc/network/htons.c
--- a/libc/network/htons.c
+++ b/libc/network/htons.c
@@ -1,9 +0,0 @@
-#include <netinet/in.h>
-#include <byteswap.h>
-
-#undef htons
-uint16_t htons(uint16_t n)
-{
- union { int i; char c; } u = { 1 };
- return u.c ? bswap_16(n) : n;
-}
diff --git a/libc/network/ntohl.c b/libc/network/ntohl.c
--- a/libc/network/ntohl.c
+++ b/libc/network/ntohl.c
@@ -1,9 +0,0 @@
-#include <netinet/in.h>
-#include <byteswap.h>
-
-#undef ntohl
-uint32_t ntohl(uint32_t n)
-{
- union { int i; char c; } u = { 1 };
- return u.c ? bswap_32(n) : n;
-}
diff --git a/libc/network/ntohs.c b/libc/network/ntohs.c
--- a/libc/network/ntohs.c
+++ b/libc/network/ntohs.c
@@ -1,9 +0,0 @@
-#include <netinet/in.h>
-#include <byteswap.h>
-
-#undef ntohs
-uint16_t ntohs(uint16_t n)
-{
- union { int i; char c; } u = { 1 };
- return u.c ? bswap_16(n) : n;
-}
diff --git a/libc/string/strchr.c b/libc/string/strchr.c
--- a/libc/string/strchr.c
+++ b/libc/string/strchr.c
@@ -1,10 +0,0 @@
-#include <string.h>
-
-char *__strchrnul(const char *, int);
-
-#undef strchr
-char *strchr(const char *s, int c)
-{
- char *r = __strchrnul(s, c);
- return *(unsigned char *)r == (unsigned char)c ? r : 0;
-}
diff --git a/libc/string/strcmp.c b/libc/string/strcmp.c
--- a/libc/string/strcmp.c
+++ b/libc/string/strcmp.c
@@ -1,8 +0,0 @@
-#include <string.h>
-
-#undef strcmp
-int strcmp(const char *l, const char *r)
-{
- for (; *l==*r && *l && *r; l++, r++);
- return *(unsigned char *)l - *(unsigned char *)r;
-}
diff --git a/libc/string/strcspn.c b/libc/string/strcspn.c
--- a/libc/string/strcspn.c
+++ b/libc/string/strcspn.c
@@ -1,20 +0,0 @@
-#include <string.h>
-
-#define BITOP(a,b,op) \
- ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
-
-char *__strchrnul(const char *, int);
-
-#undef strcspn
-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/libc/string/strdup.c b/libc/string/strdup.c
--- a/libc/string/strdup.c
+++ b/libc/string/strdup.c
@@ -1,14 +0,0 @@
-#include <stdlib.h>
-#include <string.h>
-#include "libc.h"
-
-#undef __strdup
-char *__strdup(const char *s)
-{
- size_t l = strlen(s);
- char *d = malloc(l+1);
- if (!d) return NULL;
- return memcpy(d, s, l+1);
-}
-
-weak_alias(__strdup, strdup);
diff --git a/libc/string/strncmp.c b/libc/string/strncmp.c
--- a/libc/string/strncmp.c
+++ b/libc/string/strncmp.c
@@ -1,10 +0,0 @@
-#include <string.h>
-
-#undef strncmp
-int strncmp(const char *_l, const char *_r, size_t n)
-{
- const unsigned char *l=(void *)_l, *r=(void *)_r;
- if (!n--) return 0;
- for (; *l && *r && n && *l == *r ; l++, r++, n--);
- return *l - *r;
-}
diff --git a/libc/string/strpbrk.c b/libc/string/strpbrk.c
--- a/libc/string/strpbrk.c
+++ b/libc/string/strpbrk.c
@@ -1,8 +0,0 @@
-#include <string.h>
-
-#undef strpbrk
-char *strpbrk(const char *s, const char *b)
-{
- s += strcspn(s, b);
- return *s ? (char *)s : 0;
-}
diff --git a/libc/string/strsep.c b/libc/string/strsep.c
--- a/libc/string/strsep.c
+++ b/libc/string/strsep.c
@@ -1,14 +0,0 @@
-#define _GNU_SOURCE
-#include <string.h>
-
-#undef strsep
-char *strsep(char **str, const char *sep)
-{
- char *s = *str, *end;
- if (!s) return NULL;
- end = s + strcspn(s, sep);
- if (*end) *end++ = 0;
- else end = 0;
- *str = end;
- return s;
-}
diff --git a/libc/string/strspn.c b/libc/string/strspn.c
--- a/libc/string/strspn.c
+++ b/libc/string/strspn.c

Nadav Har'El

unread,
Jul 29, 2020, 4:41:15 AM7/29/20
to Waldemar Kozaczuk, Osv Dev
Thanks! Committing, despite the weird change in the top line of the makefile, which I see you've undone in the next patch :-)

--
Nadav Har'El
n...@scylladb.com


--
You received this message because you are subscribed to the Google Groups "OSv Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to osv-dev+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/osv-dev/20200727164350.10609-1-jwkozaczuk%40gmail.com.

Commit Bot

unread,
Jul 29, 2020, 4:43:42 AM7/29/20
to osv...@googlegroups.com, Waldemar Kozaczuk
From: Waldemar Kozaczuk <jwkoz...@gmail.com>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

libc: revert some files with "__restrict" to point back to musl

At some point in the beginning of OSv the musl sources were copied
to libc/ folder and then gradually removed in favor of the original
copies in musl/src/ folder.

This patch removes another 15 files from libc/string/
which only differred by "__reserved" vs "restrict" keyword.
The original musl sources have "restrict" keyword and their
corresponding copies have "__reserved" keyword even though
per this commit log 5cd0168fc566e5f7263b04948b4df6695d8ae721
they seem to have been copied "as is" from musl.

Based on what I have researched the __restrict keyword
is a GCC extension and predates "restrict" and it happens
to have same effect on pointer optimization as the latter one.
We also compile all C source files with "-std=gnu99" option
which adds GCC extensions to c99. So all in all I think
we can drop the difference and simply point to the original
musl sources.

This patch further eliminates unnecessary files in libc/
folder.

Signed-off-by: Waldemar Kozaczuk <jwkoz...@gmail.com>
Message-Id: <20200727164350.1...@gmail.com>

---
diff --git a/Makefile b/Makefile
--- a/libc/string/strcat.c
+++ b/libc/string/strcat.c
@@ -1,7 +0,0 @@
-#include <string.h>
-
-char *strcat(char *__restrict dest, const char *__restrict src)
-{
- strcpy(dest + strlen(dest), src);
- return dest;
-}
diff --git a/libc/string/strcpy.c b/libc/string/strcpy.c
--- a/libc/string/strcpy.c
+++ b/libc/string/strcpy.c
@@ -1,16 +0,0 @@
-#include <string.h>
-
-char *__stpcpy(char *, const char *);
-
-char *strcpy(char *__restrict dest, const char *__restrict src)
-{
-#if 1
- __stpcpy(dest, src);
- return dest;
-#else
- const unsigned char *s = src;
- unsigned char *d = dest;
- while ((*d++ = *s++));
- return dest;
-#endif
-}
diff --git a/libc/string/strncat.c b/libc/string/strncat.c
--- a/libc/string/strncat.c
+++ b/libc/string/strncat.c
@@ -1,10 +0,0 @@
-#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;
-}
diff --git a/libc/string/strncpy.c b/libc/string/strncpy.c
--- a/libc/string/strncpy.c
+++ b/libc/string/strncpy.c
@@ -1,9 +0,0 @@
-#include <string.h>
-
-char *__stpncpy(char *, const char *, size_t);
-
-char *strncpy(char *__restrict d, const char *__restrict s, size_t n)
-{
- __stpncpy(d, s, n);
- return d;
-}
diff --git a/libc/string/strtok.c b/libc/string/strtok.c
--- a/libc/string/strtok.c
+++ b/libc/string/strtok.c
@@ -1,13 +0,0 @@
-#include <string.h>
-
-char *strtok(char *__restrict s, const char *__restrict sep)
-{
- static char *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/libc/string/swab.c b/libc/string/swab.c
--- a/libc/string/swab.c
+++ b/libc/string/swab.c
@@ -1,13 +0,0 @@
-#include <unistd.h>
-
-void swab(const void *__restrict _src, void *__restrict _dest, ssize_t n)
-{
- const char *src = _src;
- char *dest = _dest;
- for (; n>0; n-=2) {
- dest[0] = src[1];
- dest[1] = src[0];
- dest += 2;
- src += 2;
- }
-}
diff --git a/libc/string/wcpcpy.c b/libc/string/wcpcpy.c
--- a/libc/string/wcpcpy.c
+++ b/libc/string/wcpcpy.c
@@ -1,6 +0,0 @@
-#include <wchar.h>
-
-wchar_t *wcpcpy(wchar_t *__restrict d, const wchar_t *__restrict s)
-{
- return wcscpy(d, s) + wcslen(s);
-}
diff --git a/libc/string/wcpncpy.c b/libc/string/wcpncpy.c
--- a/libc/string/wcpncpy.c
+++ b/libc/string/wcpncpy.c
@@ -1,6 +0,0 @@
-#include <wchar.h>
-
-wchar_t *wcpncpy(wchar_t *__restrict d, const wchar_t *__restrict s, size_t n)
-{
- return wcsncpy(d, s, n) + wcsnlen(s, n);
-}
diff --git a/libc/string/wcscat.c b/libc/string/wcscat.c
--- a/libc/string/wcscat.c
+++ b/libc/string/wcscat.c
@@ -1,7 +0,0 @@
-#include <wchar.h>
-
-wchar_t *wcscat(wchar_t *__restrict dest, const wchar_t *__restrict src)
-{
- wcscpy(dest + wcslen(dest), src);
- return dest;
-}
diff --git a/libc/string/wcscpy.c b/libc/string/wcscpy.c
--- a/libc/string/wcscpy.c
+++ b/libc/string/wcscpy.c
@@ -1,8 +0,0 @@
-#include <wchar.h>
-
-wchar_t *wcscpy(wchar_t *__restrict d, const wchar_t *__restrict s)
-{
- wchar_t *a = d;
- while ((*d++ = *s++));
- return a;
-}
diff --git a/libc/string/wcsncat.c b/libc/string/wcsncat.c
--- a/libc/string/wcsncat.c
+++ b/libc/string/wcsncat.c
@@ -1,10 +0,0 @@
-#include <wchar.h>
-
-wchar_t *wcsncat(wchar_t *__restrict d, const wchar_t *__restrict s, size_t n)
-{
- wchar_t *a = d;
- d += wcslen(d);
- while (n && *s) n--, *d++ = *s++;
- *d++ = 0;
- return a;
-}
diff --git a/libc/string/wcsncpy.c b/libc/string/wcsncpy.c
--- a/libc/string/wcsncpy.c
+++ b/libc/string/wcsncpy.c
@@ -1,9 +0,0 @@
-#include <wchar.h>
-
-wchar_t *wcsncpy(wchar_t *__restrict d, const wchar_t *__restrict s, size_t n)
-{
- wchar_t *a = d;
- while (n && *s) n--, *d++ = *s++;
- wmemset(d, 0, n);
- return a;
-}
diff --git a/libc/string/wcsstr.c b/libc/string/wcsstr.c
--- a/libc/string/wcsstr.c
+++ b/libc/string/wcsstr.c
--- a/libc/string/wcstok.c
+++ b/libc/string/wcstok.c
@@ -1,12 +0,0 @@
-#include <wchar.h>
-
-wchar_t *wcstok(wchar_t *__restrict s, const wchar_t *__restrict sep, wchar_t **__restrict p)
-{
- if (!s && !(s = *p)) return NULL;
- s += wcsspn(s, sep);
- if (!*s) return *p = 0;
- *p = s + wcscspn(s, sep);
- if (**p) *(*p)++ = 0;
- else *p = 0;
- return s;
-}
diff --git a/libc/string/wmemcpy.c b/libc/string/wmemcpy.c
--- a/libc/string/wmemcpy.c
+++ b/libc/string/wmemcpy.c
Reply all
Reply to author
Forward
0 new messages