diff --git a/src/string.c b/src/string.c index f337701c..b0fe1eb7 100644 --- a/src/string.c +++ b/src/string.c @@ -16,6 +16,27 @@ void *memcpy(void *s1, const void *s2, size_t n) return s1; } +void *memmove(void *s1, const void *s2, size_t n) +{ + char *dest = (char *)s1; + const char *src = (const char *)s2; + + if (dest <= src) { + while (n--) { + *dest++ = *src++; + } + } else { + src += n; + dest += n; + + while (n--) { + *--dest = *--src; + } + } + + return s1; +} + int memcmp(const void *s1, const void *s2, size_t n) { const unsigned char *p1 = (const unsigned char *)s1; @@ -100,6 +121,21 @@ int strcmp(const char *s1, const char *s2) return (*(unsigned char *)s1 - *(unsigned char *)s2); } +int strncmp(const char *s1, const char *s2, size_t n) +{ + while (n && *s1 && (*s1 == *s2)) { + ++s1; + ++s2; + --n; + } + + if (n == 0) { + return 0; + } else { + return (*(unsigned char *)s1 - *(unsigned char *)s2); + } +} + size_t strlen(const char *s) { size_t rc = 0; @@ -121,3 +157,20 @@ char *strchr(const char *s, int c) return NULL; } + +char *strrchr(const char *s, int c) +{ + size_t i = 0; + + while (s[i++]) { + /* EMPTY */ + } + + do { + if (s[--i] == (char)c) { + return (char *)s + i; + } + } while (i); + + return NULL; +} diff --git a/src/string.h b/src/string.h index 96409e53..114d80b4 100644 --- a/src/string.h +++ b/src/string.h @@ -6,13 +6,16 @@ #include void *memcpy(void *s1, const void *s2, size_t n); +void *memmove(void *s1, const void *s2, size_t n); int memcmp(const void *s1, const void *s2, size_t n); void *memset(void *s, int c, size_t n); void *memchr(const void *s, int c, size_t n); char *strcpy(char *s1, const char *s2); char *strncpy(char *s1, const char *s2, size_t n); int strcmp(const char *s1, const char *s2); +int strncmp(const char *s1, const char *s2, size_t n); size_t strlen(const char *s); char *strchr(const char *s, int c); +char *strrchr(const char *s, int c); #endif