Ver Fonte

feat(syscall): add getcwd

greatbridf há 2 anos atrás
pai
commit
5cd3d7fd69
3 ficheiros alterados com 20 adições e 0 exclusões
  1. 1 0
      gblibc/private-include/syscall.h
  2. 5 0
      gblibc/src/unistd.c
  3. 14 0
      src/kernel/syscall.cpp

+ 1 - 0
gblibc/private-include/syscall.h

@@ -13,6 +13,7 @@
 #define SYS_read (0x07)
 #define SYS_getdents (0x08)
 #define SYS_open (0x09)
+#define SYS_getcwd (0x0a)
 
 #ifdef __cplusplus
 extern "C" {

+ 5 - 0
gblibc/src/unistd.c

@@ -37,3 +37,8 @@ int chdir(const char* path)
 {
     return syscall1(SYS_chdir, (uint32_t)path);
 }
+
+char* getcwd(char* buf, size_t bufsize)
+{
+    return syscall2(SYS_getcwd, (uint32_t)buf, bufsize);
+}

+ 14 - 0
src/kernel/syscall.cpp

@@ -13,6 +13,7 @@
 #include <kernel_main.hpp>
 #include <stdint.h>
 #include <stdio.h>
+#include <string.h>
 #include <types/allocator.hpp>
 #include <types/elf.hpp>
 #include <types/status.h>
@@ -280,6 +281,18 @@ void _syscall_open(interrupt_stack* data)
     data->s_regs.eax = current_process->files.open(path, flags);
 }
 
+void _syscall_getcwd(interrupt_stack* data)
+{
+    char* buf = reinterpret_cast<char*>(data->s_regs.edi);
+    size_t bufsize = reinterpret_cast<size_t>(data->s_regs.esi);
+
+    // TODO: use copy_to_user
+    strncpy(buf, current_process->pwd.c_str(), bufsize);
+    buf[bufsize - 1] = 0;
+
+    SYSCALL_SET_RETURN_VAL_EAX(buf);
+}
+
 void init_syscall(void)
 {
     syscall_handlers[0] = _syscall_fork;
@@ -292,4 +305,5 @@ void init_syscall(void)
     syscall_handlers[7] = _syscall_read;
     syscall_handlers[8] = _syscall_getdents;
     syscall_handlers[9] = _syscall_open;
+    syscall_handlers[10] = _syscall_getcwd;
 }