From 655452d663f6f9b32d450cc5d00c3005b7588ea1 Mon Sep 17 00:00:00 2001 From: Danny Holman Date: Mon, 18 Jan 2021 21:32:00 -0600 Subject: kernel: finish implementing string functions Finish implementing the functions from string.h in string.c. Signed-off-by: Danny Holman --- kernel/string.c | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/kernel/string.c b/kernel/string.c index 3610909..5b5957c 100644 --- a/kernel/string.c +++ b/kernel/string.c @@ -1,19 +1,53 @@ #include int memcmp(const void *str1, const void *str2, size_t n) { - return 0; + unsigned char *p1 = str1; + unsigned char *p2 = str2; + int ret = 0; + + if (str1 == str2) + return 0; + + while (n > 0) { + if (*p1 != *p2) { + ret = (*p1 > *p2)?1:-1; + break; + } + n--; + p1++; + p2++; + } + return ret; } void* memcpy(void* __restrict dest, const void* __restrict src, size_t n) { - return NULL; + if (dest == src) + return dest; + + unsigned char *pdest = dest; + unsigned char const *psrc = src; + for (size_t i = 0; i < n; i++) + pdest[i] = psrc[i]; + return dest; } void* memmove(void* __restrict dest, const void* __restrict src, size_t n) { - return NULL; + if (dest == src) + return dest; + + unsigned char const *psrc = src; + unsigned char buffer[n]; + + for (size_t i = 0; i < n; i++) + buffer[i] = psrc[i]; + return memcpy(dest, (void*)buffer, n); } void* memset(void *str, int c, size_t n) { - return NULL; + unsigned char *p = str; + for (size_t i = 0; i < n; i++) + p[i] = (unsigned char)c; + return str; } size_t strlen(const char *str) { -- cgit v1.2.3