ref: 22c3bdf20b42098542640447cd409de2b999e23b
parent: 31198f45160ad08b972e4e14272a08cb79393134
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Aug 27 07:44:57 EDT 2021
libc: Update string to last version This code is being updated out of the tree of scc and it is time to synchroniza both copies now.
--- a/src/libc/string/Makefile
+++ b/src/libc/string/Makefile
@@ -4,7 +4,12 @@
include $(PROJECTDIR)/scripts/rules.mk
include ../rules.mk
-OBJS = \
+OBJS =\
+ memchr.$O\
+ memcmp.$O\
+ memcpy.$O\
+ memmove.$O\
+ memset.$O\
strcat.$O\
strchr.$O\
strcmp.$O\
--- a/src/libc/string/memchr.c
+++ b/src/libc/string/memchr.c
@@ -1,4 +1,5 @@
#include <string.h>
+
#undef memchr
void *
--- a/src/libc/string/memcmp.c
+++ b/src/libc/string/memcmp.c
@@ -1,14 +1,15 @@
#include <string.h>
+
#undef memcmp
int
memcmp(const void *s1, const void *s2, size_t n)
{
- const char *s = s1;
- const char *t = s2;
+ const unsigned char *s = s1;
+ const unsigned char *t = s2;
- for ( ; n > 0 && *s == *t; --n)
+ for (; n > 0 && *s == *t; --n)
++s, ++t;
- return (n > 0) ? *(unsigned char *) s - *(unsigned char *) t : 0;
+ return (n > 0) ? *s - *t : 0;
}
--- a/src/libc/string/memcpy.c
+++ b/src/libc/string/memcpy.c
@@ -1,13 +1,15 @@
#include <string.h>
+
#undef memcpy
void *
-memcpy(void * restrict dst, const void * restrict src, size_t n)
+memcpy(void *restrict s1, const void *restrict s2, size_t n)
{
- char *s1 = dst;
- const char *s2 = src;
+ char *d = s1;
+ const char *s = s2;
while (n-- > 0)
- *s1++ = *s2++;
- return dst;
+ *d++ = *s++;
+
+ return s1;
}
--- a/src/libc/string/memmove.c
+++ b/src/libc/string/memmove.c
@@ -1,10 +1,12 @@
#include <string.h>
+
#undef memmove
void *
-memmove(void *dst, const void *src, size_t n)
+memmove(void *s1, const void *s2, size_t n)
{
- char *d = dst, *s = (char *) src;
+ char *d = s1;
+ const char *s = s2;
if (d < s) {
while (n-- > 0)
@@ -11,8 +13,9 @@
*d++ = *s++;
} else {
s += n-1, d += n-1;
+
while (n-- > 0)
*d-- = *s--;
}
- return dst;
+ return s1;
}
--- a/src/libc/string/memset.c
+++ b/src/libc/string/memset.c
@@ -1,4 +1,5 @@
#include <string.h>
+
#undef memset
void *
--- a/src/libc/string/strcat.c
+++ b/src/libc/string/strcat.c
@@ -1,14 +1,17 @@
#include <string.h>
+
#undef strcat
char *
-strcat(char * restrict dst, const char * restrict src)
+strcat(char *restrict s1, const char *restrict s2)
{
- char *ret = dst;
+ char *ret = s1;
- while (*dst)
- ++dst;
- while (*dst++ = *src++)
+ while (*s1)
+ ++s1;
+
+ while ((*s1++ = *s2++) != 0)
;
+
return ret;
}
--- a/src/libc/string/strchr.c
+++ b/src/libc/string/strchr.c
@@ -1,4 +1,5 @@
#include <string.h>
+
#undef strchr
char *
@@ -6,5 +7,6 @@
{
while (*s && *s != c)
++s;
- return (*s == c) ? (char *)s : NULL;
+
+ return (*s == c) ? s : NULL;
}
--- a/src/libc/string/strcmp.c
+++ b/src/libc/string/strcmp.c
@@ -1,4 +1,5 @@
#include <string.h>
+
#undef strcmp
int
@@ -6,5 +7,5 @@
{
while (*s1 && *s2 && *s1 == *s2)
++s1, ++s2;
- return *(unsigned char *)s1 - *(unsigned char *)s2;
+ return *(unsigned char *) s1 - *(unsigned char *) s2;
}
--- a/src/libc/string/strcoll.c
+++ b/src/libc/string/strcoll.c
@@ -1,4 +1,5 @@
#include <string.h>
+
#undef strcoll
int
--- a/src/libc/string/strcpy.c
+++ b/src/libc/string/strcpy.c
@@ -1,12 +1,14 @@
#include <string.h>
+
#undef strcpy
char *
-strcpy(char * restrict dst, const char * restrict src)
+strcpy(char *restrict s1, const char *restrict s2)
{
- char *ret = dst;
+ char *ret = s1;
- while (*dst++ = *src++)
+ while ((*s1++ = *s2++) != '\0')
;
+
return ret;
}
--- a/src/libc/string/strcspn.c
+++ b/src/libc/string/strcspn.c
@@ -1,4 +1,5 @@
#include <string.h>
+
#undef strcspn
size_t
@@ -5,16 +6,15 @@
strcspn(const char *s1, const char *s2)
{
const unsigned char *s = s1;
- const unsigned char *accept = s2;
- unsigned ch;
+ const unsigned char *reject = s2;
size_t n;
- char buf[__NUMCHARS];
+ unsigned ch;
+ char map[__NUMCHARS] = {0};
- memset(buf, 0, sizeof(buf));
- while (ch = *accept++)
- buf[ch] = 1;
+ while (ch = *reject++)
+ map[ch] = 1;
- for (n = 0; (ch = *s++) && !buf[ch]; ++n)
+ for (n = 0; (ch = *s++) && !map[ch]; ++n)
;
return n;
--- a/src/libc/string/strerror.c
+++ b/src/libc/string/strerror.c
@@ -1,11 +1,12 @@
#include <errno.h>
#include <string.h>
+
#undef strerror
char *
strerror(int errnum)
{
- if (errnum > _sys_nerr)
+ if (errnum > EUNKNOWN || errnum <= 0)
errnum = EUNKNOWN;
return _sys_errlist[errnum];
}
--- a/src/libc/string/strlen.c
+++ b/src/libc/string/strlen.c
@@ -1,4 +1,5 @@
#include <string.h>
+
#undef strlen
size_t
--- a/src/libc/string/strncat.c
+++ b/src/libc/string/strncat.c
@@ -1,15 +1,18 @@
#include <string.h>
+
#undef strncat
char *
-strncat(char * restrict dst, const char * restrict src, size_t n)
+strncat(char *restrict s1, const char *restrict s2, size_t n)
{
- char *ret = dst;
+ char *ret = s1;
- while (*dst)
- ++dst;
- while (n-- > 0 && *src)
- *dst++ = *src++;
- *dst = '\0';
+ while(*s1)
+ ++s1;
+
+ while (n-- > 0 && *s2)
+ *s1++ = *s2++;
+ *s1 = '\0';
+
return ret;
}
--- a/src/libc/string/strncmp.c
+++ b/src/libc/string/strncmp.c
@@ -1,14 +1,15 @@
#include <string.h>
+
#undef strncmp
int
strncmp(const char *s1, const char *s2, size_t n)
{
- int c;
-
- for ( ; n > 0 && (c = *s1) && c == *s2; --n)
+ for ( ; n > 0 && *s1 == *s2; --n)
++s1, ++s2;
+
if (n == 0)
return 0;
+
return *(unsigned char *) s1 - *(unsigned char *) s2;
}
--- a/src/libc/string/strncpy.c
+++ b/src/libc/string/strncpy.c
@@ -1,14 +1,17 @@
#include <string.h>
+
#undef strncpy
char *
-strncpy(char * restrict dst, const char * restrict src, size_t n)
+strncpy(char *restrict s1, const char *restrict s2, size_t n)
{
- char *ret = dst;
+ char *ret = s1;
- for (; n > 0 && *src; --n)
- *dst++ = *src++;
+ for (; n > 0 && *s2; --n)
+ *s1++ = *s2++;
+
while (n-- > 0)
- *dst++ = '\0';
+ *s1++ = '\0';
+
return ret;
}
--- a/src/libc/string/strpbrk.c
+++ b/src/libc/string/strpbrk.c
@@ -1,4 +1,5 @@
#include <string.h>
+
#undef strpbrk
char *
@@ -7,14 +8,13 @@
const unsigned char *s = s1;
const unsigned char *accept = s2;
unsigned ch;
- char buf[__NUMCHARS];
+ char map[__NUMCHARS] = {0};
- memset(buf, 0, sizeof(buf));
- while (ch = *accept++)
- buf[ch] = 1;
+ while ((ch = *accept++) != 0)
+ map[ch] = 1;
- while ((ch = *s) && !buf[ch])
+ while ((ch = *s) != 0 && !map[ch])
s++;
- return (ch == '\0') ? NULL : (char *) s;
+ return (ch == '\0') ? NULL : s;
}
--- a/src/libc/string/strrchr.c
+++ b/src/libc/string/strrchr.c
@@ -1,4 +1,5 @@
#include <string.h>
+
#undef strrchr
char *
@@ -10,5 +11,6 @@
++t;
while (t > s && *t != c)
--t;
- return (*t == c) ? (char *)t : NULL;
+
+ return (*t == c) ? t : NULL;
}
--- a/src/libc/string/strspn.c
+++ b/src/libc/string/strspn.c
@@ -1,4 +1,5 @@
#include <string.h>
+
#undef strspn
size_t
@@ -6,15 +7,14 @@
{
const unsigned char *s = s1;
const unsigned char *accept = s2;
- unsigned ch;
size_t n;
- char buf[__NUMCHARS];
+ unsigned ch;
+ char map[__NUMCHARS] = {0};
- memset(buf, 0, sizeof(buf));
- while (ch = *accept++)
- buf[ch] = 1;
+ while ((ch = *accept++) != '\0')
+ map[ch] = 1;
- for (n = 0; (ch = *s++) && buf[ch]; ++n)
+ for (n = 0; (ch = *s++) != '\0' && map[ch]; ++n)
;
return n;
--- a/src/libc/string/strstr.c
+++ b/src/libc/string/strstr.c
@@ -1,5 +1,6 @@
#include <stddef.h>
#include <string.h>
+
#undef strstr
char *
@@ -7,12 +8,15 @@
{
const char *p;
int c = *s2;
+ int len;
- if (c == '\0')
- return NULL;
+ if ((len = strlen(s2)) == 0)
+ return s1;
+
for (p = s1; p = strchr(p, c); ++p) {
- if (!strcmp(p, s2))
- return (char *) p;
+ if (!strncmp(p, s2, len))
+ return p;
}
+
return NULL;
}
--- a/src/libc/string/strtok.c
+++ b/src/libc/string/strtok.c
@@ -1,25 +1,26 @@
#include <string.h>
+
#undef strtok
char *
-strtok(char * restrict s, const char * restrict delim)
+strtok(char * restrict s1, const char * restrict s2)
{
static char *line;
- if (s)
- line = s;
- if (!s && !line)
+ if (s1)
+ line = s1;
+ else if (!line)
return NULL;
- s = line + strspn(line, delim);
- if (*s == '\0')
+ s1 = line + strspn(line, s2);
+ if (*s1 == '\0')
return line = NULL;
- line = s + strcspn(s, delim);
- if (*line != '\0')
- *line++ = '\0';
- else
+ line = s1 + strcspn(s1, s2);
+ if (*line == '\0')
line = NULL;
+ else
+ *line++ = '\0';
- return s;
+ return s1;
}
--- a/src/libc/string/strxfrm.c
+++ b/src/libc/string/strxfrm.c
@@ -1,4 +1,5 @@
#include <string.h>
+
#undef strxfrm
size_t