-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin.c
59 lines (47 loc) · 863 Bytes
/
builtin.c
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
52
53
54
55
56
57
#include "builtin.h"
#include "lib/dbg.h"
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
char *builtin_cmds[] = {
"cd",
"help",
"exit"
};
int sea_num_builtins()
{
return sizeof(builtin_cmds) / sizeof(char *);
}
int is_builtin(char *arg)
{
for (int i = 0; i < sea_num_builtins(); i++) {
if (strcmp(arg, builtin_cmds[i]) == 0) {
return i;
}
}
return -1;
}
int sea_cd(char **args)
{
// Default to the home dir if no dir is provided.
char *dir;
if (args[1] == NULL) {
dir = getenv("HOME");
} else {
dir = args[1];
}
int status = chdir(dir);
check(status == 0, "Could not change dir.");
return 1;
error:
return -1;
}
int sea_help(char **args)
{
printf("Hah, nope.");
return 0;
}
int sea_exit(char **args)
{
return 0;
}