From f490bce1e822b5b8b126c089c5d8a98c48dc1e9b Mon Sep 17 00:00:00 2001 From: "Zachary D. Rowitsch" <rowitsch@yahoo.com> Date: Fri, 23 Dec 2022 15:03:33 -0500 Subject: [PATCH] Adapt some userspace stdlib to work in the kernel, remove some redundancy --- Makefile | 4 +- programs/stdlib/src/stdio.c | 8 +++ programs/stdlib/src/stdlib.c | 3 + programs/stdlib/src/stdlib.h | 3 + src/fs/fat/fat16.c | 2 +- src/fs/file.c | 2 +- src/fs/pparser.c | 2 +- src/isr80h/process.c | 2 +- src/kernel.c | 2 +- src/libk/stdio.c | 1 + src/libk/stdio.h | 1 + src/libk/stdlib.c | 1 + src/libk/stdlib.h | 1 + src/libk/string.c | 1 + src/libk/string.h | 1 + src/loader/formats/elfloader.c | 2 +- src/string/string.c | 101 --------------------------------- src/string/string.h | 16 ------ src/task/process.c | 2 +- src/task/task.c | 2 +- 20 files changed, 30 insertions(+), 127 deletions(-) create mode 120000 src/libk/stdio.c create mode 120000 src/libk/stdio.h create mode 120000 src/libk/stdlib.c create mode 120000 src/libk/stdlib.h create mode 120000 src/libk/string.c create mode 120000 src/libk/string.h delete mode 100644 src/string/string.c delete mode 100644 src/string/string.h diff --git a/Makefile b/Makefile index bbca23b..e8e7834 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,8 @@ O_FILES+=$(ASM_SRC:src/%.asm=build/%.asm.o) #kernel.asm.o must be first, then the rest FILES=build/kernel.asm.o $(O_FILES) -INCLUDES = -Isrc -FLAGS = -g -ffreestanding -falign-jumps -falign-functions -falign-labels -falign-loops -fstrength-reduce -fomit-frame-pointer -finline-functions -Wno-unused-function -fno-builtin -Werror -Wno-unused-label -Wno-cpp -Wno-unused-parameter -nostdlib -nostartfiles -nodefaultlibs -Wall -O0 -Iinc -std=gnu99 +INCLUDES = -Isrc -Isrc/libk +FLAGS = -D__KERNEL__ -g -ffreestanding -falign-jumps -falign-functions -falign-labels -falign-loops -fstrength-reduce -fomit-frame-pointer -finline-functions -Wno-unused-function -fno-builtin -Werror -Wno-unused-label -Wno-cpp -Wno-unused-parameter -nostdlib -nostartfiles -nodefaultlibs -Wall -O0 -Iinc -std=gnu99 CC=i686-elf-gcc LD=i686-elf-ld diff --git a/programs/stdlib/src/stdio.c b/programs/stdlib/src/stdio.c index 62cc2a4..5c29287 100644 --- a/programs/stdlib/src/stdio.c +++ b/programs/stdlib/src/stdio.c @@ -1,11 +1,19 @@ #include "stdio.h" +#ifdef __KERNEL__ +#include "kernel.h" +#else #include "os.h" +#endif #include "stdlib.h" #include <stdarg.h> int putchar(int c) { +#ifdef __KERNEL__ + terminal_writechar(c, 15); +#else os_putchar((char)c); +#endif return 0; } diff --git a/programs/stdlib/src/stdlib.c b/programs/stdlib/src/stdlib.c index d8a5835..a5c4c84 100644 --- a/programs/stdlib/src/stdlib.c +++ b/programs/stdlib/src/stdlib.c @@ -1,4 +1,6 @@ #include "stdlib.h" + +#ifndef __KERNEL__ #include "os.h" void* malloc(size_t size) { @@ -9,6 +11,7 @@ void* malloc(size_t size) { void free(void* ptr) { os_free(ptr); } +#endif char* itoa(int i) { //directly from Developing multithreaded kernel from scratch course diff --git a/programs/stdlib/src/stdlib.h b/programs/stdlib/src/stdlib.h index 9d243db..f18a825 100644 --- a/programs/stdlib/src/stdlib.h +++ b/programs/stdlib/src/stdlib.h @@ -3,8 +3,11 @@ #include <stddef.h> +#ifndef __KERNEL__ void* malloc(size_t size); void free(void* ptr); +#endif + char* itoa(int i); #endif diff --git a/src/fs/fat/fat16.c b/src/fs/fat/fat16.c index 1b9956a..1bc412a 100644 --- a/src/fs/fat/fat16.c +++ b/src/fs/fat/fat16.c @@ -1,6 +1,6 @@ #include "fat16.h" #include "status.h" -#include "string/string.h" +#include "string.h" #include "disk/disk.h" #include "disk/streamer.h" #include "memory/heap/kheap.h" diff --git a/src/fs/file.c b/src/fs/file.c index 42f979b..248fffe 100644 --- a/src/fs/file.c +++ b/src/fs/file.c @@ -6,7 +6,7 @@ #include "kernel.h" #include "fat/fat16.h" #include "disk/disk.h" -#include "string/string.h" +#include "string.h" struct filesystem* filesystems[MAX_FILESYSTEMS]; struct file_descriptor* file_descriptors[MAX_FILE_DESCRIPTORS]; diff --git a/src/fs/pparser.c b/src/fs/pparser.c index b5f66d2..96f296f 100644 --- a/src/fs/pparser.c +++ b/src/fs/pparser.c @@ -1,6 +1,6 @@ #include "pparser.h" #include "config.h" -#include "string/string.h" +#include "string.h" #include "memory/heap/kheap.h" #include "memory/memory.h" #include "status.h" diff --git a/src/isr80h/process.c b/src/isr80h/process.c index e86a5ec..c8baa4f 100644 --- a/src/isr80h/process.c +++ b/src/isr80h/process.c @@ -3,7 +3,7 @@ #include "config.h" #include "status.h" #include "task/process.h" -#include "string/string.h" +#include "string.h" #include "kernel.h" void* isr80h_command6_process_load_start(struct interrupt_frame* frame) { diff --git a/src/kernel.c b/src/kernel.c index b272e6f..2eb7934 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -9,7 +9,7 @@ #include "memory/memory.h" #include "disk/disk.h" #include "fs/pparser.h" -#include "string/string.h" +#include "string.h" #include "disk/streamer.h" #include "fs/file.h" #include "gdt/gdt.h" diff --git a/src/libk/stdio.c b/src/libk/stdio.c new file mode 120000 index 0000000..536ea59 --- /dev/null +++ b/src/libk/stdio.c @@ -0,0 +1 @@ +../../programs/stdlib/src/stdio.c \ No newline at end of file diff --git a/src/libk/stdio.h b/src/libk/stdio.h new file mode 120000 index 0000000..e148a33 --- /dev/null +++ b/src/libk/stdio.h @@ -0,0 +1 @@ +../../programs/stdlib/src/stdio.h \ No newline at end of file diff --git a/src/libk/stdlib.c b/src/libk/stdlib.c new file mode 120000 index 0000000..04c2245 --- /dev/null +++ b/src/libk/stdlib.c @@ -0,0 +1 @@ +../../programs/stdlib/src/stdlib.c \ No newline at end of file diff --git a/src/libk/stdlib.h b/src/libk/stdlib.h new file mode 120000 index 0000000..6021282 --- /dev/null +++ b/src/libk/stdlib.h @@ -0,0 +1 @@ +../../programs/stdlib/src/stdlib.h \ No newline at end of file diff --git a/src/libk/string.c b/src/libk/string.c new file mode 120000 index 0000000..549c0a4 --- /dev/null +++ b/src/libk/string.c @@ -0,0 +1 @@ +../../programs/stdlib/src/string.c \ No newline at end of file diff --git a/src/libk/string.h b/src/libk/string.h new file mode 120000 index 0000000..188c894 --- /dev/null +++ b/src/libk/string.h @@ -0,0 +1 @@ +../../programs/stdlib/src/string.h \ No newline at end of file diff --git a/src/loader/formats/elfloader.c b/src/loader/formats/elfloader.c index a876b86..00bf334 100644 --- a/src/loader/formats/elfloader.c +++ b/src/loader/formats/elfloader.c @@ -6,7 +6,7 @@ #include <stdbool.h> #include "memory/memory.h" #include "memory/heap/kheap.h" -#include "string/string.h" +#include "string.h" #include "memory/paging/paging.h" #include "kernel.h" diff --git a/src/string/string.c b/src/string/string.c deleted file mode 100644 index af19072..0000000 --- a/src/string/string.c +++ /dev/null @@ -1,101 +0,0 @@ -#include "string.h" - - -int strnlen(const char* ptr, int max) { - int i=0; - - for (i = 0; i<max; i++) { - if (ptr[i] == 0) - break; - } - - return i; -} - -int strlen(const char* ptr) { - int i=0; - while(*ptr != 0) { - i++; - ptr += 1; - } - - return i; -} - -bool isdigit(char c) { - return c >=48 && c <= 57; -} - -int tonumericdigit(char c) { - return c - 48; -} - -char* strcpy(char* dest, const char* src) { - char* res = dest; - while (*src != 0) { - *dest = *src; - src +=1; - dest +=1; - } - - *dest = 0x00; - - return res; -} - -char* strncpy(char* dest, const char* src, int count) { - int i = 0; - for (i = 0; i < count - 1; i++) { - if (src[i] == 0x00) - break; - dest[i] = src[i]; - } - - dest[i] = 0x00; - return dest; -} - -int strncmp(const char* str1, const char* str2, int n) { - unsigned char u1, u2; - - while (n-- > 0) { - u1 = (unsigned char)*str1++; - u2 = (unsigned char)*str2++; - if (u1 != u2) - return u1 - u2; - if (u1 == '\0') - return 0; - } - return 0; -} - -int strnlen_terminator(const char* str, int max, char terminator) { - int i = 0; - for (i = 0; i<max; i++) { - if (str[i] == '\0' || str[i] == terminator) - break; - } - return i; -} - -char tolower(char s1) { - if (s1 >= 65 && s1 <=90) { - s1 += 32; - } - return s1; -} - -int istrncmp(const char* s1, const char* s2, int n) { - unsigned char u1, u2; - - while (n-- > 0) { - u1 = (unsigned char)*s1++; - u2 = (unsigned char)*s2++; - if (u1 != u2 && tolower(u1) != tolower(u2)) { - return u1 - u2; - } - if (u1 == '\0') - return 0; - } - return 0; -} diff --git a/src/string/string.h b/src/string/string.h deleted file mode 100644 index 4fb9596..0000000 --- a/src/string/string.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef STRING_H -#define STRING_H - -#include <stdbool.h> - -int strlen(const char* ptr); -int strnlen(const char* ptr, int max); -bool isdigit(char c); -int tonumericdigit(char c); -char* strcpy(char* dest, const char* src); -char* strncpy(char* dest, const char* src, int count); -int strncmp(const char* str1, const char* str2, int n); -int istrncmp(const char* s1, const char* s2, int n); -int strnlength_terminator(const char* str, int ma, char terminator); -char tolower(char s1); -#endif diff --git a/src/task/process.c b/src/task/process.c index e10fefc..84f15da 100644 --- a/src/task/process.c +++ b/src/task/process.c @@ -5,7 +5,7 @@ #include "memory/heap/kheap.h" #include "kernel.h" #include "status.h" -#include "string/string.h" +#include "string.h" #include "fs/file.h" #include "loader/formats/elfloader.h" diff --git a/src/task/task.c b/src/task/task.c index f7367f6..2cc8a06 100644 --- a/src/task/task.c +++ b/src/task/task.c @@ -4,7 +4,7 @@ #include "memory/heap/kheap.h" #include "memory/memory.h" #include "memory/paging/paging.h" -#include "string/string.h" +#include "string.h" #include "idt/idt.h" #include "loader/formats/elfloader.h" #include "process.h"