From c601aed9f21540add494e31d631da701574cc312 Mon Sep 17 00:00:00 2001 From: Nathan Giddings Date: Wed, 18 Nov 2020 13:36:09 -0600 Subject: [PATCH] Wrote strlen() and strcpy() --- src/Makefile.am | 2 +- src/module.cpp | 2 +- src/util.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++- src/util.hpp | 4 ++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 52e771e..718ad10 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,5 @@ noinst_PROGRAMS = quark-kernel -quark_kernel_SOURCES = startup.cpp elf.cpp tty.cpp systeminfo.cpp util.cpp memorymap.cpp pageallocator.cpp allocator.cpp scheduler.cpp +quark_kernel_SOURCES = startup.cpp module.cpp tty.cpp systeminfo.cpp util.cpp memorymap.cpp pageallocator.cpp allocator.cpp scheduler.cpp quark_kernel_LDADD = -lgcc quark_kernel_CPPFLAGS = -ffreestanding -mgeneral-regs-only -O0 -Wall -fno-exceptions -fno-rtti -ggdb quark_kernel_LDFLAGS = -nostdlib diff --git a/src/module.cpp b/src/module.cpp index e327acf..c0a1eee 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -1,4 +1,4 @@ -#include "elf.hpp" +#include "module.hpp" #include "util.hpp" kernel::ELF::ELF() diff --git a/src/util.cpp b/src/util.cpp index fe13afc..2dfa310 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -64,7 +64,7 @@ int memcmp(const void* ptr1, const void* ptr2, size_t num) return 0; } -void* memset (void* ptr, int value, size_t num) +void* memset(void* ptr, int value, size_t num) { uint8_t* dest = (uint8_t*) ptr; uint8_t v = (uint8_t) value; @@ -75,6 +75,51 @@ void* memset (void* ptr, int value, size_t num) return ptr; } +int strlen(const char* str) +{ + int i = 0; + while(str[i] != '\0') + { + i++; + } + return i; +} + +char* strcpy(char* destination, const char* source) +{ + int sourceLen = strlen(source); + if((destination <= source) || (destination > (source + sourceLen))) + { + char* d = destination; + const char* s = source; + while(true) + { + *d = *s; + if(*s == '\0') + { + break; + } + else + { + s++; + d++; + } + } + } + else + { + char* d = destination + sourceLen; + const char* s = source + sourceLen; + do + { + *d = *s; + d--; + s--; + } while(d > destination); + } + return destination; +} + void __cxa_pure_virtual() { // do nothing diff --git a/src/util.hpp b/src/util.hpp index acf21d0..a16c112 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -13,4 +13,8 @@ extern "C" int memcmp(const void* ptr1, const void* ptr2, size_t num); extern "C" void* memset(void* ptr, int value, size_t num); +extern "C" int strlen(const char* str); + +extern "C" char* strcpy(char* destination, const char* source); + #endif \ No newline at end of file