-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
154 lines (139 loc) · 3.29 KB
/
shell.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "shell.h"
#define TRUE 1
#define MAX_CHILD 10
#define MAX_PROMPT 256
pid_t child_pid[MAX_CHILD];
void sig_handler()
{
pid_t childpid;
int i;
for(i = 0; i < MAX_CHILD; ++i)
{
childpid = waitpid(child_pid[i], NULL, WNOHANG);
if (childpid > 0) {
printf("process : %u exited\n", childpid);
child_pid[i] = 0;
}
else if (childpid < 0) {
if (errno != ECHILD)
printf("waitpid error");
}
}
return;
}
void loop(void)
{
struct parse_info info;
int pipe_fd[2], out_fd, in_fd;
char promptbuf[MAX_PROMPT];
pid_t childpid1, childpid2;
int status;
char *command = NULL;
char **parameters = malloc(sizeof(char *) * ARG_MAX);
extern char *buf;
buf = malloc(sizeof(char) * MAXARG);
if (buf == NULL || parameters == NULL) {
printf("buf and parameters malloc error\n");
return;
}
if (signal(SIGCHLD, sig_handler) == SIG_ERR)
printf("signal error");
while(TRUE) {
prompt(promptbuf);
int paranum = read_command(&command, parameters, promptbuf);
if (paranum < 0) {
printf("read_command error");
continue;
}
paranum--;
parse(parameters, paranum, &info);
if (builtin_command(command, parameters))
continue;
if (info.flags & PIPED) {
if (pipe(pipe_fd) < 0) {
printf("pipe error");
exit(0);
}
}
if ((childpid1 = fork()) != 0) {
if (info.flags & PIPED) {
if((childpid2 = fork()) == 0) {
close(pipe_fd[1]);
close(fileno(stdin));
dup2(pipe_fd[0], fileno(stdin));
close(pipe_fd[0]);
execvp(info.cmd2, info.parameters2);
} else {
close(pipe_fd[0]);
close(pipe_fd[1]);
waitpid(childpid2, &status, 0);
}
}
if (info.flags & BACKGROUND) {
printf("chile pid : %u\n", childpid1);
int i ;
for(i = 0; i < MAX_CHILD; ++i) {
if(child_pid[i] == 0) {
child_pid[i] = childpid1;
break;
}
}
if (i == MAX_CHILD)
printf("too much childpid");
}
else {
waitpid(childpid1, &status, 0);
}
} else {
if (info.flags & PIPED) {
if (!(info.flags & OUT_REDIRECT) && !(info.flags & OUT_REDIRECT_APPEND)) {
close(pipe_fd[0]);
close(fileno(stdout));
dup2(pipe_fd[1], fileno(stdout));
close(pipe_fd[1]);
} else {
close(pipe_fd[0]);
close(pipe_fd[1]);
if (info.flags & OUT_REDIRECT)
out_fd = open(info.out_file, O_WRONLY|O_TRUNC|O_CREAT, 0666);
if (info.flags & OUT_REDIRECT_APPEND)
out_fd = open(info.out_file, O_WRONLY|O_APPEND|O_TRUNC, 0666);
close(fileno(stdout));
dup2(out_fd, fileno(stdout));
close(out_fd);
}
} else {
if(info.flags & OUT_REDIRECT) {
out_fd = open(info.out_file, O_WRONLY|O_TRUNC|O_CREAT, 0666);
close(fileno(stdout));
dup2(out_fd, fileno(stdout));
close(out_fd);
}
if(info.flags & OUT_REDIRECT_APPEND) {
out_fd = open(info.out_file, O_WRONLY|O_APPEND|O_TRUNC, 0666);
close(fileno(stdout));
dup2(out_fd, fileno(stdout));
close(out_fd);
}
}
if(info.flags & IN_REDIRECT) {
in_fd = open(info.in_file, O_RDONLY|O_CREAT, 0666);
close(fileno(stdin));
dup2(in_fd, fileno(stdin));
close(in_fd);
}
execvp(command, parameters);
exit(0);
}
}
free(parameters);
free(buf);
}
int main()
{
int i;
for(i = 0; i < MAX_CHILD; ++i)
child_pid[i] = 0;
loop();
return 0;
}