Adapt some userspace stdlib to work in the kernel, remove some redundancy

This commit is contained in:
Zachary D. Rowitsch 2022-12-23 15:03:33 -05:00
parent c52ec69591
commit f490bce1e8
20 changed files with 30 additions and 127 deletions

@ -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

@ -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;
}

@ -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

@ -3,8 +3,11 @@
#include <stddef.h>
#ifndef __KERNEL__
void* malloc(size_t size);
void free(void* ptr);
#endif
char* itoa(int i);
#endif

@ -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"

@ -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];

@ -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"

@ -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) {

@ -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"

1
src/libk/stdio.c Symbolic link

@ -0,0 +1 @@
../../programs/stdlib/src/stdio.c

1
src/libk/stdio.h Symbolic link

@ -0,0 +1 @@
../../programs/stdlib/src/stdio.h

1
src/libk/stdlib.c Symbolic link

@ -0,0 +1 @@
../../programs/stdlib/src/stdlib.c

1
src/libk/stdlib.h Symbolic link

@ -0,0 +1 @@
../../programs/stdlib/src/stdlib.h

1
src/libk/string.c Symbolic link

@ -0,0 +1 @@
../../programs/stdlib/src/string.c

1
src/libk/string.h Symbolic link

@ -0,0 +1 @@
../../programs/stdlib/src/string.h

@ -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"

@ -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;
}

@ -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

@ -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"

@ -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"