From 40390ebbd0c3e7363fec85e2c5ecd33fd3c36a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 4 Nov 2015 09:06:27 +0100 Subject: kernel: add stdlib.[ch] --- kernel/Makefile | 6 ++++-- kernel/stdlib.c | 33 +++++++++++++++++++++++++++++++++ kernel/stdlib.h | 12 ++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 kernel/stdlib.c create mode 100644 kernel/stdlib.h diff --git a/kernel/Makefile b/kernel/Makefile index 7250696..431c01a 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,7 +1,7 @@ all: kernel.elf -OBJS=entry.o tty.o -HEADERS=tty.h +OBJS=entry.o stdlib.o tty.o +HEADERS=stdlib.h tty.h .SUFFIXES: .asm @@ -20,6 +20,8 @@ kernel.elf: kernel.o $(OBJS) $(HEADERS) entry.o: entry.asm +stdlib.o: stdlib.c stdlib.h + tty.o: tty.c tty.h diff --git a/kernel/stdlib.c b/kernel/stdlib.c new file mode 100644 index 0000000..92fdec2 --- /dev/null +++ b/kernel/stdlib.c @@ -0,0 +1,33 @@ +#include "stdlib.h" + +void *memset(void *s, int c, size_t n) +{ + char *p = (char *) s; + + for (size_t i = 0; i < n; i++, p++) + *p = (char) c; + + return s; +} + +/* overlap is not handled */ +void *memcpy(void *dest, void *src, size_t n) +{ + char *s = (char *) src; + char *d = (char *) dest; + + for (size_t i = 0; i < n; i++, s++, d++) + *d = *s; + + return dest; +} + +int strncmp(const char *s1, const char *s2, size_t n) +{ + for (size_t i = 0; i < n; i++, s1++, s2++) { + if (*s1 < *s2) return -1; + if (*s1 > *s2) return 1; + } + + return 0; +} diff --git a/kernel/stdlib.h b/kernel/stdlib.h new file mode 100644 index 0000000..58bc3fa --- /dev/null +++ b/kernel/stdlib.h @@ -0,0 +1,12 @@ +#ifndef _STDLIB_H_ +#define _STDLIB_H_ + +#include "types.h" + +void *memset(void *s, int c, size_t n); + +void *memcpy(void *dest, void *src, size_t n); + +int strncmp(const char *s1, const char *s2, size_t n); + +#endif -- cgit v1.1-2-g2b99