-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin.c
54 lines (48 loc) · 1023 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
#include "builtin.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define CWD_SIZE_MAX 64
#include "argv.h"
#include "input.h"
#include "job.h"
#include "process.h"
static void do_cd(char** argv) {
if (!argv[1]) {
printf("usage: cd <path>\n");
return;
}
if (chdir(argv[1]) != 0) {
perror("cd");
return;
}
}
static void do_pwd(char** argv) {
char* buf = malloc(CWD_SIZE_MAX);
printf("%s\n", getcwd(buf, CWD_SIZE_MAX));
free(buf);
}
int execute_builtin(char** argv) {
if (!strncmp(argv[0], "cd", 3)) {
do_cd(argv);
return 0;
} else if (!strncmp(argv[0], "pwd", 4)) {
do_pwd(argv);
return 0;
} else if (!strncmp(argv[0], "jobs", 5)) {
show_jobs();
return 0;
} else if (!strncmp(argv[0], "fg", 3)) {
do_fg(argv);
return 0;
} else if (!strncmp(argv[0], "bg", 3)) {
do_bg(argv);
return 0;
} else if (!strncmp(argv[0], "exit", 5)) {
exit(0);
} else {
/* Not built-in command */
return 1;
}
}