From 3ae26ed88a3194b6522753e9792cfe759b03e22d Mon Sep 17 00:00:00 2001 From: Ruslan Isaev Date: Fri, 18 Jul 2025 21:02:54 +0300 Subject: [PATCH] syscalls: fix cwd-related stuff --- include/exec_from_file.h | 2 +- include/string.h | 2 ++ src/kernel/kernel.c | 2 +- src/tasking/exec_from_file.c | 10 +++++++--- src/tasking/syscalls.c | 10 ++++------ src/utils/string.c | 22 ++++++++++++++++++++++ 6 files changed, 37 insertions(+), 11 deletions(-) diff --git a/include/exec_from_file.h b/include/exec_from_file.h index 7d163ea..3eeecd9 100644 --- a/include/exec_from_file.h +++ b/include/exec_from_file.h @@ -2,4 +2,4 @@ #include "../include/elf.h" #include "../include/stdio.h" -void exec_from_file(const char* filename); \ No newline at end of file +void exec_from_file(const char* filename, const char* dirname); \ No newline at end of file diff --git a/include/string.h b/include/string.h index 45904c8..96d86a3 100644 --- a/include/string.h +++ b/include/string.h @@ -8,5 +8,7 @@ void* memcpy(void* __restrict, const void* __restrict, size_t); void* memmove(void*, const void*, size_t); void* memset(void*, int, size_t); size_t strlen(const char*); +char *strcat(char *dest, const char *src); +char *strcpy(char *dest, const char *src); #endif diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index eddcb7e..ae00450 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -218,7 +218,7 @@ void kmain(unsigned int magic, unsigned int info) printf("huy1 points to: 0x%X huy2 points to:0x%X\n", huy, huy2); printf("huy1: %s\nhuy2: %s", huy, huy2); - exec_from_file("/ebalo/forktest"); + exec_from_file("/ebalo/aye", "/ebalo/"); while(1) { diff --git a/src/tasking/exec_from_file.c b/src/tasking/exec_from_file.c index e0b0e8a..9578b58 100644 --- a/src/tasking/exec_from_file.c +++ b/src/tasking/exec_from_file.c @@ -20,7 +20,7 @@ disk_ops_t ops1 = static fat_t g_fat; -int read_file(const char* path, uint8_t** buffer) +int read_file(const char* path, uint8_t** buffer, dir_t* dir) { file_t file; int err = fat_fopen(&file, path, "r"); @@ -38,19 +38,22 @@ int read_file(const char* path, uint8_t** buffer) debug_log("eeee\n"); int cnt = fat_fread(&file, *buffer, file_size); + + *dir = file.dir; return fat_fclose(&file); } -void exec_from_file(const char* filename) +void exec_from_file(const char* filename, const char* dirname) { asm volatile("cli"); int err, cnt; err = fat_mount(&ops1, 0, &g_fat, "ebalo"); uint8_t* file; + dir_t dir; - int result = read_file(filename, &file); + int result = read_file(filename, &file, &dir); //debug_log("result: %X\n", result); //debug_log("buffer: %s\n", file); @@ -140,6 +143,7 @@ void exec_from_file(const char* filename) set_page_dir(curr_pd_phys); Process* p_task1 = task_create(elf_ehdr->e_entry, USER_STACK_TOP, 3, proc_pd); + *p_task1->cwd = dir; asm volatile("sti"); } \ No newline at end of file diff --git a/src/tasking/syscalls.c b/src/tasking/syscalls.c index b8998d8..43893e3 100644 --- a/src/tasking/syscalls.c +++ b/src/tasking/syscalls.c @@ -232,17 +232,15 @@ int sys_close(TrapFrame *tf) int sys_chdir(TrapFrame *tf) { + strcat(current->cwd->fat->path, "/"); + strcat(current->cwd->fat->path, (char*)tf->ebx); return fat_opendir(current->cwd, (const char*)tf->ebx); } int sys_getcwd(TrapFrame *tf) { - int len = tf->ecx; - - if(len >= 260) - return (int)NULL; - - memcpy((void*)tf->ebx, current->cwd->fat->name, current->cwd->fat->namelen); + strcpy((char*)tf->ebx, current->cwd->fat->path); + return tf->ebx; } diff --git a/src/utils/string.c b/src/utils/string.c index 618fa07..d60ce5d 100644 --- a/src/utils/string.c +++ b/src/utils/string.c @@ -48,3 +48,25 @@ size_t strlen(const char* str) len++; return len; } + +char *strcat(char *dest, const char *src) { + char *original_dest = dest; + + while (*dest != '\0') { + dest++; + } + + while ((*dest++ = *src++) != '\0') + ; + + return original_dest; +} + +char *strcpy(char *dest, const char *src) { + char *original_dest = dest; + + while ((*dest++ = *src++) != '\0') + ; + + return original_dest; +} \ No newline at end of file