-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltins.c
More file actions
68 lines (56 loc) · 1.06 KB
/
Copy pathbuiltins.c
File metadata and controls
68 lines (56 loc) · 1.06 KB
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
58
59
60
61
62
63
64
65
66
67
68
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int fsh_cd(char **args);
int fsh_help(char **args);
int fsh_exit(char **args);
int fsh_pwd();
char *builtin_str[] = {
"cd",
"help",
"pwd",
"exit"
};
int (*builtin_func[]) (char **) = {
&fsh_cd,
&fsh_help,
&fsh_pwd,
&fsh_exit
};
int fsh_num_builtins() {
return sizeof(builtin_str) / sizeof(char *);
}
int fsh_cd(char **args)
{
if (args[1] == NULL) {
fprintf(stderr, "fsh: expected argument to \"cd\"\n");
} else {
if (chdir(args[1]) != 0) {
perror("lsh");
}
}
return 1;
}
int fsh_help(char **args)
{
int i;
printf("KSHyst's FayeShell\n");
for (i = 0; i < fsh_num_builtins(); i++) {
printf(" %s\n", builtin_str[i]);
}
return 1;
}
int fsh_exit(char **args)
{
return 0;
}
int fsh_pwd()
{
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("current working directory: %s\n", cwd);
} else {
printf("error getting current directory");
}
return 1;
}