diff options
author | dzwdz | 2022-09-02 23:56:18 +0200 |
---|---|---|
committer | dzwdz | 2022-09-02 23:56:18 +0200 |
commit | 6e4b9831f903e583d58de8b4265159f6d859ebc2 (patch) | |
tree | 9e941257df8898356d605a7f3398aa09d9b00ec9 /src/user/lib/string.c | |
parent | e7770ccc0fab8f3a4b65bf2f7a1a11ad453f77e4 (diff) |
user/ports: bare minimum to run doomgeneric
Diffstat (limited to 'src/user/lib/string.c')
-rw-r--r-- | src/user/lib/string.c | 46 |
1 files changed, 43 insertions, 3 deletions
diff --git a/src/user/lib/string.c b/src/user/lib/string.c index 386cffa..fa9c0ca 100644 --- a/src/user/lib/string.c +++ b/src/user/lib/string.c @@ -1,6 +1,7 @@ #include <ctype.h> #include <errno.h> #include <string.h> +#include <strings.h> long strtol(const char *restrict s, char **restrict end, int base) { long res = 0; @@ -55,7 +56,14 @@ double strtod(const char *restrict s, char **restrict end) { char *strchr(const char *s, int c) { for (; *s; s++) { - if (*s == c) return (char*)s; + if (*s == c) return (char *)s; + } + return NULL; +} + +char *strrchr(const char *s, int c) { + for (int i = strlen(s) + 1; i >= 0; i--) { + if (s[i] == c) return (char *)s + i; } return NULL; } @@ -123,9 +131,24 @@ char *strstr(const char *s1, const char *s2) { } char *strcpy(char *restrict s1, const char *restrict s2) { - char *ret = s1; while (*s2) *s1++ = *s2++; - return ret; + *s1 = *s2; + return s1; +} + +char *strncpy(char *restrict s1, const char *restrict s2, size_t n) { + for (size_t i = 0; i < n; i++) { + s1[i] = s2[i]; + if (s1[i] == '\0') return s1 + i; // TODO fill with null bytes + } + return s1 + n; +} + +char *strdup(const char *s) { + size_t len = strlen(s) + 1; + char *buf = malloc(len); + if (buf) memcpy(buf, s, len); + return buf; } // TODO strerror mapping @@ -133,3 +156,20 @@ char *strerror(int errnum) { (void)errnum; return "unknown error"; } + +/* strings.h */ +int strcasecmp(const char *s1, const char *s2) { + return strncasecmp(s1, s2, ~0); +} + +int strncasecmp(const char *s1, const char *s2, size_t n) { + for (size_t i = 0; i < n; i++) { + char c1 = tolower(s1[i]), c2 = tolower(s2[i]); + if (c1 == '\0' || c1 != c2) { + if (c1 < c2) return -1; + else if (c1 > c2) return 1; + else return 0; + } + } + return 0; +} |