-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute-command.c
More file actions
183 lines (156 loc) · 5.15 KB
/
execute-command.c
File metadata and controls
183 lines (156 loc) · 5.15 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
// UCLA CS 111 Lab 1 command execution
#include "command.h"
#include "command-internals.h"
#include <error.h>
#include <errno.h> //creates the integer errno
#include <unistd.h> //for pipe and fork
#include <stdio.h> //for standard input-output
#include <string.h>
#include <sys/types.h> //for pid_t
#include <sys/wait.h> //for waitpid
#include <stdlib.h> //for exit
#include <fcntl.h> //for controlling file
/* FIXME: You may need to add #include directives, macro definitions,
static function definitions, etc. */
int
command_status (command_t c)
{
return c->status;
}
int ctr = 0;
void io(command_t c)
{
if(c->input){
int input;
int dp2;
input = open(c->input, O_RDWR, 0644);
if(input<0)
error(3,0,"No such file");
dp2 = dup2(input, 0);
if(dp2 < 0)
error(3, 0, "Redirect failed");
close(input);
}
if(c->output)
{
int output;
int dp2;
output = open(c->output, O_CREAT|O_TRUNC|O_WRONLY, 0644);
if( output < 0)
error(1,0,"Cannot write to the file");
dp2 = dup2(output, 1);
if( dp2 < 0)
error(1, 0, "Could not use dup2 for output");
if( close(output) < 0)
error(1, 0, "Could not close output file");
}
}
void
execute_command (command_t c, bool time_travel)
{
/* FIXME: Replace this with your implementation. You may need to
add auxiliary functions and otherwise modify the source code.
You can also use external functions defined in the GNU C Library. */
// print_command(c);
//return;
int fd[2];
pipe(fd);
pid_t x;
pid_t firstPid;
pid_t secondPid;
//print_command(c);
switch(c->type)
{
case SIMPLE_COMMAND:
x = fork();
if(x == 0) //child
{
//printf("I should be here \n");
io(c);
execvp(c->u.word[0], c->u.word);
// printf("How the hell do I get here \n");
exit(127);
}
if(x > 0) //parent
{
int status;
waitpid(x, &status,0);
//printf("status is %d and exit status is %d \n",status,WEXITSTATUS(status));
c->status = WEXITSTATUS(status);
}
break;
case AND_COMMAND:
execute_command(c->u.command[0], time_travel);
if(c->u.command[0]->status == 0)
{
execute_command(c->u.command[1], time_travel);
c->status = c->u.command[1]->status;
}
else
c->status = c->u.command[0]->status;
break;
case OR_COMMAND:
execute_command(c->u.command[0], time_travel);
if(c->u.command[0]->status != 0)
{
execute_command(c->u.command[1], time_travel);
c->status = c->u.command[1]->status;
}
else
c->status = c->u.command[0]->status;
break;
case SEQUENCE_COMMAND:
execute_command(c->u.command[0],time_travel);
execute_command(c->u.command[1],time_travel);
c->status = c->u.command[1]->status;
break;
case PIPE_COMMAND:
firstPid = fork();
if (firstPid == 0) //execute cmd on the right
{
close(fd[1]); //close unused write end
dup2(fd[0], 0);
//char *a[] = { “sort”, 0 };
//execvp(a[0], a);
execute_command(c->u.command[1], time_travel);
exit(c->u.command[1]->status);
}
else
{
secondPid = fork();
if (secondPid == 0) { //execute command on the left
close(fd[0]); //close unused readend
dup2(fd[1], 1);
//char *a[] = { “cat”, “foo.txt”, 0 };
//execvp(a[0], a);
execute_command(c->u.command[0], time_travel);
exit(c->u.command[0]->status);
}
else {
close(fd[0]); //close unused read and write end
close(fd[1]);
int status;
int returnedPid = waitpid(-1, &status, 0);
if (returnedPid == secondPid) { //this means second finished first so //wait on the first
waitpid(firstPid, &status, 0);
//c->status = WEXITSTATUS(status);
c->status = c->u.command[1]->status;
//set exit status of second
}
if (returnedPid == firstPid)
{
waitpid(secondPid, &status, 0);
//set exit status of first
c->status = c->u.command[0]->status;
}
}
}
break;
case SUBSHELL_COMMAND:
io(c);
execute_command(c->u.subshell_command,time_travel);
c->status = c->u.subshell_command->status;
break;
// error (1, 0, "command execution not yet implemented");
}
}