-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruncmd.c
74 lines (56 loc) · 1.4 KB
/
runcmd.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
#include "runcmd.h"
int status = 0;
struct cmd *parsed_pipe;
// runs the command in 'cmd'
int
run_cmd(char *cmd, char *prompt, stack_t *signal_alt_stack, history_data_t *history_data)
{
pid_t _pid;
struct cmd *parsed;
// if the "enter" key is pressed
// just print the prompt again
if (cmd[0] == END_STRING)
return 0;
// "history" built-in call
if (history(cmd, &status, history_data))
return 0;
// "cd" built-in call
if (cd(cmd, prompt, &status))
return 0;
// "exit" built-in call
if (exit_shell(cmd, &status))
return EXIT_SHELL;
// "pwd" built-in call
if (pwd(cmd, &status))
return 0;
// parses the command line
parsed = parse_line(cmd, &status);
// forks and run the command
if ((_pid = fork()) == 0) {
// keep a reference
// to the parsed pipe cmd
// so it can be freed later
if (parsed->type == PIPE)
parsed_pipe = parsed;
if (parsed->type != BACK) {
setpgid(USE_PID_OF_THIS_PROCESS,
SET_GPID_SAME_AS_PID_OF_THIS_PROCESS);
}
exec_cmd(parsed, signal_alt_stack);
}
// stores the pid of the process
parsed->pid = _pid;
// background process special treatment
if (parsed->type == BACK) {
print_back_info(parsed);
free_command(parsed);
return 0;
}
// waits for the process to finish
waitpid(_pid, &status, NO_OPTIONS);
int exit_code = parse_exit_code(status);
print_status_info(parsed);
free_command(parsed);
status = exit_code;
return 0;
}