syscalls: chdir

This commit is contained in:
Matheus
2022-09-30 16:39:19 -03:00
parent 4afca1db07
commit 89e57f0578
2 changed files with 53 additions and 1 deletions
+3 -1
View File
@@ -23,7 +23,8 @@ extern syscall_fcntl
extern syscall_getpid
extern syscall_getdirent
extern syscall_ioctl
func_count equ 22
extern syscall_chdir
func_count equ 23
func_table:
@@ -49,6 +50,7 @@ func_table:
dq syscall_getpid
dq syscall_getdirent
dq syscall_ioctl
dq syscall_chdir
section .text
global asm_syscall_entry
+50
View File
@@ -0,0 +1,50 @@
#include <kernel/syscalls.h>
#include <kernel/vmm.h>
#include <kernel/vfs.h>
#include <errno.h>
#include <string.h>
#include <arch/cls.h>
#include <kernel/sched.h>
#include <sys/stat.h>
syscallret syscall_chdir(const char* path){
syscallret retv;
retv.ret = -1;
if(path > USER_SPACE_END){
retv.errno = EFAULT;
return retv;
}
size_t len = strlen(path);
char buff[len+1];
strcpy(buff, path);
proc_t* proc = arch_getcls()->thread->proc;
vnode_t* node;
retv.errno = vfs_open(&node, buff[0] == '/' ? proc->root : proc->cwd, buff);
if(retv.errno)
return retv;
if(GETTYPE(node->st.st_mode) != TYPE_DIR){
vfs_close(node);
retv.errno = ENOTDIR;
return retv;
}
vnode_t* old = proc->cwd;
proc->cwd = node;
vfs_close(old);
retv.ret = 0;
return retv;
}