From 77a7742538f922d988f63236dff2e56acca8eddc Mon Sep 17 00:00:00 2001 From: Thomas Oltmann Date: Sun, 16 Mar 2025 01:29:52 +0100 Subject: [PATCH] memcpy() + memset(); Makefile for libc --- libc/Makefile | 53 ++++++++++++++++++++++++++++++++++++++++++++ libc/string/memcpy.c | 10 +++++++++ libc/string/memset.c | 9 ++++++++ 3 files changed, 72 insertions(+) create mode 100644 libc/Makefile create mode 100644 libc/string/memcpy.c create mode 100644 libc/string/memset.c diff --git a/libc/Makefile b/libc/Makefile new file mode 100644 index 0000000..784e4ef --- /dev/null +++ b/libc/Makefile @@ -0,0 +1,53 @@ +include ../config.mk + +AR=ar +ARFLAGS=rcs + +LIBC_OBJECTS= \ + string/ffs.o \ + string/ffsl.o \ + string/ffsll.o \ + string/memccpy.o \ + string/memchr.o \ + string/memcmp.o \ + string/memcpy.o \ + string/memmove.o \ + string/memrchr.o \ + string/memset.o \ + string/stpcpy.o \ + string/stpncpy.o \ + string/strcat.o \ + string/strchr.o \ + string/strchrnul.o \ + string/strcmp.o \ + string/strcoll.o \ + string/strcpy.o \ + string/strcspn.o \ + string/stresep.o \ + string/strlcat.o \ + string/strlcpy.o \ + string/strlen.o \ + string/strncat.o \ + string/strncmp.o \ + string/strncpy.o \ + string/strnlen.o \ + string/strpbrk.o \ + string/strrchr.o \ + string/strsep.o \ + string/strspn.o \ + string/strstr.o \ + string/strtok_r.o \ + string/strxfrm.o \ + # end of source list + +libc.a: $(LIBC_OBJECTS) + $(AR) $(ARFLAGS) $@ $(LIBC_OBJECTS) + +.c.o: string.h + $(CC) $(CFLAGS) -c -o $@ $(@:.o=.c) $(CPPFLAGS) + +clean: + rm -f $(LIBC_OBJECTS) + rm -f libc.a + +.PHONY: clean diff --git a/libc/string/memcpy.c b/libc/string/memcpy.c new file mode 100644 index 0000000..720c361 --- /dev/null +++ b/libc/string/memcpy.c @@ -0,0 +1,10 @@ +#include + +void *memcpy(void *dest, const void *src, size_t n) { + unsigned char *d = dest; + const unsigned char *s = src; + for (size_t i = 0; i < n; i++) { + d[i] = s[i]; + } + return dest; +} diff --git a/libc/string/memset.c b/libc/string/memset.c new file mode 100644 index 0000000..f6524eb --- /dev/null +++ b/libc/string/memset.c @@ -0,0 +1,9 @@ +#include + +void *memset(void *ptr, int value, size_t num) { + unsigned char *byte_ptr = ptr; + for (size_t i = 0; i < num; i++) { + byte_ptr[i] = (unsigned char)value; + } + return ptr; +}