Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/kern/syscall/chdir.c
2093 views
1
#include <types.h>
2
#include <lib.h>
3
#include <copyinout.h>
4
#include <kern/iovec.h>
5
#include <kern/fcntl.h>
6
#include <uio.h>
7
#include <vfs.h>
8
#include <thread.h>
9
#include <current.h>
10
#include <syscall.h>
11
12
#define MAX_DIR_LEN 128
13
14
int
15
sys_chdir( userptr_t ubuf ) {
16
char kbuf[MAX_DIR_LEN];
17
int err;
18
struct vnode *v_dir = NULL;
19
20
KASSERT( curthread != NULL );
21
KASSERT( curthread->td_proc != NULL );
22
23
//copy the buffer from userland into kernel land.
24
err = copyinstr( ubuf, kbuf, sizeof( kbuf ), NULL );
25
if( err )
26
return err;
27
28
//try to open the directory pointed by the path.
29
err = vfs_open( kbuf, O_RDONLY, 0644, &v_dir );
30
if( err )
31
return err;
32
33
//change the current directory.
34
err = vfs_setcurdir( v_dir );
35
36
//close the vnode.
37
vfs_close( v_dir );
38
39
if( err )
40
return err;
41
42
return 0;
43
}
44
45