From a375561ca15a99dd7024e5b8170a3c8fcf25b892 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sun, 30 Nov 2014 20:05:47 +0100 Subject: More library functions & renaming. --- kernel/Makefile | 6 +++-- kernel/include/printf.h | 3 ++- kernel/include/stdlib.h | 7 ----- kernel/include/string.h | 13 ++++++++++ kernel/l0/dbglog.c | 2 +- kernel/lib/stdlib.c | 8 ------ kernel/lib/string.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 88 insertions(+), 19 deletions(-) delete mode 100644 kernel/include/stdlib.h create mode 100644 kernel/include/string.h delete mode 100644 kernel/lib/stdlib.c create mode 100644 kernel/lib/string.c (limited to 'kernel') diff --git a/kernel/Makefile b/kernel/Makefile index 6e973dd..04721ba 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -5,11 +5,11 @@ ASFLAGS = -felf CC = i586-elf-gcc CFLAGS = -ffreestanding -O2 -std=gnu99 -Wall -Wextra -I . -I ./include # CXX = i586-elf-g++ -# CXFLAGS = -ffreestanding -O2 -Wall -Wextra -I . -I ./include -fno-exceptions -fno-rtti +# CXFLAGS = -ffreestanding -O3 -Wall -Wextra -I . -I ./include -fno-exceptions -fno-rtti LD = i586-elf-gcc LDFLAGS = -T linker.ld -ffreestanding -O2 -nostdlib -lgcc -OBJ = lib/stdlib.o lib/printf.o l0/loader.o l0/kmain.o l0/dbglog.o l0/sys.o +OBJ = lib/string.o lib/printf.o l0/loader.o l0/kmain.o l0/dbglog.o l0/sys.o OUT = kernel.bin all: $(OUT) @@ -30,3 +30,5 @@ clean: rm */*.o || true mrproper: clean rm $(OUT) + +rebuild: mrproper all diff --git a/kernel/include/printf.h b/kernel/include/printf.h index 4569827..b37816d 100644 --- a/kernel/include/printf.h +++ b/kernel/include/printf.h @@ -1,6 +1,7 @@ #pragma once -#include +#include +#include #include int snprintf(char* s, size_t n, const char* format, ...); diff --git a/kernel/include/stdlib.h b/kernel/include/stdlib.h deleted file mode 100644 index 601e7c4..0000000 --- a/kernel/include/stdlib.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include -#include - -size_t strlen(const char*); - diff --git a/kernel/include/string.h b/kernel/include/string.h new file mode 100644 index 0000000..1eb3eef --- /dev/null +++ b/kernel/include/string.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include + +void *memcpy(void *dest, const void *src, int count); +void *memset(void *dest, int val, int count); + +size_t strlen(const char *str); +char *strchr(const char *str, char c); +char *strcpy(char *dest, const char *src); +char *strcat(char *dest, const char *src); +int strcmp(const char *s1, const char *s2); diff --git a/kernel/l0/dbglog.c b/kernel/l0/dbglog.c index 0285002..47098a0 100644 --- a/kernel/l0/dbglog.c +++ b/kernel/l0/dbglog.c @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/kernel/lib/stdlib.c b/kernel/lib/stdlib.c deleted file mode 100644 index 6710da2..0000000 --- a/kernel/lib/stdlib.c +++ /dev/null @@ -1,8 +0,0 @@ -#include - -size_t strlen(const char* str) { - size_t ret = 0; - while (str[ret] != 0) - ret++; - return ret; -} diff --git a/kernel/lib/string.c b/kernel/lib/string.c new file mode 100644 index 0000000..da8f60e --- /dev/null +++ b/kernel/lib/string.c @@ -0,0 +1,68 @@ +#include + + +size_t strlen(const char* str) { + size_t ret = 0; + while (str[ret] != 0) + ret++; + return ret; +} + +char *strchr(const char *str, char c) { + while (*str) { + if (*str == c) return (char*)str; + str++; + } + return NULL; +} + +char *strcpy(char *dest, const char *src) { + memcpy(dest, src, strlen(src) + 1); + return (char*)src; +} + +char *strcat(char *dest, const char *src) { + char *dest2 = dest; + dest2 += strlen(dest) - 1; + while (*src) { + *dest2 = *src; + src++; + dest2++; + } + *dest2 = 0; + return dest; +} + +int strcmp(const char *s1, const char *s2) { + while ((*s1) && (*s1 == *s2)) { + s1++; + s2++; + } + return (* (unsigned char*)s1 - *(unsigned char*)s2); +} + +void *memcpy(void *vd, const void *vs, int count) { + uint8_t *dest = (uint8_t*)vd, *src = (uint8_t*)vs; + int f = count % 4, n = count / 4, i; + const uint32_t* s = (uint32_t*)src; + uint32_t* d = (uint32_t*)dest; + for (i = 0; i < n; i++) { + d[i] = s[i]; + } + if (f != 0) { + for (i = count - f; i < count; i++) { + dest[i] = src[i]; + } + } + return vd; +} + +void *memset(void *dest, int val, int count) { + uint8_t *dest_c = (uint8_t*)dest; + int i; + for (i = 0; i < count; i++) { + dest_c[i] = val; + } + return dest; +} + -- cgit v1.2.3