From caa440daf6e7954cc9515fd090beb64b87493eca Mon Sep 17 00:00:00 2001 From: Joursoir Date: Mon, 2 Aug 2021 22:31:56 +0000 Subject: string: make memcpy(), memset(), memmove() --- kernel/string.c | 42 ++++++++++++++++++++++++++++++++++++++++++ kernel/string.h | 3 +++ 2 files changed, 45 insertions(+) (limited to 'kernel') diff --git a/kernel/string.c b/kernel/string.c index d038c27..006b907 100644 --- a/kernel/string.c +++ b/kernel/string.c @@ -7,3 +7,45 @@ size_t strlen(const char *str) len++; return len; } + +/* + Copy bytes in memory. If copying takes place between objects + that overlap, the behavior is undefined. +*/ +void *memcpy(void *dest, const void *src, size_t n) +{ + size_t i; + const char *s = src; + char *d = dest; + + for(i = 0; i < n; i++) + d[i] = s[i]; + return dest; +} + +void *memset(void *dest, int c, size_t n) +{ + char *d = dest; + while(n--) + *d++ = c; + return dest; +} + +/* + Copy bytes in memory with overlapping areas. +*/ +void *memmove(void *dest, const void *src, size_t n) +{ + const char *s = src; + char *d = dest; + + if(dest == src) + return dest; + + if(dest < src) + return memcpy(dest, src, n); + + while(n--) + d[n] = s[n]; + return dest; +} diff --git a/kernel/string.h b/kernel/string.h index dc760a5..3c628b3 100644 --- a/kernel/string.h +++ b/kernel/string.h @@ -5,5 +5,8 @@ #include size_t strlen(const char *str); +void *memcpy(void *dest, const void *src, size_t n); +void *memset(void *dest, int c, size_t n); +void *memmove(void *dest, const void *src, size_t n); #endif /* KERNEL_STRING */ -- cgit v1.2.3-18-g5258