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 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'kernel/string.c') 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; +} -- cgit v1.2.3-18-g5258