]> git.corax.cc Git - corax/commitdiff
Add standard C library for use inside the kernel
authorMatthias Kruk <m@m10k.eu>
Tue, 19 Nov 2019 06:42:26 +0000 (15:42 +0900)
committerMatthias Kruk <m@m10k.eu>
Tue, 19 Nov 2019 06:42:26 +0000 (15:42 +0900)
kernel/klibc/Makefile [new file with mode: 0644]
kernel/klibc/string.c [new file with mode: 0644]

diff --git a/kernel/klibc/Makefile b/kernel/klibc/Makefile
new file mode 100644 (file)
index 0000000..6ea855a
--- /dev/null
@@ -0,0 +1,16 @@
+OUTPUT = klibc.a
+OBJECTS = string.o
+PHONY = clean
+INCLUDES = -I../include -I../../include
+CFLAGS = -m32 -Wall -nostdlib -nodefaultlibs -nostartfiles -ffreestanding $(INCLUDES)
+ASFLAGS = $(CFLAGS)
+
+all: $(OUTPUT)
+
+$(OUTPUT): $(OBJECTS)
+       ar -rc $@ $^
+
+clean:
+       rm -rf $(OUTPUT) $(OBJECTS)
+
+.PHONY: $(PHONY)
diff --git a/kernel/klibc/string.c b/kernel/klibc/string.c
new file mode 100644 (file)
index 0000000..e3a9224
--- /dev/null
@@ -0,0 +1,38 @@
+#include <sys/types.h>
+#include <corax/types.h>
+
+size_t strlen(const char *s)
+{
+       size_t ret_val;
+
+       for(ret_val = 0; s[ret_val]; ret_val++);
+
+       return(ret_val);
+}
+
+void* memcpy(void *dst, const void *src, size_t n)
+{
+       u32_t *d;
+       const u32_t *s;
+
+       d = (u32_t*)dst;
+       s = (const u32_t*)src;
+
+       while(n > 4) {
+               *d++ = *s++;
+               n -= 4;
+       }
+
+       if(n >= 2) {
+               *((u16_t*)d) = *((u16_t*)s);
+               d = (u32_t*)(((void*)d) + 2);
+               s = (u32_t*)(((void*)s) + 2);
+               n -= 2;
+       }
+
+       if(n >= 1) {
+               *((u8_t*)d) = *((u8_t*)s);
+       }
+
+       return(dst);
+}