-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.c
More file actions
267 lines (225 loc) · 7.53 KB
/
execute.c
File metadata and controls
267 lines (225 loc) · 7.53 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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include "execute.h"
#include "signals.h"
/* Function to execute user commands */
void execute(Command *command) {
char **argv = command->argv;
char executable_path[512];
char tapper_path[512];
char tappet_path[512];
char* last_slash;
int out = 0; // Output redirection flag
int err = 0; // Error redirection flag
int in = 0; // Input redirection flag
int file_descriptor; // File Descriptor for output file if redirection is intended
char *out_file = NULL; // Output file name if any
char *err_file = NULL; // Output file for error
char *in_file = NULL; // Input file name if any
char *both_file = NULL; // Both stout and stdin go here if directed
readlink("/proc/self/exe", executable_path, sizeof(executable_path));
strcpy(tapper_path, executable_path);
strcpy(tappet_path, executable_path);
// Check if the command contains '>' for redirection or '1>' for standard output redirection
for (int i = 0; argv[i] != NULL; i++) {
if (strcmp(argv[i], ">") == 0 || (strcmp(argv[i], "1>") == 0)) {
out_file = argv[i + 1]; // The next string is the output file name
argv[i] = NULL; // Remove '>' from command
out = 1;
}
}
// Check if the command contains '2>' for error redirection
for (int i = 0; argv[i] != NULL; i++) {
if (strcmp(argv[i], "2>") == 0) {
err_file = argv[i + 1];
argv[i] = NULL;
err = 1;
}
}
for (int i = 0; argv[i] != NULL; i++) {
if (strcmp(argv[i], "&>") == 0) {
both_file = argv[i + 1];
argv[i] = NULL;
out = 1;
err = 1;
}
}
// Check if the command contains '<' for input redirection
for (int i = 0; argv[i] != NULL; i++) {
if (strcmp(argv[i], "<") == 0) {
in_file = argv[i + 1];
argv[i] = NULL;
in = 1;
}
}
pid_t pid = fork();
if (pid == 0) {
if (out) {
if (both_file) {
file_descriptor = open(both_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
} else {
file_descriptor = open(out_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
}
dup2(file_descriptor, STDOUT_FILENO);
close(file_descriptor);
}
if (err) {
if (both_file) {
file_descriptor = open(both_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
} else {
file_descriptor = open(err_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
}
dup2(file_descriptor, STDERR_FILENO);
close(file_descriptor);
}
if (in) {
file_descriptor = open(in_file, O_RDONLY);
dup2(file_descriptor, STDIN_FILENO);
close(file_descriptor);
}
if (strcmp(argv[0], "tapper") == 0) {
last_slash = strrchr(tapper_path, '/');
if (last_slash != NULL) {
*(last_slash + 1) = '\0'; // trim path after last "/"
}
strcat(tapper_path, "tapper");
argv[0] = tapper_path;
}
if (strcmp(argv[0], "tappet") == 0) {
last_slash = strrchr(tappet_path, '/');
if (last_slash != NULL) {
*(last_slash + 1) = '\0'; // trim path after last "/"
}
strcat(tappet_path, "tappet");
argv[0] = tappet_path;
}
if (execvp(argv[0], argv) < 0) {
fprintf(stderr, "*** ERROR: exec failed\n");
exit(1);
} else {
printf("executing: %s\n", argv[0]);
}
} else if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
else {
int status;
waitpid(pid, &status, 0);
}
}
void execute_pipe_commands(Command *cmd) {
int prev_pipe = STDIN_FILENO;
int pfds[2];
pid_t *pids_arr; // Created an array to hold pid of all child processes
int cmd_count = 0; // Count of total commands
Command *tmp = cmd;
// Count total commands
while (tmp) {
cmd_count++;
tmp = tmp->next;
}
pids_arr = (pid_t*)malloc(cmd_count * sizeof(pid_t));
pid_t pid;
Command *current_command = cmd;
int i = 0; // Counter to store pid in the array
while (current_command) {
if (pipe(pfds) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid = fork();
if (pid == 0) {
if (prev_pipe != STDIN_FILENO) {
dup2(prev_pipe, STDIN_FILENO);
close(prev_pipe);
}
if (current_command->next) {
dup2(pfds[1], STDOUT_FILENO);
}
close(pfds[0]);
close(pfds[1]);
execute(current_command);
_exit(EXIT_FAILURE);
} else if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
if (prev_pipe != STDIN_FILENO) {
close(prev_pipe);
}
close(pfds[1]);
prev_pipe = pfds[0];
current_command = current_command->next;
pids_arr[i++] = pid; // Store the pid of this child
}
if (prev_pipe != STDIN_FILENO) {
close(prev_pipe);
}
// Waiting for child processes in the order they were created
for (i = 0; i < cmd_count; ++i) {
waitpid(pids_arr[i], NULL, 0);
}
free(pids_arr);
}
void execute_separated_commands(char* cmdline) {
char *line;
Command *command_list = NULL, *last_command = NULL;
char *rest = cmdline;
// This will split the command line input by semicolons and pipes
while ((line = strsep(&rest, ";")) != NULL) {
// Check if it is a piped command
if (strchr(line, '|') != NULL) {
Command *piped_command = pipe_parser(line);
execute_pipe_commands(piped_command);
continue;
}
Command *current_command = parser(line);
// Append commands to a linked list
if (last_command == NULL) {
command_list = current_command;
last_command = current_command;
} else {
last_command->next = current_command;
last_command = current_command;
}
}
Command *current = command_list;
while(current != NULL){
int status;
pid_t pid;
signal(SIGINT, sigint_handler);
if (current->argv[0] && strcmp(current->argv[0], "exit") == 0)
exit(0);
pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork Failed");
exit(1);
} else if (pid == 0) {
signal(SIGINT, SIG_DFL);
execute(current);
exit(0); //important to exit otherwise it will continue to execute the remaining commands in the child
} else {
// keep track of child pid
childpids[childpids_size++] = pid;
if (current->background) {
printf("Background task running... pid: %d, Command: %s\n", pid, current->argv[0]);
usleep(5000);
}
else {
waitpid(pid, &status, 0); //Ensure you don't have zombie process
}
// Reassign SIGINT handler in parent shell
signal(SIGINT, sigint_handler);
}
Command *next_command = current->next;
free(current->argv);
free(current);
current = next_command;
}
}