-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute-command.c
321 lines (259 loc) · 5.85 KB
/
execute-command.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// UCLA CS 111 Lab 1 command execution
#include "command.h"
#include "command-internals.h"
#include <error.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
pid_t run_command(command_t c);
command_t find_command(command_t c, bool side);
void set_IO(command_t c);
void set_subshell_IO (command_t c, int* fd);
pid_t execute (command_t c);
int
command_status (command_t c)
{
return c->status;
}
void
execute_command (command_t c)
{
pid_t pid;
int status;
pid = run_command(c);
if (c->type == SIMPLE_COMMAND)
{
waitpid(pid, &status, 0);
c->status = WEXITSTATUS(status);
}
}
pid_t
run_command (command_t c)
{
pid_t pid, pid2;
int status;
int fd[2];
int subshell_fd[4];
switch (c->type)
{
case SIMPLE_COMMAND:
pid = execute(c);
break;
case SUBSHELL_COMMAND:
if (c->input || c->output)
{
set_subshell_IO(c, subshell_fd);
}
pid = run_command(c->u.subshell_command);
if (c->u.subshell_command->type == SIMPLE_COMMAND)
{
waitpid(pid, &status, 0);
c->u.subshell_command->status = WEXITSTATUS(status);
}
c->status = c->u.subshell_command->status;
if (c->input || c->output)
{
//Restores stdin and stdout
close(subshell_fd[3]);
close(subshell_fd[4]);
dup2(subshell_fd[0], 0);
dup2(subshell_fd[1], 1);
close(subshell_fd[0]);
close(subshell_fd[1]);
}
break;
case AND_COMMAND:
pid = run_command(c->u.command[0]);
if (c->u.command[0]->type == SIMPLE_COMMAND)
{
waitpid(pid, &status, 0);
c->u.command[0]->status = WEXITSTATUS(status);
}
c->status = c->u.command[0]->status;
if (c->u.command[0]->status == 0)
{
pid = run_command(c->u.command[1]);
if (c->u.command[1]->type == SIMPLE_COMMAND)
{
waitpid(pid, &status, 0);
c->u.command[1]->status = WEXITSTATUS(status);
}
c->status = c->u.command[1]->status;
}
break;
case OR_COMMAND:
pid = run_command(c->u.command[0]);
if (c->u.command[0]->type == SIMPLE_COMMAND)
{
waitpid(pid, &status, 0);
c->u.command[0]->status = WEXITSTATUS(status);
}
c->status = c->u.command[0]->status;
if (c->u.command[0]->status != 0)
{
pid = run_command(c->u.command[1]);
if (c->u.command[1]->type == SIMPLE_COMMAND)
{
waitpid(pid, &status, 0);
c->u.command[1]->status = WEXITSTATUS(status);
}
c->status = c->u.command[1]->status;
}
break;
case PIPE_COMMAND:
if (pipe(fd) == -1)
error(1, errno, "Error creating pipe");
//Sets the pipe locations
if (c->u.command[0]->type == SIMPLE_COMMAND)
c->u.command[0]->write_pipe = fd;
else
find_command(c->u.command[0], 1)->write_pipe = fd;
if (c->u.command[1]->type == SIMPLE_COMMAND)
c->u.command[1]->read_pipe = fd;
else
find_command(c->u.command[1], 0)->read_pipe = fd;
pid = run_command(c->u.command[0]);
pid2 = run_command(c->u.command[1]);
close(fd[0]);
close(fd[1]);
waitpid(pid, &status, 0);
c->u.command[0]->status = WEXITSTATUS(status);
waitpid(pid2, &status, 0);
c->u.command[1]->status = WEXITSTATUS(status);
c->status = WEXITSTATUS(status);
break;
case SEQUENCE_COMMAND:
pid = run_command(c->u.command[0]);
if (c->u.command[0]->type == SIMPLE_COMMAND)
{
waitpid(pid, &status, 0);
c->u.command[0]->status = WEXITSTATUS(status);
}
pid = run_command(c->u.command[1]);
if (c->u.command[1]->type == SIMPLE_COMMAND)
{
waitpid(pid, &status, 0);
c->u.command[1]->status = WEXITSTATUS(status);
}
c->status = c->u.command[1]->status;
break;
}
return pid;
}
command_t find_command(command_t c, bool side)
{
//Searches through the command tree looking for the leftmost or rightmost command
//side == 0 means find the leftmost, side == 1 finds the rightmost
//Used for piping
command_t val;
switch (c->type)
{
case AND_COMMAND:
case OR_COMMAND:
case SEQUENCE_COMMAND:
case PIPE_COMMAND:
val = find_command(c->u.command[side], side);
break;
case SUBSHELL_COMMAND:
val = find_command(c->u.subshell_command, side);
break;
default:
val = c;
break;
}
return val;
}
void
set_IO (command_t c)
{
int fd[2]; //fd[0] is read, fd[1] is write
if (c->input)
{
fd[0] = open(c->input, O_RDONLY);
if (fd[0] < 0)
error (1, errno, "Error opening file");
dup2(fd[0], 0);
close (fd[0]);
}
if (c->read_pipe)
{
if (!c->input)
dup2(c->read_pipe[0], 0);
close(c->read_pipe[0]);
close(c->read_pipe[1]);
}
if (c->output)
{
fd[1] = open(c->output, O_CREAT | O_TRUNC | O_WRONLY, 0644);
if (fd[1] < 0)
error(1, errno, "Error opening file");
dup2(fd[1], 1);
close (fd[1]);
}
if (c->write_pipe)
{
if (!c->output)
dup2(c->write_pipe[1], 1);
close(c->write_pipe[0]);
close(c->write_pipe[1]);
}
}
void
set_subshell_IO (command_t c, int* fd)
{
//Saves stdin and stdout so they can be restored
fd[0] = dup(0);
fd[1] = dup(1);
if (c->input)
{
fd[3] = open(c->input, O_RDONLY);
if (fd[3] < 0)
error (1, errno, "Error opening file");
dup2(fd[3], 0);
close (fd[3]);
}
if (c->read_pipe)
{
if (!c->input)
dup2(c->read_pipe[0], 0);
close(c->read_pipe[0]);
close(c->read_pipe[1]);
}
if (c->output)
{
fd[4] = open(c->output, O_CREAT | O_TRUNC | O_WRONLY, 0644);
if (fd[4] < 0)
error(1, errno, "Error opening file");
dup2(fd[4], 1);
close (fd[4]);
}
if (c->write_pipe)
{
if (!c->output)
dup2(c->write_pipe[1], 1);
close(c->write_pipe[0]);
close(c->write_pipe[1]);
}
}
pid_t
execute(command_t c)
{
pid_t pid;
pid = fork();
if (pid == -1)
error(1, errno, "Error forking process");
if (pid == 0)
{
set_IO (c);
if (strncmp(c->u.word[0], "exec", 4) == 0)
c->u.word++;
execvp(c->u.word[0], c->u.word);
error(1, errno, "Error executing command %s", c->u.word[0]);
}
return pid;
}