diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/include/string.h | 2 | ||||
-rw-r--r-- | src/common/string.c | 20 |
2 files changed, 21 insertions, 1 deletions
diff --git a/src/common/include/string.h b/src/common/include/string.h index 9608dd5..686382d 100644 --- a/src/common/include/string.h +++ b/src/common/include/string.h @@ -12,8 +12,10 @@ size_t strlen(const char *str); char *strchr(const char *str, char c); char *strrchr(const char *str, char c); char *strcpy(char *dest, const char *src); +char *strncpy(char *dest, const char *src, size_t n); char *strcat(char *dest, const char *src); int strcmp(const char *s1, const char *s2); +int strncmp(const char *s1, const char *s2, size_t n); char *strdup(const char* str); diff --git a/src/common/string.c b/src/common/string.c index dc25aee..b50356a 100644 --- a/src/common/string.c +++ b/src/common/string.c @@ -26,7 +26,15 @@ char *strrchr(const char *str, char c) { char *strcpy(char *dest, const char *src) { memcpy(dest, src, strlen(src) + 1); - return (char*)src; + return (char*)dest; +} + +char *strncpy(char *dest, const char *src, size_t n) { + size_t x = strlen(src + 1); + if (n < x) x = n; + memcpy(dest, src, x); + if (n > x) memset(dest + n, 0, n - x); + return (char*)dest; } char *strcat(char *dest, const char *src) { @@ -49,6 +57,16 @@ int strcmp(const char *s1, const char *s2) { return (*(unsigned char*)s1 - *(unsigned char*)s2); } +int strcmp(const char *s1, const char *s2, size_t n) { + size_t i = 0; + while ((*s1) && (*s1 == *s2) && i != n) { + s1++; + s2++; + i++; + } + return (*(unsigned char*)s1 - *(unsigned char*)s2); +} + void *memcpy(void *vd, const void *vs, size_t count) { uint8_t *dest = (uint8_t*)vd, *src = (uint8_t*)vs; size_t f = count % 4, n = count / 4, i; |