-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute-command.c
More file actions
222 lines (214 loc) · 4.25 KB
/
execute-command.c
File metadata and controls
222 lines (214 loc) · 4.25 KB
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
#include "command.h"
#include "command-internals.h"
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <error.h>
#include <fcntl.h>
/* FIXME: You may need to add #include directives, macro definitions,
static function definitions, etc. */
int
command_status (command_t c)
{
return c->status;
}
void execute_simple(command_t c)
{
int status;
int fd2,fd3;
pid_t pd = fork();
if(pd == 0)
{
if (c->input != NULL)
{
// fd = dup(0);
fd2 = open(c->input, O_RDONLY, 0644);
//printf("Input is %s, fd2 is %d", c->input, fd2);
if(dup2(fd2, 0)<0)
exit(1);
}
if (c->output != NULL)
{
fd3 = open(c->output, O_CREAT | O_TRUNC | O_WRONLY, 0644);
if(dup2(fd3, 1)<0)
exit(1);
}
if( c->u.word[0][0] == 'e' &&
c->u.word[0][1] == 'x' &&
c->u.word[0][2] == 'e' &&
c->u.word[0][3] == 'c' &&
c->u.word[0][4] == '\0')
execvp(c->u.word[1],&(c->u.word[1]));
else
execvp(c->u.word[0],c->u.word);
}
else
{
waitpid(pd,&status,0);
c->status = WEXITSTATUS(status);
}
}
void execute_andor(command_t c,bool time_travel)
{
int status;
pid_t pd = fork();
if((pd == 0))
{
execute_command(c->u.command[0],time_travel);
int st = c->u.command[0]->status;
if(st == 0)
exit(0);
else
exit(1);
//exit(c->u.command[0]->status);
}
else
{
waitpid(pd,&status,0);
int j = WEXITSTATUS(status);
if((c->type == AND_COMMAND && j == 0)
|| (c->type == OR_COMMAND && j != 0))
{
pid_t pd2 = fork();
if(pd2 == 0)
{
execute_command(c->u.command[1],time_travel);
int st = c->u.command[1]->status;
if(st == 0)
exit(0);
else
exit(1);
//exit(c->u.command[1]->status);
}
else
{
waitpid(pd2,&status,0);
c->status = WEXITSTATUS(status);
}
}
else
c->status = WEXITSTATUS(status);
}
}
void execute_seq(command_t c, bool time_travel)
{
pid_t pd = fork();
if(pd == 0)
{
execute_command(c->u.command[0],time_travel);
int st = c->u.command[0]->status;
if(st == 0)
exit(0);
else
exit(1);
}
else
{
int status;
waitpid(pd,&status,0);
pid_t pd2 = fork();
if(pd2 == 0)
{
execute_command(c->u.command[1],time_travel);
int st = c->u.command[1]->status;
if(st == 0)
exit(0);
else
exit(1);
//exit(c->u.command[1]->status);
}
else
{
waitpid(pd2,&status,0);
c->status = WEXITSTATUS(status);
}
}
}
void execute_subshell(command_t c, bool time_travel)
{
// pid_t firstPid = fork();
//if (firstPid < 0)
//error(1, errno, "fork failed");
//if (firstPid == 0)
// {
if (c->output != NULL)
{
int fd = open(c->output, O_CREAT | O_TRUNC | O_WRONLY, 0644);
dup2(fd, 1);
}
execute_command(c->u.subshell_command, time_travel);
// int st = c->u.subshell_command->status;
//if (st == 0)
// exit(0);
//else
// exit(1);
// }
// else
// {
//int status;
//waitpid(firstPid, &status, 0);
c->status = c->u.subshell_command->status;
//}
}
void execute_pipe(command_t c, bool time_travel)
{
int fd[2];
pipe(fd);
pid_t firstPid = fork();
if (firstPid == 0) // first child
{
close(fd[1]);
dup2(fd[0],0);
execute_command(c->u.command[1], time_travel);
int st = c->u.command[1]->status;
if (st == 0)
exit(0);
else
exit(1);
}
else
{
pid_t secondPid = fork();
if (secondPid == 0)
{
close(fd[0]);
dup2(fd[1],1);
execute_command(c->u.command[0], time_travel);
int st = c->u.command[0]->status;
if (st == 0)
exit(0);
else
exit(1);
}
else
{
close(fd[0]);
close(fd[1]);
int status;
waitpid(-1, &status, 0);
waitpid(-1, &status, 0);
int estatus = WEXITSTATUS(status);
c->status = estatus;
}
}
}
void
execute_command (command_t c, bool time_travel)
{
time_travel = false;
switch(c->type)
{
case SIMPLE_COMMAND:
execute_simple(c); break;
case OR_COMMAND:
case AND_COMMAND:
execute_andor(c, time_travel); break;
case SEQUENCE_COMMAND:
execute_seq(c,time_travel);break;
case PIPE_COMMAND:
execute_pipe(c, time_travel); break;
case SUBSHELL_COMMAND:
execute_subshell(c, time_travel); break;
}
}