scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit e1574e8a5c51b6a316947694cf70192ee98f9b6a
parent 07a0d5a07e238e5cd637bcfac976b69d698651d9
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 27 Aug 2021 11:44:57 +0200

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.

Diffstat:
Msrc/libc/string/Makefile | 7++++++-
Msrc/libc/string/memchr.c | 1+
Msrc/libc/string/memcmp.c | 9+++++----
Msrc/libc/string/memcpy.c | 12+++++++-----
Msrc/libc/string/memmove.c | 9++++++---
Msrc/libc/string/memset.c | 1+
Msrc/libc/string/strcat.c | 13++++++++-----
Msrc/libc/string/strchr.c | 4+++-
Msrc/libc/string/strcmp.c | 3++-
Msrc/libc/string/strcoll.c | 1+
Msrc/libc/string/strcpy.c | 8+++++---
Msrc/libc/string/strcspn.c | 14+++++++-------
Msrc/libc/string/strerror.c | 3++-
Msrc/libc/string/strlen.c | 1+
Msrc/libc/string/strncat.c | 17++++++++++-------
Msrc/libc/string/strncmp.c | 7++++---
Msrc/libc/string/strncpy.c | 13++++++++-----
Msrc/libc/string/strpbrk.c | 12++++++------
Msrc/libc/string/strrchr.c | 4+++-
Msrc/libc/string/strspn.c | 12++++++------
Msrc/libc/string/strstr.c | 12++++++++----
Msrc/libc/string/strtok.c | 23++++++++++++-----------
Msrc/libc/string/strxfrm.c | 1+
23 files changed, 113 insertions(+), 74 deletions(-)

diff --git a/src/libc/string/Makefile b/src/libc/string/Makefile @@ -4,7 +4,12 @@ PROJECTDIR =../../.. 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\ diff --git a/src/libc/string/memchr.c b/src/libc/string/memchr.c @@ -1,4 +1,5 @@ #include <string.h> + #undef memchr void * diff --git 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; } diff --git 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; } diff --git a/src/libc/string/memmove.c b/src/libc/string/memmove.c @@ -1,18 +1,21 @@ #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) *d++ = *s++; } else { s += n-1, d += n-1; + while (n-- > 0) *d-- = *s--; } - return dst; + return s1; } diff --git a/src/libc/string/memset.c b/src/libc/string/memset.c @@ -1,4 +1,5 @@ #include <string.h> + #undef memset void * diff --git 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 (*s1) + ++s1; - while (*dst) - ++dst; - while (*dst++ = *src++) + while ((*s1++ = *s2++) != 0) ; + return ret; } diff --git 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 @@ strchr(const char *s, int c) { while (*s && *s != c) ++s; - return (*s == c) ? (char *)s : NULL; + + return (*s == c) ? s : NULL; } diff --git 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 @@ strcmp(const char *s1, const char *s2) { while (*s1 && *s2 && *s1 == *s2) ++s1, ++s2; - return *(unsigned char *)s1 - *(unsigned char *)s2; + return *(unsigned char *) s1 - *(unsigned char *) s2; } diff --git a/src/libc/string/strcoll.c b/src/libc/string/strcoll.c @@ -1,4 +1,5 @@ #include <string.h> + #undef strcoll int diff --git 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; } diff --git a/src/libc/string/strcspn.c b/src/libc/string/strcspn.c @@ -1,20 +1,20 @@ #include <string.h> + #undef strcspn size_t 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; diff --git 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]; } diff --git a/src/libc/string/strlen.c b/src/libc/string/strlen.c @@ -1,4 +1,5 @@ #include <string.h> + #undef strlen size_t diff --git 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(*s1) + ++s1; + + while (n-- > 0 && *s2) + *s1++ = *s2++; + *s1 = '\0'; - while (*dst) - ++dst; - while (n-- > 0 && *src) - *dst++ = *src++; - *dst = '\0'; return ret; } diff --git 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; } diff --git 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 && *s2; --n) + *s1++ = *s2++; - for (; n > 0 && *src; --n) - *dst++ = *src++; while (n-- > 0) - *dst++ = '\0'; + *s1++ = '\0'; + return ret; } diff --git 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 @@ strpbrk(const char *s1, const char *s2) 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; } diff --git 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 @@ strrchr(const char *s, int c) ++t; while (t > s && *t != c) --t; - return (*t == c) ? (char *)t : NULL; + + return (*t == c) ? t : NULL; } diff --git 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 @@ strspn(const char *s1, const char *s2) { 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; diff --git 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 @@ strstr(const char *s1, const char *s2) { const char *p; int c = *s2; + int len; + + if ((len = strlen(s2)) == 0) + return s1; - if (c == '\0') - return NULL; for (p = s1; p = strchr(p, c); ++p) { - if (!strcmp(p, s2)) - return (char *) p; + if (!strncmp(p, s2, len)) + return p; } + return NULL; } diff --git 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; } diff --git a/src/libc/string/strxfrm.c b/src/libc/string/strxfrm.c @@ -1,4 +1,5 @@ #include <string.h> + #undef strxfrm size_t