-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilt_in.c
More file actions
51 lines (43 loc) · 931 Bytes
/
built_in.c
File metadata and controls
51 lines (43 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include "built_in.h"
#include "svec.h"
char* built_in_cmd_strings[] = {"cd", "exit"};
int (*built_in_cmds[]) (svec* tokens) = {
*nush_cd,
*nush_exit
};
int
lengthof_built_ins() {
return sizeof(built_in_cmd_strings) / sizeof(built_in_cmd_strings[0]);
}
int
get_built_in_cmd_code(char* cmd) {
int ii;
for(ii = 0; ii < lengthof_built_ins(); ++ii) {
if(strcmp(cmd, built_in_cmd_strings[ii]) == 0) {
return ii;
}
}
return -1;
}
int
exec_built_in_cmd(int code, svec* tokens) {
return built_in_cmds[code](tokens);
}
int
nush_cd(svec* tokens) {
if(tokens->data[1] == 0 || strcmp(tokens->data[1], "~") == 0) {
// change to home directory here if required
} else {
chdir(tokens->data[1]);
}
return 0;
}
int
nush_exit(svec* tokens) {
exit(0);
return 0;
}