From 99e183a2f569729d722d83503cb851d6198fc1fe Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 23 Jul 2020 13:10:22 +0200 Subject: Some functions for stdlib --- src/lib/mem.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/lib/mem.c (limited to 'src/lib/mem.c') diff --git a/src/lib/mem.c b/src/lib/mem.c new file mode 100644 index 0000000..00b9735 --- /dev/null +++ b/src/lib/mem.c @@ -0,0 +1,33 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#include + +void *memcpy(void *dst, const void *src, u32 n) +{ + const char *sp = (const char *)src; + char *dp = (char *)dst; + for (; n != 0; n--) + *dp++ = *sp++; + return dst; +} + +void *memset(void *dst, char val, u32 n) +{ + char *temp = (char *)dst; + for (; n != 0; n--) + *temp++ = val; + return dst; +} + +int memcmp(const void *s1, const void *s2, u32 n) +{ + const u8 *a = (const u8 *)s1; + const u8 *b = (const u8 *)s2; + for (u32 i = 0; i < n; i++) { + if (a[i] < b[i]) + return -1; + else if (b[i] < a[i]) + return 1; + } + return 0; +} -- cgit v1.2.3