From 344952fb5f8c1ffb39e79571c9a8b565869b1247 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Tue, 11 Nov 2025 19:54:09 +0100 Subject: [PATCH] Move string functions/utils from HAL to std/string --- kernel/FastLZ/6unpack_mem.c | 3 +- kernel/Makefile | 1 - kernel/baseimg/baseimg.c | 3 +- kernel/dev/dev.c | 3 +- kernel/dev/fbdev.c | 3 +- kernel/diskpart/mbr.c | 3 +- kernel/dlmalloc/dlmalloc_port.inc | 3 +- kernel/fs/portlfs/portlfs.c | 5 +- kernel/hal/hal.h | 11 --- kernel/hal/util.c | 152 ------------------------------ kernel/hal/x86_64/gdt.c | 3 +- kernel/hal/x86_64/vmm.c | 5 +- kernel/hshtb.h | 13 +-- kernel/ipc/pipe/pipe.c | 3 +- kernel/path/path.c | 5 +- kernel/pmm/pmm.c | 3 +- kernel/proc/proc.c | 11 ++- kernel/proc/proc.h | 3 +- kernel/std/string.c | 136 +++++++++++++++++++++++--- kernel/std/string.h | 2 + kernel/storedev/ramsd.c | 5 +- kernel/syscall/dev.c | 3 +- kernel/syscall/mman.c | 3 +- kernel/syscall/proc.c | 7 +- kernel/syscall/vfs.c | 9 +- kernel/time/time.c | 3 +- kernel/vfs/vfs.c | 27 +++--- 27 files changed, 200 insertions(+), 228 deletions(-) delete mode 100644 kernel/hal/util.c diff --git a/kernel/FastLZ/6unpack_mem.c b/kernel/FastLZ/6unpack_mem.c index dce3793..57e13f4 100644 --- a/kernel/FastLZ/6unpack_mem.c +++ b/kernel/FastLZ/6unpack_mem.c @@ -2,6 +2,7 @@ #include #include "hal/hal.h" #include "fastlz.h" +#include "std/string.h" #define SIXPACK_OK 0 #define SIXPACK_ERR_MAGIC -1 @@ -93,7 +94,7 @@ int sixpack_decompress_mem(const uint8_t *input, size_t in_size, /* Stored (uncompressed) */ if (written + size > out_cap) return SIXPACK_ERR_NO_SPACE; - hal_memcpy(output + written, chunk_data, size); + memcpy(output + written, chunk_data, size); written += size; } else if (opts == 1) { /* FastLZ compressed */ diff --git a/kernel/Makefile b/kernel/Makefile index 6680dc0..9aae158 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -46,7 +46,6 @@ SRCFILES += $(call GRABSRC, \ fs/portlfs \ baseimg \ proc \ - hal \ hal/$(ARCH) \ std \ flanterm/src \ diff --git a/kernel/baseimg/baseimg.c b/kernel/baseimg/baseimg.c index 1058dc6..0e3a077 100644 --- a/kernel/baseimg/baseimg.c +++ b/kernel/baseimg/baseimg.c @@ -9,6 +9,7 @@ #include "dlmalloc/malloc.h" #include "FastLZ/fastlz.h" #include "FastLZ/6unpack_mem.h" +#include "std/string.h" #define BASEIMG_DECOMPRESSED (1024*1024*4) @@ -29,7 +30,7 @@ void baseimg_init(void) { LOG("baseimg", "looking for base image...\n"); for (size_t i = 0; i < BOOT_INFO.modules->module_count; i++) { struct limine_file *module = BOOT_INFO.modules->modules[i]; - if (hal_strcmp(util_get_filename(module->path), "base.img.6pk") == 0) { + if (strcmp(util_get_filename(module->path), "base.img.6pk") == 0) { baseimg = module; break; } diff --git a/kernel/dev/dev.c b/kernel/dev/dev.c index 3dafe2c..a4c14ac 100644 --- a/kernel/dev/dev.c +++ b/kernel/dev/dev.c @@ -4,6 +4,7 @@ #include "dev.h" #include "hshtb.h" #include "hal/hal.h" +#include "std/string.h" #include "termdev.h" #include "ps2kbdev.h" #include "serialdev.h" @@ -12,7 +13,7 @@ DevTable DEVTABLE; void dev_init(void) { - hal_memset(&DEVTABLE, 0, sizeof(DEVTABLE)); + memset(&DEVTABLE, 0, sizeof(DEVTABLE)); spinlock_init(&DEVTABLE.spinlock); termdev_init(); diff --git a/kernel/dev/fbdev.c b/kernel/dev/fbdev.c index 97009e6..8cfa676 100644 --- a/kernel/dev/fbdev.c +++ b/kernel/dev/fbdev.c @@ -9,6 +9,7 @@ #include "hal/hal.h" #include "bootinfo/bootinfo.h" #include "errors.h" +#include "std/string.h" int32_t fbdev_getinfo(struct Dev *dev, uint8_t *buffer, size_t len, uint64_t pid) { (void)dev; (void)pid; @@ -24,7 +25,7 @@ int32_t fbdev_getinfo(struct Dev *dev, uint8_t *buffer, size_t len, uint64_t pid .fontw = 8, .fonth = 16, }; - hal_memcpy(buffer, &info, sizeof(info)); + memcpy(buffer, &info, sizeof(info)); return E_OK; } diff --git a/kernel/diskpart/mbr.c b/kernel/diskpart/mbr.c index 7a75d8d..b880624 100644 --- a/kernel/diskpart/mbr.c +++ b/kernel/diskpart/mbr.c @@ -10,6 +10,7 @@ #include "hal/hal.h" #include "util/util.h" #include "dev/dev.h" +#include "std/string.h" typedef struct { uint8_t driveattrs; @@ -62,7 +63,7 @@ bool diskpart_mbr_probe(StoreDev *sd) { LOG("diskpart", "probing for MBR\n"); MBR mbr; - hal_memset(&mbr, 0, sizeof(mbr)); + memset(&mbr, 0, sizeof(mbr)); sd->read(sd, (uint8_t *const)&mbr, 0, 0, sizeof(mbr)); if (!(mbr.validsign[0] == 0x55 && mbr.validsign[1] == 0xAA)) { diff --git a/kernel/dlmalloc/dlmalloc_port.inc b/kernel/dlmalloc/dlmalloc_port.inc index 5c670cb..3fd1fc5 100644 --- a/kernel/dlmalloc/dlmalloc_port.inc +++ b/kernel/dlmalloc/dlmalloc_port.inc @@ -9,6 +9,7 @@ #include "pmm/pmm.h" #include "malloc.h" #include "bootinfo/bootinfo.h" +#include "std/string.h" #define USE_DL_PREFIX 1 #define LACKS_SYS_TYPES_H 1 @@ -67,7 +68,7 @@ void *sbrk(long inc) { uint64_t blocks = _DIV_ROUNDUP(inc, BITMAP_BLOCK_SIZE); uint8_t *virt = VIRT(pmm_alloc(blocks)); - hal_memset(virt, 0, blocks * BITMAP_BLOCK_SIZE); + memset(virt, 0, blocks * BITMAP_BLOCK_SIZE); _last = (void *)(virt + inc); return virt; } diff --git a/kernel/fs/portlfs/portlfs.c b/kernel/fs/portlfs/portlfs.c index a375ed0..e75e4be 100644 --- a/kernel/fs/portlfs/portlfs.c +++ b/kernel/fs/portlfs/portlfs.c @@ -6,6 +6,7 @@ #include "kprintf.h" #include "dlmalloc/malloc.h" #include "hal/hal.h" +#include "std/string.h" int32_t littlefs_cleanup(struct VfsMountPoint *vmp) { dlfree((void *)vmp->fs.littlefs.instance.cfg); @@ -132,7 +133,7 @@ struct VfsObj *littlefs_open(struct VfsMountPoint *vmp, const char *path, uint32 if (vobj == NULL) { return NULL; } - hal_memset(vobj, 0, sizeof(*vobj)); + memset(vobj, 0, sizeof(*vobj)); spinlock_init(&vobj->spinlock); int lfs_flags = 0; @@ -214,7 +215,7 @@ int32_t littlefs_fetchdirent(struct VfsMountPoint *vmp, const char *path, FsDire direntbuf->stat.type = FSSTAT_DIR; } - hal_memcpy(direntbuf->name, entinfo.name, sizeof(direntbuf->name)); + memcpy(direntbuf->name, entinfo.name, sizeof(direntbuf->name)); break; } i++; diff --git a/kernel/hal/hal.h b/kernel/hal/hal.h index 6bb5f22..e75ac36 100644 --- a/kernel/hal/hal.h +++ b/kernel/hal/hal.h @@ -9,17 +9,6 @@ __attribute__((noreturn)) void hal_hang(void); void hal_init(void); void hal_intr_disable(void); void hal_intr_enable(void); -void *hal_memset(void *p, int c, size_t n); -void *hal_memcpy(void *dst, const void *src, size_t n); -size_t hal_strlen(char *s); -int hal_memcmp(const void *s1, const void *s2, int len); -int hal_strcmp(const char *a, const char *b); -size_t hal_strcspn(const char *s, const char *reject); -size_t hal_strspn(const char *s, const char *accept); -char *hal_strcpy(char *dest, const char *src); -char *hal_strchr(const char *s, int c); -char *hal_strstr(const char *str, const char *substring); -char *hal_string_combine(char *dest, const char *src); void hal_wait(uint32_t ms); int32_t hal_randnum(void); diff --git a/kernel/hal/util.c b/kernel/hal/util.c deleted file mode 100644 index c86aa7f..0000000 --- a/kernel/hal/util.c +++ /dev/null @@ -1,152 +0,0 @@ -#include -#include -#include "hal.h" - -void *hal_memset(void *p, int c, size_t n) { - char *cp = p; - for (size_t i = 0; i < n; i++) cp[i] = c; - return p; -} - -void *hal_memcpy(void *dst, const void *src, size_t n) { - char *a = dst; - const char *b = src; - for (size_t i = 0; i < n; i++) a[i] = b[i]; - return dst; -} - -size_t hal_strlen(char *s) { - char *s2; - for (s2 = s; *s2; ++s2); - return (s2 - s); -} - -// https://aticleworld.com/memcmp-in-c/ -int hal_memcmp(const void *s1, const void *s2, int len) -{ - unsigned char *p = (unsigned char *)s1; - unsigned char *q = (unsigned char *)s2; - int charCompareStatus = 0; - //If both pointer pointing same memory block - if (s1 == s2) - { - return charCompareStatus; - } - while (len > 0) - { - if (*p != *q) - { //compare the mismatching character - charCompareStatus = (*p >*q)?1:-1; - break; - } - len--; - p++; - q++; - } - return charCompareStatus; -} - -int hal_strcmp(const char *a, const char *b) { - while (*a && (*a == *b)) { - a++, b++; - } - return (unsigned char)*a - (unsigned char)*b; -} - -size_t hal_strcspn(const char *s, const char *reject) { - size_t count = 0; - for (; *s != '\0'; ++s) { - const char *r = reject; - while (*r != '\0') { - if (*s == *r) { - return count; - } - r++; - } - count++; - } - return count; -} - -size_t hal_strspn(const char *s, const char *accept) { - size_t count = 0; - for (; *s != '\0'; ++s) { - const char *a = accept; - int matched = 0; - while (*a != '\0') { - if (*s == *a) { - matched = 1; - break; - } - a++; - } - if (!matched) { - return count; - } - count++; - } - return count; -} - -char *hal_strcpy(char *dest, const char *src) { - char *d = dest; - while ((*d++ = *src++) != '\0') { - ; - } - return dest; -} - -char *hal_strchr(const char *s, int c) { - char ch = (char)c; - while (*s != '\0') { - if (*s == ch) { - return (char *)s; - } - s++; - } - if (ch == '\0') { - return (char *)s; - } - return NULL; -} - -char *hal_strstr(const char *str, const char *substring) -{ - const char *a; - const char *b; - - b = substring; - - if (*b == 0) { - return (char *) str; - } - - for ( ; *str != 0; str += 1) { - if (*str != *b) { - continue; - } - - a = str; - while (1) { - if (*b == 0) { - return (char *) str; - } - if (*a++ != *b++) { - break; - } - } - b = substring; - } - - return NULL; -} - -char *hal_string_combine(char *dest, const char *src) { - size_t i, j; - for(i = 0; dest[i] != '\0'; i++); - for(j = 0; src[j] != '\0'; j++) { - dest[i+j] = src[j]; - } - dest[i+j] = '\0'; - return dest; -} diff --git a/kernel/hal/x86_64/gdt.c b/kernel/hal/x86_64/gdt.c index e69c7e8..2aadce5 100644 --- a/kernel/hal/x86_64/gdt.c +++ b/kernel/hal/x86_64/gdt.c @@ -3,6 +3,7 @@ #include "compiler/attr.h" #include "hal/hal.h" #include "gdt.h" +#include "std/string.h" #define GDT_PRESENT 0x80 #define GDT_TSS 0x89 @@ -43,7 +44,7 @@ void gdt_setentry(GdtEntry *ent, uint32_t base, uint32_t limit, uint8_t acc, uin } void gdt_init(void) { - hal_memset(&tss, 0, sizeof(tss)); + memset(&tss, 0, sizeof(tss)); tss.iopb_off = sizeof(tss); tss.rsp0 = (uint64_t)(kernelstack + sizeof(kernelstack)); diff --git a/kernel/hal/x86_64/vmm.c b/kernel/hal/x86_64/vmm.c index a4fef5f..e1b31c3 100644 --- a/kernel/hal/x86_64/vmm.c +++ b/kernel/hal/x86_64/vmm.c @@ -8,6 +8,7 @@ #include "proc/proc.h" #include "kprintf.h" #include "spinlock/spinlock.h" +#include "std/string.h" uint64_t KERNEL_CR3 = 0; SpinLock spinlock; @@ -41,7 +42,7 @@ uint64_t *hal_vmm_nexttable(uint64_t *table, uint64_t ent, bool alloc) { uint8_t *newphys = pmm_alloc(1); phys = (uint64_t)newphys; - hal_memset(VIRT(phys), 0, HAL_PAGE_SIZE); + memset(VIRT(phys), 0, HAL_PAGE_SIZE); table[ent] = phys | HAL_PG_USER | HAL_PG_RW | HAL_PG_PRESENT; } return (uint64_t *)((uint8_t *)VIRT(phys)); @@ -112,7 +113,7 @@ void hal_vmm_map_kern(uint64_t targetcr3) { uint64_t hal_vmm_userproc_pml4_phys(void) { uint8_t *cr3phys = pmm_alloc(1); uint64_t phys = (uint64_t)cr3phys; - hal_memset(VIRT(phys), 0, HAL_PAGE_SIZE); + memset(VIRT(phys), 0, HAL_PAGE_SIZE); uint64_t *kcr3 = (uint64_t *)VIRT(KERNEL_CR3); uint64_t *pml4 = (uint64_t *)VIRT(phys); diff --git a/kernel/hshtb.h b/kernel/hshtb.h index 4c36fcf..2db9394 100644 --- a/kernel/hshtb.h +++ b/kernel/hshtb.h @@ -6,6 +6,7 @@ #include #include "hal/hal.h" #include "util/util.h" +#include "std/string.h" #define HSHTB_FNV32_OFF 0x811c9dc5u #define HSHTB_FNV32_PRIME 0x01000193u @@ -32,7 +33,7 @@ enum { #define HSHTB_ALLOC(tb, keyfield, k, out) \ do { \ size_t __len = __HSHTB_ARRAY_LEN(tb); \ - uint32_t __h = hshtb_fnv32((k), hal_strlen((k))); \ + uint32_t __h = hshtb_fnv32((k), strlen((k))); \ size_t __idx = __h % __len; \ size_t __start = __idx; \ typeof(&(tb)[0]) __tomb = NULL; \ @@ -40,13 +41,13 @@ enum { do { \ if ((tb)[__idx]._hshtbstate == HSHTB_EMPTY) { \ typeof(&(tb)[0]) __slot = __tomb ? __tomb : &(tb)[__idx]; \ - hal_memset(__slot, 0, sizeof(*__slot)); \ + memset(__slot, 0, sizeof(*__slot)); \ __slot->_hshtbstate = HSHTB_TAKEN; \ - hal_memcpy(__slot->keyfield, (k), MIN(sizeof(__slot->keyfield) - 1, hal_strlen((k)))); \ + memcpy(__slot->keyfield, (k), MIN(sizeof(__slot->keyfield) - 1, strlen((k)))); \ (out) = __slot; \ break; \ } \ - if ((tb)[__idx]._hshtbstate == HSHTB_TAKEN && hal_strcmp((tb)[__idx].keyfield, (k)) == 0) { \ + if ((tb)[__idx]._hshtbstate == HSHTB_TAKEN && strcmp((tb)[__idx].keyfield, (k)) == 0) { \ (out) = &(tb)[__idx]; \ break; \ } \ @@ -60,7 +61,7 @@ enum { #define HSHTB_GET(tb, keyfield, k, out) \ do { \ size_t __len = __HSHTB_ARRAY_LEN(tb); \ - uint32_t __h = hshtb_fnv32((k), hal_strlen((k))); \ + uint32_t __h = hshtb_fnv32((k), strlen((k))); \ size_t __idx = __h % __len; \ size_t __start = __idx; \ (out) = NULL; \ @@ -68,7 +69,7 @@ enum { if ((tb)[__idx]._hshtbstate == HSHTB_EMPTY) { \ break; \ } \ - if ((tb)[__idx]._hshtbstate == HSHTB_TAKEN && hal_strcmp((tb)[__idx].keyfield, (k)) == 0) { \ + if ((tb)[__idx]._hshtbstate == HSHTB_TAKEN && strcmp((tb)[__idx].keyfield, (k)) == 0) { \ (out) = &(tb)[__idx]; \ break; \ } \ diff --git a/kernel/ipc/pipe/pipe.c b/kernel/ipc/pipe/pipe.c index ab19bca..8766020 100644 --- a/kernel/ipc/pipe/pipe.c +++ b/kernel/ipc/pipe/pipe.c @@ -7,9 +7,10 @@ #include "errors.h" #include "pipe.h" #include "kprintf.h" +#include "std/string.h" int32_t ipc_pipeinit(IpcPipe *pipe, uint64_t pid) { - hal_memset(pipe, 0, sizeof(*pipe)); + memset(pipe, 0, sizeof(*pipe)); spinlock_init(&pipe->spinlock); pipe->ownerpid = pid; diff --git a/kernel/path/path.c b/kernel/path/path.c index 973f9f2..6c70ab6 100644 --- a/kernel/path/path.c +++ b/kernel/path/path.c @@ -1,5 +1,6 @@ #include "path.h" #include "hal/hal.h" +#include "std/string.h" void path_parse(const char *in, char *mp, char *path) { if (in == 0 || *in == 0) { @@ -45,8 +46,8 @@ void path_parse(const char *in, char *mp, char *path) { return; } - if (hal_strstr(path, "/../") || hal_strstr(path, "/./") - || hal_strcmp(path, "..") == 0 || hal_strcmp(path, ".") == 0) { + if (strstr(path, "/../") || strstr(path, "/./") + || strcmp(path, "..") == 0 || strcmp(path, ".") == 0) { mp[0] = 0; path[0] = 0; return; diff --git a/kernel/pmm/pmm.c b/kernel/pmm/pmm.c index 1f44a13..7f66639 100644 --- a/kernel/pmm/pmm.c +++ b/kernel/pmm/pmm.c @@ -7,6 +7,7 @@ #include "spinlock/spinlock.h" #include "util/util.h" #include "hal/hal.h" +#include "std/string.h" PhysMem PHYS_MEM; @@ -37,7 +38,7 @@ void pmm_init(void) { size_t physbegin = memmap_ent->base; bm->map = (uint8_t *)(physbegin + BOOT_INFO.hhdm_off); - hal_memset(bm->map, 0xff, bm->nbytes); + memset(bm->map, 0xff, bm->nbytes); for (size_t i = 0; i < BOOT_INFO.memmap_entrycount; i++) { struct limine_memmap_entry *entry = BOOT_INFO.memmap_entries[i]; if (entry == memmap_ent) { diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c index 22aecdc..d62e77d 100644 --- a/kernel/proc/proc.c +++ b/kernel/proc/proc.c @@ -16,6 +16,7 @@ #include "sysdefs/proc.h" #include "sysdefs/fs.h" #include "time/time.h" +#include "std/string.h" #define PROC_REAPER_FREQ 30 @@ -53,8 +54,8 @@ ElfAuxval proc_load_elf_segs(Proc *proc, uint8_t *data) { uint8_t *physaddr = pmm_alloc(blocks); uint8_t *virtaddr = (uint8_t *)(phdr->p_vaddr & ~(HAL_PAGE_SIZE - 1)); - hal_memset(VIRT(physaddr), 0, blocks * HAL_PAGE_SIZE); - hal_memcpy(VIRT(physaddr) + off, (data + phdr->p_offset), phdr->p_filesz); + memset(VIRT(physaddr), 0, blocks * HAL_PAGE_SIZE); + memcpy(VIRT(physaddr) + off, (data + phdr->p_offset), phdr->p_filesz); uint32_t pgflags = HAL_PG_USER | HAL_PG_PRESENT; if (phdr->p_flags & PF_W) { @@ -105,10 +106,10 @@ Proc *proc_spawnuser(char *mountpoint, char *path) { vfs_close(vobj); Proc *proc = dlmalloc(sizeof(*proc)); - hal_memset(proc, 0, sizeof(*proc)); + memset(proc, 0, sizeof(*proc)); ksprintf(proc->name, "%s:%s", mountpoint, path); - hal_memset(&proc->platformdata.trapframe, 0, sizeof(proc->platformdata.trapframe)); + memset(&proc->platformdata.trapframe, 0, sizeof(proc->platformdata.trapframe)); proc->platformdata.cr3 = hal_vmm_userproc_pml4_phys(); uint8_t *kstackp = (uint8_t *)pmm_alloc(PROC_STACKBLOCKS) + PROC_STACKSIZE; @@ -223,7 +224,7 @@ void proc_sched(void *cpustate) { hal_intr_disable(); sched_ticks++; - hal_memcpy(&PROCS.current->platformdata.trapframe, cpustate, sizeof(IntrStackFrame)); + memcpy(&PROCS.current->platformdata.trapframe, cpustate, sizeof(IntrStackFrame)); PROCS.current = proc_nextready(); diff --git a/kernel/proc/proc.h b/kernel/proc/proc.h index 468394f..870c02e 100644 --- a/kernel/proc/proc.h +++ b/kernel/proc/proc.h @@ -10,6 +10,7 @@ #include "sysdefs/proc.h" #include "sysdefs/time.h" #include "dev/dev.h" +#include "std/string.h" #define PROC_NAME_MAX 0x100 @@ -90,7 +91,7 @@ void proc_kill(Proc *proc); #define PROC_ARG(proc, str) \ do { \ ProcArg *__arg = dlmalloc(sizeof(*__arg)); \ - hal_strcpy(__arg->string, (str)); \ + strcpy(__arg->string, (str)); \ LL_APPEND((proc)->procargs.list, __arg); \ (proc)->procargs.len++; \ } while(0) diff --git a/kernel/std/string.c b/kernel/std/string.c index 5c833b2..778d797 100644 --- a/kernel/std/string.c +++ b/kernel/std/string.c @@ -1,39 +1,151 @@ #include -#include "hal/hal.h" +#include void *memset(void *p, int c, size_t n) { - return hal_memset(p,c,n); + char *cp = p; + for (size_t i = 0; i < n; i++) cp[i] = c; + return p; } void *memcpy(void *dst, const void *src, size_t n) { - return hal_memcpy(dst,src,n); + char *a = dst; + const char *b = src; + for (size_t i = 0; i < n; i++) a[i] = b[i]; + return dst; } size_t strlen(char *s) { - return hal_strlen(s); + char *s2; + for (s2 = s; *s2; ++s2); + return (s2 - s); +} + +// https://aticleworld.com/memcmp-in-c/ +int memcmp(const void *s1, const void *s2, int len) +{ + unsigned char *p = (unsigned char *)s1; + unsigned char *q = (unsigned char *)s2; + int charCompareStatus = 0; + //If both pointer pointing same memory block + if (s1 == s2) + { + return charCompareStatus; + } + while (len > 0) + { + if (*p != *q) + { //compare the mismatching character + charCompareStatus = (*p >*q)?1:-1; + break; + } + len--; + p++; + q++; + } + return charCompareStatus; } int strcmp(const char *a, const char *b) { - return hal_strcmp(a, b); + while (*a && (*a == *b)) { + a++, b++; + } + return (unsigned char)*a - (unsigned char)*b; } size_t strcspn(const char *s, const char *reject) { - return hal_strcspn(s, reject); + size_t count = 0; + for (; *s != '\0'; ++s) { + const char *r = reject; + while (*r != '\0') { + if (*s == *r) { + return count; + } + r++; + } + count++; + } + return count; } size_t strspn(const char *s, const char *accept) { - return hal_strspn(s, accept); + size_t count = 0; + for (; *s != '\0'; ++s) { + const char *a = accept; + int matched = 0; + while (*a != '\0') { + if (*s == *a) { + matched = 1; + break; + } + a++; + } + if (!matched) { + return count; + } + count++; + } + return count; } - char *strcpy(char *dest, const char *src) { - return hal_strcpy(dest, src); + char *d = dest; + while ((*d++ = *src++) != '\0') { + ; + } + return dest; } char *strchr(const char *s, int c) { - return hal_strchr(s, c); + char ch = (char)c; + while (*s != '\0') { + if (*s == ch) { + return (char *)s; + } + s++; + } + if (ch == '\0') { + return (char *)s; + } + return NULL; } -int memcmp(const void *s1, const void *s2, int len) { - return hal_memcmp(s1, s2, len); +char *strstr(const char *str, const char *substring) +{ + const char *a; + const char *b; + + b = substring; + + if (*b == 0) { + return (char *) str; + } + + for ( ; *str != 0; str += 1) { + if (*str != *b) { + continue; + } + + a = str; + while (1) { + if (*b == 0) { + return (char *) str; + } + if (*a++ != *b++) { + break; + } + } + b = substring; + } + + return NULL; +} + +char *strcat(char *dest, const char *src) { + size_t i, j; + for(i = 0; dest[i] != '\0'; i++); + for(j = 0; src[j] != '\0'; j++) { + dest[i+j] = src[j]; + } + dest[i+j] = '\0'; + return dest; } diff --git a/kernel/std/string.h b/kernel/std/string.h index 4315b8c..4314800 100644 --- a/kernel/std/string.h +++ b/kernel/std/string.h @@ -12,5 +12,7 @@ size_t strspn(const char *s, const char *accept); char *strcpy(char *dest, const char *src); char *strchr(const char *s, int c); int memcmp(const void *s1, const void *s2, int len); +char *strstr(const char *str, const char *substring); +char *strcat(char *dest, const char *src); #endif // STD_STRING_H_ diff --git a/kernel/storedev/ramsd.c b/kernel/storedev/ramsd.c index 89ca8cc..252a77b 100644 --- a/kernel/storedev/ramsd.c +++ b/kernel/storedev/ramsd.c @@ -8,6 +8,7 @@ #include "hal/hal.h" #include "util/util.h" #include "kprintf.h" +#include "std/string.h" int32_t ramsd_init(struct StoreDev *sd, void *extra) { RamSdInitExtra *e = extra; @@ -26,7 +27,7 @@ int32_t ramsd_init(struct StoreDev *sd, void *extra) { int32_t ramsd_read(struct StoreDev *sd, uint8_t *const buffer, ptrdiff_t sector, ptrdiff_t off, size_t size) { RamSd *ramsd = &sd->sd.ramsd; spinlock_acquire(&sd->spinlock); - hal_memcpy(buffer, ramsd->buffer + (sector * sd->sectorsize + off), size); + memcpy(buffer, ramsd->buffer + (sector * sd->sectorsize + off), size); spinlock_release(&sd->spinlock); return E_OK; } @@ -34,7 +35,7 @@ int32_t ramsd_read(struct StoreDev *sd, uint8_t *const buffer, ptrdiff_t sector, int32_t ramsd_write(struct StoreDev *sd, const uint8_t *const buffer, ptrdiff_t sector, ptrdiff_t off, size_t size) { RamSd *ramsd = &sd->sd.ramsd; spinlock_acquire(&sd->spinlock); - hal_memcpy(ramsd->buffer + (sector * sd->sectorsize + off), buffer, size); + memcpy(ramsd->buffer + (sector * sd->sectorsize + off), buffer, size); spinlock_release(&sd->spinlock); return E_OK; } diff --git a/kernel/syscall/dev.c b/kernel/syscall/dev.c index b636fc7..fbbd437 100644 --- a/kernel/syscall/dev.c +++ b/kernel/syscall/dev.c @@ -9,6 +9,7 @@ #include "util/util.h" #include "hshtb.h" #include "dev/dev.h" +#include "std/string.h" int32_t SYSCALL2(sys_dev_gethandle, dev1, devname1) { uint64_t *devh = (uint64_t *)dev1; @@ -87,7 +88,7 @@ int32_t SYSCALL2(sys_dev_stat, devstat1, idx1) { for (size_t i = 0; i < LEN(DEVTABLE.devs); i++) { if (i == idx && DEVTABLE.devs[i]._hshtbstate == HSHTB_TAKEN) { Dev *dev = &DEVTABLE.devs[i]; - hal_memcpy(devstat->name, dev->ident, sizeof(dev->ident)); + memcpy(devstat->name, dev->ident, sizeof(dev->ident)); for (size_t j = 0; j < DEV_FNS_MAX; j++) { if (dev->fns[j] != NULL) { devstat->nfns++; diff --git a/kernel/syscall/mman.c b/kernel/syscall/mman.c index 5968b33..672f59e 100644 --- a/kernel/syscall/mman.c +++ b/kernel/syscall/mman.c @@ -11,6 +11,7 @@ #include "bootinfo/bootinfo.h" #include "dlmalloc/malloc.h" #include "kprintf.h" +#include "std/string.h" int32_t SYSCALL5(sys_mman_map, addr1, size1, prot1, flags1, out1) { uint8_t *addr = (uint8_t *)addr1; @@ -28,7 +29,7 @@ int32_t SYSCALL5(sys_mman_map, addr1, size1, prot1, flags1, out1) { size_t pages = _DIV_ROUNDUP(size, HAL_PAGE_SIZE); uint8_t *phys = (uint8_t *)pmm_alloc(pages); - hal_memset(VIRT(phys), 0, pages * HAL_PAGE_SIZE); + memset(VIRT(phys), 0, pages * HAL_PAGE_SIZE); spinlock_acquire(&PROCS.spinlock); Proc *proc = NULL; diff --git a/kernel/syscall/proc.c b/kernel/syscall/proc.c index 18dd392..61390cf 100644 --- a/kernel/syscall/proc.c +++ b/kernel/syscall/proc.c @@ -11,6 +11,7 @@ #include "kprintf.h" #include "dlmalloc/malloc.h" #include "ipc/pipe/pipe.h" +#include "std/string.h" #define _MP_MAX 0xff #define _PATH_MAX VFS_PATH_MAX @@ -178,7 +179,7 @@ int32_t SYSCALL4(sys_proc_argv, pid1, argslen1, argbuf1, maxargs1) { ret = E_INVALIDARGUMENT; goto done; } - hal_strcpy(argbuf[i], arg->string); + strcpy(argbuf[i], arg->string); } *argslen = i; @@ -214,7 +215,7 @@ int32_t SYSCALL2(sys_proc_stat, pidx, pstat1) { LL_FOREACH_SAFE_IDX(PROCS.procs, p, ptmp, i) { if (i == pidx) { stat->pid = p->pid; - hal_strcpy(stat->name, p->name); + strcpy(stat->name, p->name); stat->state = p->state; VasRange *vas, *vastmp; @@ -222,7 +223,7 @@ int32_t SYSCALL2(sys_proc_stat, pidx, pstat1) { stat->usemem += vas->size; } - hal_memcpy(&stat->time, &p->time, sizeof(p->time)); + memcpy(&stat->time, &p->time, sizeof(p->time)); break; } } diff --git a/kernel/syscall/vfs.c b/kernel/syscall/vfs.c index 43634ad..3f03fb8 100644 --- a/kernel/syscall/vfs.c +++ b/kernel/syscall/vfs.c @@ -14,6 +14,7 @@ #include "storedev/storedev.h" #include "util/util.h" #include "hshtb.h" +#include "std/string.h" int32_t SYSCALL4(sys_vfsmount, mountpoint1, fstypestr1, devid1, format1) { int32_t ret = E_OK; @@ -30,7 +31,7 @@ int32_t SYSCALL4(sys_vfsmount, mountpoint1, fstypestr1, devid1, format1) { int32_t fstype; - if (hal_strcmp(fstypestr, "LittleFS") == 0) { + if (strcmp(fstypestr, "LittleFS") == 0) { fstype = VFS_LITTLEFS; } else { ret = E_INVALIDARGUMENT; @@ -76,7 +77,7 @@ int32_t SYSCALL1(sys_vfsavailmounts, availmounts1) { for (size_t i = 0; i < VFS_MOUNTPOINTS_MAX; i++) { VfsMountPoint *vmp = &VFS_TABLE.mountpoints[i]; if (vmp->_hshtbstate == HSHTB_TAKEN) { - hal_memcpy(availmounts->labels[i], vmp->label, VFS_MOUNTPOINT_LABEL_MAX); + memcpy(availmounts->labels[i], vmp->label, VFS_MOUNTPOINT_LABEL_MAX); availmounts->fieldavail[i] = 1; } } @@ -105,7 +106,7 @@ int32_t SYSCALL2(sys_vfsmountstat, statbuf1, label1) { return E_NOENTRY; } - hal_memcpy(statbuf->label, vmp->label, sizeof(statbuf->label)); + memcpy(statbuf->label, vmp->label, sizeof(statbuf->label)); statbuf->fstype = vmp->fstype; StoreDev *vmpsd = vmp->backingsd; @@ -113,7 +114,7 @@ int32_t SYSCALL2(sys_vfsmountstat, statbuf1, label1) { for (size_t i = 0; i < LEN(DEVTABLE.devs); i++) { Dev *dev = &DEVTABLE.devs[i]; if (dev->extra != NULL && ((StoreDev *)dev->extra)->_magic == STOREDEV_MAGIC && vmpsd == dev->extra) { - hal_memcpy(statbuf->devname, dev->ident, sizeof(statbuf->devname)); + memcpy(statbuf->devname, dev->ident, sizeof(statbuf->devname)); break; } } diff --git a/kernel/time/time.c b/kernel/time/time.c index ea792c8..6eff46d 100644 --- a/kernel/time/time.c +++ b/kernel/time/time.c @@ -2,6 +2,7 @@ #include #include "time.h" #include "hal/hal.h" +#include "std/string.h" #define CMOS_PORT 0x70 #define CMOS_RETURN 0x71 @@ -48,7 +49,7 @@ void time_get(Time *time) { continue; } time_fill(&t2); - if (hal_memcmp(&t1, &t2, sizeof(t1)) == 0) { + if (memcmp(&t1, &t2, sizeof(t1)) == 0) { break; } } diff --git a/kernel/vfs/vfs.c b/kernel/vfs/vfs.c index b357484..0018447 100644 --- a/kernel/vfs/vfs.c +++ b/kernel/vfs/vfs.c @@ -12,6 +12,7 @@ #include "storedev/storedev.h" #include "baseimg/baseimg.h" #include "dlmalloc/malloc.h" +#include "std/string.h" VfsTable VFS_TABLE; VfsBusyObjs VFS_BUSY_VOBJS; @@ -19,16 +20,16 @@ VfsBusyObjs VFS_BUSY_VOBJS; #define CHECK_BUSY_VFSOBJ() \ char *tmpbuf = dlmalloc(VFS_MOUNTPOINT_LABEL_MAX+1+VFS_PATH_MAX); \ \ - hal_memset(tmpbuf, 0, VFS_MOUNTPOINT_LABEL_MAX+1+VFS_PATH_MAX); \ - hal_string_combine(tmpbuf, mountpoint); \ - hal_string_combine(tmpbuf, ":"); \ - hal_string_combine(tmpbuf, path); \ + memset(tmpbuf, 0, VFS_MOUNTPOINT_LABEL_MAX+1+VFS_PATH_MAX); \ + strcat(tmpbuf, mountpoint); \ + strcat(tmpbuf, ":"); \ + strcat(tmpbuf, path); \ \ bool busy = false; \ VfsObj *vobj, *vobjtmp; \ spinlock_acquire(&VFS_BUSY_VOBJS.spinlock); \ LL_FOREACH_SAFE(VFS_BUSY_VOBJS.vobjs, vobj, vobjtmp) { \ - if (hal_strcmp(vobj->path, tmpbuf) == 0) { \ + if (strcmp(vobj->path, tmpbuf) == 0) { \ busy = true; \ break; \ } \ @@ -39,7 +40,7 @@ VfsBusyObjs VFS_BUSY_VOBJS; void vfs_init_littlefs(VfsMountPoint *mp, bool format) { struct lfs_config *cfg = dlmalloc(sizeof(*cfg)); - hal_memset(cfg, 0, sizeof(*cfg)); + memset(cfg, 0, sizeof(*cfg)); cfg->context = mp; cfg->read = &portlfs_read; cfg->prog = &portlfs_prog; @@ -147,7 +148,7 @@ int32_t vfs_mount(char *mountpoint, int32_t fstype, StoreDev *backingsd, bool fo return E_NOMEMORY; } - hal_memcpy(mp->label, mountpoint, hal_strlen(mountpoint)); + memcpy(mp->label, mountpoint, strlen(mountpoint)); mp->backingsd = backingsd; mp->fstype = fstype; switch (fstype) { @@ -178,7 +179,7 @@ int32_t vfs_unmount(char *mountpoint) { spinlock_release(&mp->spinlock); return err; } - hal_memset(mp, 0, sizeof(*mp)); + memset(mp, 0, sizeof(*mp)); spinlock_release(&mp->spinlock); return E_OK; } @@ -205,9 +206,9 @@ VfsObj *vfs_open(char *mountpoint, const char *path, uint32_t flags) { return NULL; } - hal_string_combine(vobj1->path, mountpoint); - hal_string_combine(vobj1->path, ":"); - hal_string_combine(vobj1->path, path); + strcat(vobj1->path, mountpoint); + strcat(vobj1->path, ":"); + strcat(vobj1->path, path); spinlock_acquire(&VFS_BUSY_VOBJS.spinlock); LL_APPEND(VFS_BUSY_VOBJS.vobjs, vobj1); @@ -224,9 +225,9 @@ void vfs_close(VfsObj *vobj) { } void vfs_init(void) { - hal_memset(&VFS_TABLE, 0, sizeof(VFS_TABLE)); + memset(&VFS_TABLE, 0, sizeof(VFS_TABLE)); spinlock_init(&VFS_TABLE.spinlock); - hal_memset(&VFS_BUSY_VOBJS, 0, sizeof(VFS_BUSY_VOBJS)); + memset(&VFS_BUSY_VOBJS, 0, sizeof(VFS_BUSY_VOBJS)); spinlock_init(&VFS_BUSY_VOBJS.spinlock); {