-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.c
76 lines (62 loc) · 1 KB
/
execute.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "headers.h"
char *command_list[] = {
"echo",
"exit",
"touch",
"mkdir",
"rm",
"rmdir",
"pwd",
"head",
};
int (*command_func_list[]) (char **) = {
&echo_func,
&exit_func,
&touch_func,
&mkdir_func,
&rm_func,
&rmdir_func,
&pwd_func,
&head_func,
};
int execute(char **argv)
{
if (argv[0] == NULL)
{
return 1;
}
for (int i = 0; i < 8; i++)
{
if (strcmp(argv[0], command_list[i]) == 0)
{
return (*command_func_list[i])(argv);
}
}
return launch(argv);
}
int launch(char **argv)
{
pid_t pid;
int status;
pid = fork();
if (pid == 0)
{
if (execvp(argv[0], argv) == -1)
{
printf("%s: command not found\n",argv[0]);
}
exit(1);
}
else if (pid < 0)
{
return 1;
}
else
{
do
{
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}