-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
459 lines (455 loc) · 16.1 KB
/
main.c
File metadata and controls
459 lines (455 loc) · 16.1 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include "cd.h"
#include "cronjob.h"
#include "itoa.h"
#include "jobs.h"
#include "pinfo.h"
#include "prompt.h"
#include "run_command.h"
#include "signal_handlers.h"
#include <errno.h>
#include <fcntl.h>
#include <readline/history.h>
#include <readline/readline.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
char currDir[256];
char homeDir[256];
int curr_fg = -1;
struct job {
int pid;
char *command;
int running;
int exited;
};
int next_job_no = 1;
struct job job_store[1024];
int main() {
// Activate signal handlers
signal(SIGINT, ctrlC);
signal(SIGTSTP, ctrlZ);
signal(SIGQUIT, ctrlD);
signal(SIGCHLD, sigchld_handler);
// -> For home to be directory in which executable (as mentioned in
// moodle) is present uncomment following lines
// char exe_path[128];
// sprintf(exe_path,"/proc/%d/exe", getpid()); int tp_ret =
// readlink(exe_path, currDir, 256); currDir[tp_ret] = '\0'; for (int i =
// strlen(currDir) - 1; i >= 0; i--) {
// if ((i > 0) && (currDir[i] == '/' && currDir[i - 1] != '\\')) {
// currDir[i] = '\0';
// break;
// }
// }
// -> Path is set to Directory from which executable is invoked (as
// mentioned in pdf)
getcwd(currDir, 256);
strcpy(homeDir, currDir);
using_history();
while (1) {
char pmt_str[128];
storePromptString(pmt_str);
char *whole_command = NULL;
whole_command = readline(pmt_str);
add_history(whole_command);
char *whc_token;
char *with_pipes_command = strtok_r(whole_command, ";", &whc_token);
while (with_pipes_command != NULL) {
int has_pipes = 0;
for (int i = 0; i < strlen(with_pipes_command); i++) {
if (with_pipes_command[i] == '|') {
has_pipes = 1;
break;
}
}
char *pipe_token;
char *command = strtok_r(with_pipes_command, "|", &pipe_token);
int next_in = 0;
int sq_pipe[2];
while (command != NULL) {
if (has_pipes) {
if (next_in != 0)
close(sq_pipe[1]);
pipe(sq_pipe);
}
while (command[0] == ' ' || command[0] == '\t')
command++;
int argc = 0, max_size_arg = 0;
for (int t = 0; t < strlen(command); t++) {
int size = 0;
while (t < strlen(command) &&
!(command[t] == ' ' || command[t] == '\t')) {
size += 1;
t++;
}
if (size > max_size_arg)
max_size_arg = size;
if (size)
argc++;
}
max_size_arg++;
char **argv = (char **)(malloc(sizeof(char *) * (argc + 1)));
for (int i = 0; i <= argc; i++) {
argv[i] = (char *)(malloc(max_size_arg));
}
char *pa_token;
char *part_arg = strtok_r(command, " \t", &pa_token);
int i = 0;
while (part_arg != NULL) {
strcpy(argv[i++], part_arg);
part_arg = strtok_r(NULL, " \t", &pa_token);
}
int err_fork = 0, end = 0, start = 0;
for (int i = 0; i < argc; i++) {
int ret_rcmd = -9; // Flag set as -9 to test if child created
int stat_ret_rcmd_pipe[2];
pipe(stat_ret_rcmd_pipe);
// When argv[i] is one of ">", ">>", "<", "|"
if (!strcmp(">", argv[i])) {
if (i == argc - 1 || i == 0) {
// When command ends / starts with these special strings
fprintf(stderr, "Invalid command\n");
break;
}
end = i;
if (strcmp(">", argv[i + 1]) == 0 ||
strcmp(">>", argv[i + 1]) == 0 ||
strcmp("<", argv[i + 1]) == 0) {
// If file to be written to is a special string
fprintf(stderr, "Invalid command\n");
break;
}
int fk = fork();
if (fk == -1) {
perror("Error while forking");
err_fork = 1;
break;
}
if (fk == 0) {
if (next_in != 0) {
// Replace stdin with next_in
dup2(next_in, 0);
close(next_in);
}
// Replace stdout with file to write
int fd = open(argv[i + 1], O_CREAT | O_WRONLY | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0) {
perror("Error while creating file");
exit(1);
}
dup2(fd, 1);
char *tmp = (char *)(malloc(1));
*tmp = run_cmd(start, end, argv, max_size_arg, NULL) + '0';
close(stat_ret_rcmd_pipe[0]);
write(stat_ret_rcmd_pipe[1], tmp, 1);
exit(0);
}
waitpid(fk, NULL, 0);
start = end + 2; // This takes care of invalid commands in next iter
i++;
} else if (!strcmp(">>", argv[i])) {
if (i == argc - 1 || i == 0) {
// When command ends / starts with these special strings
fprintf(stderr, "Invalid command\n");
break;
}
end = i;
if (strcmp(">", argv[i + 1]) == 0 ||
strcmp(">>", argv[i + 1]) == 0 ||
strcmp("<", argv[i + 1]) == 0) {
// If file to be written to is a special string
fprintf(stderr, "Invalid command\n");
break;
}
int fk = fork();
if (fk == -1) {
perror("Error while forking");
err_fork = 1;
break;
}
if (fk == 0) {
if (next_in != 0) {
// Replace stdin with next_in
dup2(next_in, 0);
close(next_in);
}
// Replace stdout with file to write
int fd = open(argv[i + 1], O_CREAT | O_APPEND | O_WRONLY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0) {
perror("Error while appending to file");
exit(1);
}
dup2(fd, 1);
char *tmp = (char *)(malloc(1));
*tmp = run_cmd(start, end, argv, max_size_arg, NULL) + '0';
close(stat_ret_rcmd_pipe[0]);
write(stat_ret_rcmd_pipe[1], tmp, 1);
exit(0);
}
waitpid(fk, NULL, 0);
start = end + 2; // This takes care of invalid commands in next iter
i++;
} else if (!strcmp("<", argv[i])) {
int send_out_to_file = 0;
if (i == argc - 1 || i == 0) {
// When command ends / starts with these special strings
fprintf(stderr, "Invalid command\n");
break;
}
end = i;
if (end + 3 < argc && (strcmp(argv[end + 2], ">") == 0 ||
strcmp(argv[end + 2], ">>") == 0)) {
send_out_to_file = 1;
}
if (strcmp(">", argv[i + 1]) == 0 ||
strcmp(">>", argv[i + 1]) == 0 ||
strcmp("<", argv[i + 1]) == 0) {
// If file to be written to is a special string
fprintf(stderr, "Invalid command\n");
break;
}
int fk = fork();
if (fk == -1) {
perror("Error while forking");
err_fork = 1;
break;
}
if (fk == 0) {
int out_fd;
if (send_out_to_file) {
// Send to file instead of stdout when having > / >> after <
if (strcmp(argv[end + 2], ">>") == 0) {
out_fd = open(argv[end + 3], O_CREAT | O_APPEND | O_WRONLY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
} else {
out_fd = open(argv[end + 3], O_CREAT | O_WRONLY | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
}
if (out_fd < 0) {
perror("Error while appending to file");
exit(1);
}
// Replace stdout with out_fd
dup2(out_fd, 1);
close(out_fd);
}
// Replace stdin with file to readfrom
int fd = open(argv[i + 1], O_RDONLY);
if (fd < 0) {
perror("Error while reading file");
exit(1);
}
dup2(fd, 0);
close(fd);
if (has_pipes) {
// Replace stdout with sq_pipe[1]
if (send_out_to_file == 0)
dup2(sq_pipe[1], 1);
close(sq_pipe[1]);
}
char *tmp = (char *)(malloc(1));
*tmp = run_cmd(start, end, argv, max_size_arg, NULL) + '0';
close(stat_ret_rcmd_pipe[0]);
write(stat_ret_rcmd_pipe[1], tmp, 1);
exit(0);
}
waitpid(fk, NULL, 0);
start = end + 2; // This takes care of invalid commands in next iter
if (send_out_to_file) {
start = end + 4;
i += 2;
}
i++;
} else if (i == argc - 1) {
end = argc;
if (strcmp(argv[start], "cd") == 0) {
cd(end - start, (argv + start));
} else if (!(strcmp("pinfo", argv[start]))) {
pinfo(end - start, (argv + start));
} else if (!(strcmp("setenv", argv[start]))) {
if (end - start == 2) {
if (setenv(argv[start + 1], "", 1) == -1) {
perror("Error occured while setting environment variable");
}
} else if (end - start == 3) {
if (setenv(argv[start + 1], argv[start + 2], 1) == -1) {
perror("Error occured while setting environment variable");
}
} else {
fprintf(stderr,
"Error, setenv takes either one or two arguments\n");
}
} else if (!(strcmp("unsetenv", argv[start]))) {
if (end - start == 2) {
if (unsetenv(argv[start + 1]) == -1) {
perror("Error occurred while unsetting environment variable");
}
} else {
fprintf(stderr, "Error, unsetenv takes only one argument\n");
}
} else if (!(strcmp("kjob", argv[start]))) {
kjob(end - start, argv);
} else if (!(strcmp("bg", argv[start]))) {
bg(end - start, argv);
} else if (!(strcmp("fg", argv[start]))) {
fg(end - start, (argv + start));
} else if (!(strcmp("overkill", argv[start]))) {
overkill(end - start, (argv + start));
} else if (!(strcmp("cronjob", argv[start]))) {
int fk = fork();
if (fk == 0) {
close(sq_pipe[1]);
close(sq_pipe[0]);
cronjob(end - start, (argv + start), max_size_arg);
exit(0);
}
ret_rcmd = 0;
} else {
char **temp_argv = argv + start;
int temp_argc = end - start;
if (!strcmp("&", temp_argv[temp_argc - 1])) {
// Handle background processes here
char *temporary_store = temp_argv[argc - 1];
temp_argv[argc - 1] = NULL;
job_store[next_job_no].command =
(char *)(malloc(sizeof(temp_argv[0])));
strcpy(job_store[next_job_no].command, temp_argv[0]);
job_store[next_job_no].running = 1;
job_store[next_job_no].exited = 0;
int fk = fork();
if (fk == -1) {
perror("Error while forking");
err_fork = 1;
break;
}
if (fk == 0) {
if (has_pipes)
close(sq_pipe[1]);
int rk = execvp(temp_argv[0], temp_argv);
if (rk < 0) {
fprintf(stderr, "Command \"%s\" not found\n", temp_argv[0]);
exit(1);
}
}
setpgid(fk, fk);
job_store[next_job_no].pid = fk;
next_job_no++;
temp_argv[argc - 1] = temporary_store;
ret_rcmd = 0;
} else {
int pid_pipe[2];
pipe(pid_pipe);
int fk = fork();
if (fk == -1) {
perror("Error while forking");
err_fork = 1;
break;
}
if (fk == 0) {
int pid_fg = 0; // If inbuilt command then give pid as 0
// Replace stdin with next_in
if (next_in != 0) {
dup2(next_in, 0);
close(next_in);
}
if (has_pipes) {
// Replace stdout with sq_pipe[1]
dup2(sq_pipe[1], 1);
close(sq_pipe[1]);
}
char *tmp = (char *)(malloc(1));
*tmp = run_cmd(start, end, argv, max_size_arg, &pid_fg) + '0';
close(stat_ret_rcmd_pipe[0]);
write(stat_ret_rcmd_pipe[1], tmp, 1);
// Write pid_fg to pid_pipe
close(pid_pipe[0]);
char str_pid[100];
b10itoa(pid_fg, str_pid);
write(pid_pipe[1], str_pid, 100);
exit(0);
}
waitpid(fk, NULL, 0);
int fg_pid;
char str_pid[100];
close(pid_pipe[1]);
read(pid_pipe[0], str_pid, 100);
fg_pid = atoi(str_pid);
if (fg_pid != 0) {
// Check if fg process got suspended
char state[5];
sleep(1);
if (get_state(fg_pid, state) != -1)
if (strcmp(state, "T") == 0) {
// Process got suspended
job_store[next_job_no].command =
(char *)(malloc(sizeof((argv + start)[0])));
strcpy(job_store[next_job_no].command, (argv + start)[0]);
job_store[next_job_no].running = 0;
job_store[next_job_no].exited = 0;
job_store[next_job_no].pid = fg_pid;
next_job_no++;
}
}
}
}
} else {
continue;
}
if (ret_rcmd == -9) {
// If child created, get return status of run_cmd
close(stat_ret_rcmd_pipe[1]);
char *tmp = (char *)(malloc(1));
read(stat_ret_rcmd_pipe[0], tmp, 1);
ret_rcmd = *tmp - '0';
}
if (ret_rcmd == -1) {
// When encountered quit
overkill(1, NULL);
return 0;
}
if (ret_rcmd == 1) {
// When error occurs due to forking
command = NULL;
err_fork = 1;
break;
}
}
for (int i = 0; i <= argc; i++) {
free(argv[i]);
}
free(argv);
if (err_fork != 1) {
command = strtok_r(NULL, "|", &pipe_token);
}
if (has_pipes) {
next_in = sq_pipe[0];
}
}
write(sq_pipe[1], "\0", 1);
close(sq_pipe[1]);
if (has_pipes) {
int fk = fork();
if (fk == 0) {
char buff[1024];
while (read(next_in, buff, 1024) > 0) {
printf("%s", buff);
}
close(next_in);
exit(0);
}
waitpid(fk, NULL, 0);
}
with_pipes_command = strtok_r(NULL, ";", &whc_token);
if (with_pipes_command != NULL)
printf("\n"); // To distinguish b/w outputs of tokenized commands
}
free(whole_command);
}
overkill(1, NULL);
return 0;
}