forked from vvy/wshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwshell.c
181 lines (172 loc) · 5.47 KB
/
wshell.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* =====================================================================================
* Filename: wshell.c
* Description:
* Version: 1.0
* Created: 2013.10.16 17h12min19s
* Author: wuyue (wy), [email protected]
* Company: UESTC
* =====================================================================================
*/
#include "wshell.h"
#define TRUE 1
#define MAXPIDTABLE 1024
pid_t BPTable[MAXPIDTABLE];
void sig_handler(int sig)
{
pid_t pid;
int i;
for(i=0;i<MAXPIDTABLE;i++)
if(BPTable[i] != 0) //only handler the background processes
{
pid = waitpid(BPTable[i],NULL,WNOHANG);
if(pid > 0)
{
printf("process %d exited.\n",pid);
BPTable[i] = 0; //clear
}
else if(pid < 0)
{
if(errno != ECHILD)
perror("waitpid error");
}
//else:do nothing.
//Not background processses has their waitpid() in wshell.
}
return;
}
void proc(void)
{
int status,i;
char *command = NULL;
char **parameters;
int ParaNum;
char prompt[MAX_PROMPT];
struct parse_info info;
pid_t ChdPid,ChdPid2;
parameters = malloc(sizeof(char *)*(MAXARG+2));
buffer = malloc(sizeof(char) * MAXLINE);
if(parameters == NULL || buffer == NULL)
{
printf("Wshell error:malloc failed.\n");
return;
}
//arg[0] is command
//arg[MAXARG+1] is NULL
if(signal(SIGCHLD,sig_handler) == SIG_ERR)
perror("signal() error");
while(TRUE)
{
int pipe_fd[2],in_fd,out_fd;
type_prompt(prompt);
ParaNum = read_command(&command,parameters,prompt);
if(-1 == ParaNum)
continue;
ParaNum--;//count of units in buffer
parsing(parameters,ParaNum,&info);
if(builtin_command(command,parameters))
continue;
if(info.flag & IS_PIPED) //command2 is not null
{
if(pipe(pipe_fd)<0)
{
printf("Wshell error:pipe failed.\n");
exit(0);
}
}
if((ChdPid = fork())!=0) //wshell
{
if(info.flag & IS_PIPED)
{
if((ChdPid2=fork()) == 0) //command2
{
close(pipe_fd[1]);
close(fileno(stdin));
dup2(pipe_fd[0], fileno(stdin));
close(pipe_fd[0]);
execvp(info.command2,info.parameters2);
}
else
{
close(pipe_fd[0]);
close(pipe_fd[1]);
waitpid(ChdPid2,&status,0); //wait command2
}
}
if(info.flag & BACKGROUND)
{
printf("Child pid:%u\n",ChdPid);
int i;
for(i=0;i<MAXPIDTABLE;i++)
if(BPTable[i]==0)
BPTable[i] = ChdPid; //register a background process
if(i==MAXPIDTABLE)
perror("Too much background processes\nThere will be zombine process");
}
else
{
waitpid(ChdPid,&status,0);//wait command1
}
}
else //command1
{
if(info.flag & IS_PIPED) //command2 is not null
{
if(!(info.flag & OUT_REDIRECT) && !(info.flag & OUT_REDIRECT_APPEND)) // ONLY PIPED
{
close(pipe_fd[0]);
close(fileno(stdout));
dup2(pipe_fd[1], fileno(stdout));
close(pipe_fd[1]);
}
else //OUT_REDIRECT and PIPED
{
close(pipe_fd[0]);
close(pipe_fd[1]);//send a EOF to command2
if(info.flag & OUT_REDIRECT)
out_fd = open(info.out_file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
else
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.flag & OUT_REDIRECT) // OUT_REDIRECT WITHOUT PIPE
{
out_fd = open(info.out_file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
close(fileno(stdout));
dup2(out_fd, fileno(stdout));
close(out_fd);
}
if(info.flag & OUT_REDIRECT_APPEND) // OUT_REDIRECT_APPEND WITHOUT PIPE
{
out_fd = open(info.out_file, O_WRONLY|O_CREAT|O_APPEND, 0666);
close(fileno(stdout));
dup2(out_fd, fileno(stdout));
close(out_fd);
}
}
if(info.flag & IN_REDIRECT)
{
in_fd = open(info.in_file, O_CREAT |O_RDONLY, 0666);
close(fileno(stdin));
dup2(in_fd, fileno(stdin));
close(in_fd);
}
execvp(command,parameters);
}
}
free(parameters);
free(buffer);
}
int main() {
int i;
//init the BPTable
for(i=0;i<MAXPIDTABLE;i++)
BPTable[i] = 0;
proc();
return 0;
}