-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecute.c
597 lines (579 loc) · 18.8 KB
/
execute.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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h> // contains boolean realtons
#include "execute.h"
#include "echo.h"
#include "display.h"
#include "history.h"
#include "ls.h"
#include "pwd.h"
#include "cd.h"
#include "jobs.h"
#include "pinfo.h"
#include "pwd.h"
#include "discover.h"
#include "jobs.h"
#include "fg.h"
#include "bg.h"
#include "sig.h"
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
typedef char *stringg;
typedef struct passwd *main_struct;
typedef long long element_type;
#define MAX_LIMITS 1000
#include <time.h>
int fd;
int flag_check = 0;
int final_argc;
int checku = 0;
int var_fg = 0;
// check_valids is the function checks if valid arguments are passed
int check_validss(int argc, char stringgs[50])
{
if (argc == 0)
{
return -1;
}
if (stringgs == NULL)
{
return -1;
}
return 0;
}
// This function handles redirection along with command calls.
void handle(char *command_list[], int argc, int check)
{
int array_del[100];
int count = 0;
command_list[argc] = NULL;
int check_v = check_validss(argc, command_list[0]);
if (check_v == -1)
{
return;
}
for (int m = 0; m < argc; m++)
{
if (!strcmp(command_list[m], "<"))
{
flag_check = 1;
// < file name as i+1 is assumed to be file name
char *file_name = command_list[m + 1];
int input_fd = open(file_name, O_RDONLY);
fd = input_fd;
int error_hai = 0;
if (input_fd >= 0)
{
int dupp = dup2(input_fd, STDIN_FILENO); // stdin has been duped to standard input
if (dupp < 0)
{
perror("ERROR WHILE DUPLICATING STDIN \n");
error_hai = 1;
}
error_hai = 0;
}
else
{
perror("ERROR WHILE OPENING INPUT FILE\n");
checku = -1;
error_hai = 1;
}
if (error_hai == 1)
{
break;
}
else
{
// so we have this array_del that keeps account which index of command list has to be deleted
// here < file_name needs to be deleted.
array_del[count] = m;
count++;
array_del[count] = m + 1;
count++;
}
}
else if (strcmp(command_list[m], ">") == 0)
{
flag_check = 1;
int error_hai1 = 0;
char *file_name = command_list[m + 1];
// creates outputfile and data is redirected .
int output_fd = open(file_name, O_CREAT | O_WRONLY | O_TRUNC, 0644);
fd = output_fd;
if (output_fd < 0)
{
perror("ERROR WHILE OPENING OUTPUT FILE\n");
checku = -1;
error_hai1 = 1;
}
else
{
// stdout be duplicated to to output_fd
int dupp = dup2(output_fd, STDOUT_FILENO);
if (dupp < 0)
{
perror("ERROR WHILE DUPLICATING STDOUT\n");
error_hai1 = 1;
}
else
{
error_hai1 = 0;
}
}
if (error_hai1 == 1)
{
break;
}
else
{
// so we have this array_del that keeps account which index of command list has to be deleted
// here > file_name needs to be deleted.
array_del[count] = m;
count++;
array_del[count] = m + 1;
count++;
}
}
else if (strcmp(command_list[m], ">>") == 0)
{
flag_check = 1;
int error_hai2 = 0;
char *file_name = command_list[m + 1];
int output_fd = open(file_name, O_CREAT | O_WRONLY | O_APPEND, 0644);
fd = output_fd;
if (output_fd < 0)
{
perror("ERROR WHILE OPENING OUTPUT FILE\n");
checku = -1;
error_hai2 = 1;
}
else
{
// sicne we have to concatenate data so we dup the stdout to output fd
int dupp = dup2(output_fd, STDOUT_FILENO);
if (dupp < 0)
{
printf("error while duplicating fd \n");
error_hai2 = 1;
}
else
{
error_hai2 = 0;
}
}
if (error_hai2 == 1)
{
break;
}
else
{
// so we have this array_del that keeps account which index of command list has to be deleted
// here >> file_name needs to be deleted.
array_del[count] = m;
count++;
array_del[count] = m + 1;
count++;
}
}
}
if (count == 0)
{
// count =0 tells there was no redirection
final_argc = argc;
for (long long i = final_argc; i <= argc; i++)
{
command_list[final_argc] = NULL;
}
}
else
{
// here we make a new command list removing the redirection
char *final_command_redirecton[100];
for (long long g = 0; g < 100; g++)
{
final_command_redirecton[g] = (char *)malloc(100 * sizeof(char));
}
// allocation of space has been done note count is total number of commands to be deleted
int counter = 0;
int counter2 = 0;
for (int h = 0; h < argc; h++)
{
if (h == array_del[counter])
{
if (counter < count)
counter++;
}
else
{
// a new command list named as final_command _direction has counter 2 as it id
strcpy(final_command_redirecton[counter2], command_list[h]);
counter2++;
}
}
final_command_redirecton[counter2] = NULL;
final_argc = counter2;
// we make our command list equal to final command list
for (long long z1 = 0; z1 < counter2; z1++)
{
strcpy(command_list[z1], final_command_redirecton[z1]);
}
for (long long op = final_argc; op <= argc; op++)
{
command_list[op] = NULL;
}
}
if (check == -1)
{
return;
}
if (command_list[0] == NULL)
{
return;
}
command_list[final_argc] = NULL;
if (strcmp(command_list[0], "cd") == 0)
{
cd(final_argc, command_list);
}
else if (strcmp(command_list[0], "echo") == 0)
{
echo(final_argc, command_list);
}
else if (strcmp(command_list[0], "jobs") == 0)
{
// we are passing a argument that is bascially flag it can be "-r" or "-s"
if (command_list[1] == NULL)
{
jobs(NULL);
}
else
{
jobs(command_list[1]);
}
}
else if (strcmp(command_list[0], "pwd") == 0)
{
pwd(final_argc);
}
else if (strcmp(command_list[0], "fg") == 0)
{
calculate_fg(final_argc, command_list);
if (var_fg < 0)
{
var_fg = -var_fg;
}
}
else if (strcmp(command_list[0], "history") == 0)
{
history(final_argc, command_list);
}
else if (strcmp(command_list[0], "bg") == 0)
{
if (final_argc < 2)
{
printf("Error: TOO FEW ARGUMENTS\n");
}
else if (final_argc > 2)
{
printf("Error: TOO MANY ARGUMENTS\n");
}
else
{
calculate_bg(final_argc, command_list);
}
}
else if (strcmp(command_list[0], "sig") == 0)
{
if (final_argc == 3)
{
int job_index = atoi(command_list[1]);
int signal_index = atoi(command_list[2]);
int jb_pid = background_pid[job_index - 1];
sig(final_argc,jb_pid,signal_index);
}
}
else if (strcmp(command_list[0], "ls") == 0)
{
ls(final_argc, command_list);
}
else if (strcmp(command_list[0], "pinfo") == 0)
{
pinfo(final_argc, command_list);
}
else if (strcmp(command_list[0], "discover") == 0)
{
discover(final_argc, command_list);
}
else if (!strcmp(command_list[0], "exit") || !strcmp(command_list[0], "EXIT"))
{
print_history();
stooped = 1;
}
else
{
int pid = fork(); // creating a child process
command_list[final_argc] = NULL;
if (pid > 0)
{
if (check == 0 && pid > 0)
{
int ctrz_aya;
time_t start = time(NULL);
signal(SIGTTIN, SIG_IGN); // when some background process tries to read/ writefrom terminal then SIGTIN generated
signal(SIGTTOU, SIG_IGN); // such read/w signal are said to be ignored
waitpid(pid, &ctrz_aya, WUNTRACED);
time_t end = time(NULL);
signal(SIGTTIN, SIG_IGN); // when some background process tries to read/ writefrom terminal then SIGTIN generated
signal(SIGTTOU, SIG_IGN);
int diff = end - start;
if (WIFSTOPPED(ctrz_aya)) // generated when ctrl z
{
pidu = pid; // global variable that stores value of fg process
char command[100];
strcpy(command, command_list[0]);
long long len = strlen(command);
len++;
for (long long hj = 0; hj < 200; hj++)
{
if (background_pid[hj] == 0)
{
background_pid[hj] = pid;
bgCommand[hj] = (char *)malloc((len) * sizeof(char));
final[hj] = (char *)malloc(20 * sizeof(char));
if (!flag_check)
{
// so we have this bgCommand array externed to stores background commands
strcpy(bgCommand[hj], command_list[0]);
// this final is basically last part of the command which for sleep 7 is 7
if (command_list[1] == NULL)
{
final[hj] = NULL;
}
else
{
strcpy(final[hj], command_list[1]);
}
}
else
{
strcpy(bgCommand[hj], command_list[0]);
if (command_list[1] == NULL)
{
final[hj] = NULL;
}
else
{
strcpy(final[hj], command_list[1]);
}
}
break;
}
}
}
// performs the task of took.. time in fg processs
if (diff >= 1)
{
char *hey = (char *)malloc(1000 * sizeof(char));
cutu += end - start;
sprintf(hey, "took %lld", cutu);
fgo = 1;
strcpy(fgu, hey);
free(hey);
}
}
else if (check == 1 && pid > 0)
{
// for bacground process here check is for if process is bacground
printf("COMMAND- %s, PID : %d\n", command_list[0], pid);
char command[100];
strcpy(command, command_list[0]);
long long len = strlen(command);
len++;
for (long long i = 0; i < 200; i++)
{ // store pid of the processes in pid array and name in background command array
if (background_pid[i] == 0)
{
background_pid[i] = pid;
bgCommand[i] = (char *)malloc((len) * sizeof(char));
final[i] = (char *)malloc(20 * sizeof(char));
if (!flag_check)
{
strcpy(bgCommand[i], command_list[0]);
if (command_list[1] == NULL)
{
final[i] = NULL;
}
else
{
strcpy(final[i], command_list[1]);
}
}
else
{
strcpy(bgCommand[i], command_list[0]);
if (command_list[1] == NULL)
{
final[i] = NULL;
}
else
{
strcpy(final[i], command_list[1]);
}
}
break;
}
}
}
}
else if (pid < 0)
{
// couldnt be forked
perror("PROBLEM OCCURED");
return;
}
else
{
if (check)
{
setpgid(0, 0);
/*
The call setpgrp() is equivalent to setpgid(0,0). setpgid() sets the process group ID of the process specified by pid to pgid. If pid is zero, the process ID of the current process is used. If pgid is zero, the process ID of the process specified by pid is used.*/
}
if (execvp(command_list[0], command_list) < 0)
{
perror("Error");
exit(1);
}
}
}
check = 1; // setting background to one
argc = 0; // so for next the new command list starts from zero
}
// implementation of pipeline
void pipe_line(char *command_list[], int argc, int check)
{
// so we have this new command list to serve as argv for multiple pipe line recursive call
stringg new_comand_list[50];
for (element_type all = 0; all < 50; all++)
{
new_comand_list[all] = (stringg)malloc(sizeof(char) * 100);
}
int pipe_index = -1; // default set to -1 indeed
int checkk = 0;
// this function checks of we have pipe in the command ..so sets the pipe index to index where pipe is
for (int i = 0; i < argc; i++)
{
if (!strcmp(command_list[i], "|"))
{
pipe_index = i;
checkk = 1;
}
if (checkk == 1)
{
break;
}
}
if (pipe_index != -1)
{
int virtual_pipe[2];
// creating virtual pipe in which default command pipe redirects it
int pipe_success = 0;
pipe_success = pipe(virtual_pipe);
if (pipe_success == -1)
{
perror("| COULN'T REDIRECT\n");
return;
}
// Duplicate FD to FD2, closing FD2 and making it open on the same file
int stdoutt = STDOUT_FILENO;
int stdinn = STDIN_FILENO;
int duplicate_out = 0;
duplicate_out = dup2(virtual_pipe[1], stdoutt);
if (duplicate_out == -1)
{
perror("| CAN'T BE DUPLICATED TO STDOUT\n");
return;
}
command_list[pipe_index] = NULL;
// calling cat output.txt for case cat output.txt | head -5 | tail-3 say
handle(command_list, pipe_index, check);
dup2(shell_stdout, stdoutt);
close(virtual_pipe[1]);
int duplicate_in = dup2(virtual_pipe[0], stdinn);
if (duplicate_in == -1)
{
perror("| CAN'T BE DUPLICATED TO STDIN\n");
return;
}
// recursviely calling pipe for further command after the first pipe
int start_new_cmd_list = pipe_index + 1;
for (int j = start_new_cmd_list; j < argc; j++)
{
int new_cmd_lst_ind = j - pipe_index - 1;
strcpy(new_comand_list[new_cmd_lst_ind], command_list[j]);
}
int new_argc = argc - pipe_index - 1;
new_comand_list[new_argc] = NULL;
// new argc become tota argc- (pipeindex+1)(already executed command till this)
pipe_line(new_comand_list, new_argc, check);
close(virtual_pipe[0]);
// in case not dupped due to anyreason output and input are brought back to standard input and output
dup2(shell_stdin, STDIN_FILENO);
dup2(shell_stdout, STDOUT_FILENO);
}
else
{
command_list[argc] = NULL;
// no pipe so simply argc and argv remains the same
handle(command_list, argc, check);
// in case not dupped due to anyreason output and input are brought back to standard input and output
dup2(shell_stdin, STDIN_FILENO);
dup2(shell_stdout, STDOUT_FILENO);
}
}
void execute(stringg command, int check)
{
check = 1;
char *command_list_1[MAX_LIMITS];
element_type argc2 = 0;
int total_ampercent = 0;
// counting total ampercant
for (long long i = 0; i < strlen(command); i++)
{
if (command[i] == '&')
{
total_ampercent++;
}
}
// tokenizing on ampercant
stringg tokenize = strtok(command, "&");
do
{
command_list_1[argc2++] = tokenize;
tokenize = strtok(NULL, "&");
} while (tokenize != NULL);
command_list_1[argc2] = NULL;
for (long long k = 0; k < argc2; k++)
{
char *command_list[MAX_LIMITS];
int argc = 0;
// its checking that if total commands == total ampercant then all are background
// otherwise we have last one has foreground rest as background , so we put check =0 for that particular fg pricess
if (((argc2 - 1) == total_ampercent) && (k == (argc2 - 1)))
{
check = 0;
}
// tokenize on basis of space and tabs.
stringg tokenize1 = strtok(command_list_1[k], " \t\r\n\a");
do
{
command_list[argc++] = tokenize1;
tokenize1 = strtok(NULL, " \t\r\n\a");
} while (tokenize1 != NULL);
command_list[argc] = NULL;
pipe_line(command_list, argc, check);
}
}