-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellHelpers.c
More file actions
677 lines (616 loc) · 19.8 KB
/
Copy pathshellHelpers.c
File metadata and controls
677 lines (616 loc) · 19.8 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
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#include "shellHelpers.h"
/**
* @brief given command, parses it accordingly
* @post call freeCMD()
* @param input char*, cmd line input, as is (raw)
* @return CMD* struct of the cmd
*/
CMD *fillCMD(char *input) {
if (input == NULL)
return NULL;
strtok(input, "\n"); // trailing /n
CMD *cmd = malloc(sizeof(CMD));
cmd->args = malloc(sizeof(char *));
{ // default initialization
cmd->pipedIndex = 0;
cmd->background = 0;
cmd->invalid = 0;
cmd->src = NULL;
cmd->dest = NULL;
}
split(cmd, input);
cmd->args = realloc(cmd->args, sizeof(char *) * (cmd->argCount + 2)); // add additional space for NULL int he array of words
cmd->args[cmd->argCount] = NULL; // add a NULL to the array (in case everything is tip-top, execvp still needs NULL)
for (int i = 0; i < cmd->argCount; i++) { // go through every word in the array of words
char *currStr = cmd->args[i]; // create locl reference
if (currStr == NULL) // if argument is null, no need to read it
continue;
if (!strncmp(">", currStr, 1)) { // destFile is found
fileIOParse(cmd, &i, &(cmd->dest));
if (cmd->invalid)
break;
} else if (!strncmp("<", currStr, 1)) { // src file is found
fileIOParse(cmd, &i, &(cmd->src));
if (cmd->invalid)
break;
} else if (!strncmp("|", currStr, 1)) { // pipe is found
cmd->pipedIndex = i + 1;
if (cmd->args[cmd->pipedIndex] == NULL) {
cmd->invalid = 1;
break;
}
free(currStr);
cmd->args[i] = NULL;
} else if (!strncmp("&", currStr, 1)) { // background process is found
cmd->background = 1;
free(currStr);
cmd->args[i] = NULL;
}
}
return cmd;
}
/**
* @brief given input string, splits args into char** of CMD* struct
*
* @param cmd CMD* struct, to store char** args into
* @param input char*, input cmd raw line
*/
void split(CMD *cmd, char *input) {
char *token = strtok(input, " "); // token to use for strtok()
for (cmd->argCount = 0; token != NULL; cmd->argCount++) { // go through every "word" of the input
cmd->args = realloc(cmd->args, sizeof(char *) * (cmd->argCount + 1)); // realloc char** in CMD to account for +1 word
cmd->args[cmd->argCount] = malloc(strlen(token) + 1); // malloc strlen(word) + 1 (for \0)
// printf("token:%s\n", token);
strcpy(cmd->args[cmd->argCount], token); // copy word into array of words
token = strtok(NULL, " "); // get next word (token)
}
}
/**
* @brief used by fillCMD. if > is one of the args, sets up a file name in the CMD struct
*
* @param cmd CMD* struct to read from
* @param index index of the arg, where >/< is
* @param dest char* filename destination to store to
*/
void fileIOParse(CMD *cmd, int *index, char **dest) {
int argExists = 0; // assume filename DNE
char *currStr = cmd->args[*index];
char *tempDest = cmd->args[*index + 1]; // local refernce to possible filename
if (cmd->args[*index + 1] != NULL) { // if filename exists (> filename, as opposed ot (> (null))
argExists = 1;
*dest = malloc(strlen(tempDest) + 1);
strcpy(*dest, tempDest); // put filenme into dest
free(tempDest);
cmd->args[*index + 1] = NULL; // unlink
} else {
cmd->invalid = 1;
}
free(currStr); // unlink the > in the args
cmd->args[*index] = NULL;
if (argExists) { // shift everything down by 2 (given filename exists)
for (int j = *index; j < cmd->argCount - 2; j++) {
cmd->args[j] = cmd->args[j + 2];
}
cmd->args[cmd->argCount - 2] = NULL; // set last 2 to NULL, since they were yoinked
cmd->args[cmd->argCount - 1] = NULL;
}
(*index)--;
}
/**
* @brief given CMD*, frees it accordingly
*
* @param cmd CMD* struct, to free
*/
void freeCMD(CMD *cmd) {
if (cmd == NULL) // error checking
return;
for (int i = 0; i < cmd->argCount; i++) {
if (cmd->args[i] != NULL)
free(cmd->args[i]); // go through the args list, free accordingly
}
free(cmd->args); // free array of strings
if (cmd->src != NULL)
free(cmd->src); // if there was a file, free it
if (cmd->dest != NULL)
free(cmd->dest); // ifthere was a file, free it
free(cmd); // free the whole struct
}
/**
* @brief custom function, for testing
*
* @param cmd CMD*, to parse to string
*/
void toString(CMD *cmd) {
// to string, go through every element, print what it is
printf("\nCMD Arguments: \n");
for (int i = 0; i < cmd->argCount; i++) {
printf("\t%s\n", (cmd->args[i] != NULL) ? cmd->args[i] : "NULL");
}
printf("CMD argCount = %d\n", cmd->argCount);
printf("CMD src: %s\n", ((cmd->src == NULL) ? "NULL" : cmd->src));
printf("CMD dest: %s\n", ((cmd->dest == NULL) ? "NULL" : cmd->dest));
printf("Pipe's CMD index: %d\n", cmd->pipedIndex);
printf("Background: %s\n", ((cmd->background) ? "Yes" : "No"));
printf("CMD Valid: %s\n\n", ((cmd->invalid) ? "No" : "Yes"));
}
/**
* @brief given path and comd args, execv
*
* @param path char*, complete path of libraries to check
* @param histPath char*, to write the cmd to
* @param cmd char**, args to execute
* @return int 1 or -1 on fail, killChild on success
*/
int execute(char *path, char *histPath, char **cmd) {
if (path == NULL) { // error checking
return -1;
}
// cusotm history command
if (!strncmp(cmd[0], "history", 7)) {
return history(cmd, histPath);
}
if (!strchr(cmd[0], '/')) {
// copy path to a tempPath
char *mallocPath = malloc(strlen(path) + 1);
strcpy(mallocPath, path);
// separate by :, distinct libraries
char *token = strtok(mallocPath, ":");
while (token != NULL) {
// malloc accordingly
char *possiblePath = malloc(strlen(token) + 2 + strlen(cmd[0]));
// library + / + exe name
sprintf(possiblePath, "%s/%s", token, cmd[0]);
int status = 0;
// printf("%s\n", possiblePath);
// attempt to execute
status = execv(possiblePath, cmd);
// eitehr kills the child, frees the heap, or we free accrodingly and/or try agian with different library
free(possiblePath);
if (status == -1)
token = strtok(NULL, ":");
}
// nothing found, bad
free(mallocPath);
} else {
int status = execv(cmd[0], cmd);
}
return -1;
}
/**
* @brief custom `history` comamnd
*
* @param cmd arguments to store in HISTFILE
* @param histPath char*, path to HISTFILE
* @return int 1 for sucess
*/
int history(char **cmd, char *histPath) {
// printf("printfinh history\n");
FILE *histPoint;
int numDisplay = 0;
for (int i = 0; 1; i++) {
char *token = cmd[i]; //checking the tokens, until null is found (statistically speaking, always)
if (token == NULL)
break;
int tempNum = atoi(token);
if (tempNum)
numDisplay = tempNum; //num to of elemens to display
//if -c flag is found, clear the file, and exit
if (!strcmp("-c", token)) {
histPoint = fopen(histPath, "w");
fclose(histPoint);
numDisplay = -1;
}
}
if (numDisplay < 0) {
return 1;
}
//open for reading
histPoint = fopen(histPath, "r");
if (numDisplay == 0) {
while (!feof(histPoint)) { //read until the end, display that many
char tempStr[513];
fgets(tempStr, 513, histPoint);
numDisplay++;
}
fclose(histPoint); //reopen the file
histPoint = fopen(histPath, "r");
}
char **queue; //queue to keep track of
queue = malloc(sizeof(char *) * numDisplay); //only trank numDisplay elements
for (int i = 0; i < numDisplay; i++) {
queue[i] = malloc(sizeof(char) * 520);
}
int counter = 0;
for (counter = 0; !feof(histPoint); counter++) { //read until FEOF
char currCmd[512];
char *temp = fgets(currCmd, 512, histPoint); //store to test char*
if (temp != NULL) {
int i;
if (counter < numDisplay) //place at the first instance of NULL
i = counter;
else {
free(queue[0]); //clear first one
for (i = 0; i < numDisplay - 1; i++) {
queue[i] = queue[i + 1]; //shift everything down 1
}
queue[i] = malloc(sizeof(char) * 520); //malloc final boi
}
sprintf(queue[i], " %d %s", counter + 1, currCmd); //rpint to new one
}
}
for (int i = 0; i < numDisplay; i++) {
printf("%s", queue[i]); //print the history
if (queue[i] != NULL)
free(queue[i]);
}
free(queue);
fclose(histPoint);
return 1;
}
/**
* @brief given cmd line, execute the command
*
* @param line char*, cmd line (raw)
* @param myPATH char**, path to HOME
* @param myHOME char**, path to PATH
* @param myHISTFILE cahr**, path to HISTFILE
* @param hp FILE*, HISTFILE pointer to write to
* @param cwd char**, current working dir
* @return int 1 on success
*/
int command(char *line, char **myPATH, char **myHOME, char **myHISTFILE, FILE *hp, char **cwd, bg **head) {
pid_t childpid; /* child's process id */
pid_t grandchildpid;
char *lineCopy = malloc(strlen(line) + 1);
strcpy(lineCopy, line); //need copy for background cmd, fillCMD violates original line
CMD *cmd = fillCMD(line);
// toString(cmd);
if (!strcmp(cmd->args[0], "\n")) //doesn't like \n's
return 1;
if (!strcmp(cmd->args[0], "export")) { //custom command
return export(cmd->args[1], myHOME, myPATH, myHISTFILE);
}
if (!strcmp(cmd->args[0], "cd")) { //custom command
return changeDir(cmd, myHOME, cwd);
}
if (cmd->invalid) { //exit with error
freeCMD(cmd);
free(lineCopy);
printf("Syntax Error\n");
return 1;
}
if (hp != NULL)
fflush(hp);
childpid = fork();
if (childpid < 0) { // child fails
perror("Fork");
exit(-1);
}
if (childpid == 0) { // child process
int fd[2]; //pipe :)
FILE *fw, *fr;
if (cmd->dest != NULL)
fw = freopen(cmd->dest, "w", stdout); //file IO
if (cmd->src != NULL)
fr = freopen(cmd->src, "r", stdin);
if (cmd->pipedIndex > 0) { // need to pipe
if (pipe(fd) < 0) {
exit(-1);
}
grandchildpid = fork();
if (grandchildpid < 0) {
perror("sub-fork");
exit(-1);
} else {
if (grandchildpid == 0) {
dup2(fd[1], STDOUT_FILENO); //redirect right end
close(fd[0]);
close(fd[1]);
int tempStatusGrandchild = execute(*myPATH, *myHISTFILE, cmd->args); //something yucky could happen
if (tempStatusGrandchild == -1 || tempStatusGrandchild == 1) {
if (tempStatusGrandchild == -1)
perror("Pipe");
if (cmd->dest != NULL)
fclose(fw); //close files, yucky happened
if (cmd->src != NULL)
fclose(fr);
free(cmd);
return -1;
}
// execvp(cmd->args[0], cmd->args);
} else {
waitpid(grandchildpid, NULL, WNOHANG); // TODO check for background
dup2(fd[0], STDIN_FILENO); //left end redirection
close(fd[0]);
close(fd[1]);
char **pipedArgs = &(cmd->args[cmd->pipedIndex]); //start char** where pipe command starts in the args
int tempStatus = execute(*myPATH, *myHISTFILE, pipedArgs);
if (tempStatus == -1 || tempStatus == 1) {
if (tempStatus == -1)
perror("Pipe");
if (cmd->dest != NULL)
fclose(fw); //yucky
if (cmd->src != NULL)
fclose(fr);
free(cmd);
return -1;
}
// execvp(pipedArgs[0], pipedArgs);
}
close(fd[0]); //close both ends afterwards
close(fd[1]);
}
} else { //no pipe
int status = execute(*myPATH, *myHISTFILE, cmd->args);
if (status == -1 || status == 1) {
if (status == -1) {
perror("Fork"); //something yucky
}
if (cmd->dest != NULL)
fclose(fw); //close files
if (cmd->src != NULL)
fclose(fr);
free(cmd);
return -1;
}
}
// status = execvp(cmd->args[0], cmd->args);
if (cmd->dest != NULL)
fclose(fw); //all went smooth, close files
if (cmd->src != NULL)
fclose(fr);
} else { // parent process
if (cmd->background) {
bgAppend(head, childpid, lineCopy); //if it was background (WNOHANG was running automatically), add process to LL
} else {
waitpid(childpid, NULL, 0); //wait for child
}
}
free(lineCopy); //all good, free and return success
freeCMD(cmd);
return 1;
}
/**
* @brief reads profile and sets env variuables
*
* @param myHOME char**, sets myHOME variable
* @param myPATH char**, sets myPATH variable
* @param myHISTFILE char**, sets myHISTFILE variable
* @return int 1 for sucess
*/
int startup(char **myHOME, char **myPATH, char **myHISTFILE) {
char *startingFile = malloc(100 + 20);
sprintf(startingFile, "%s/.CIS3110_profile", *myHOME); //profile is located in home/....
// sprintf(startingFile, "%s/.CIS3110_profile", "/home/undergrad/2/ukaparyk/");
FILE *fp;
fp = fopen(startingFile, "r"); //attempt to open
if (fp == NULL) {
printf("Could not open profile\n");
return -1;
}
char fileCMD[512]; //read line by line
fgets(fileCMD, 512, fp);
for (int i = 0; !feof(fp); i++) {
int cmd = command(fileCMD, myPATH, myHOME, myHISTFILE, NULL, NULL, NULL); //execute `export` and set variable accordingly
fgets(fileCMD, 512, fp);
}
free(startingFile);
fclose(fp);
return 1;
}
/**
* @brief used in startup reading. sets myHOME, myPATH, and myHISTFILE variables given the profile
*
* @param input char*, command line to export
* @param myHOME char**, myHOME variable to edit
* @param myPATH char**, myPATH variableto edit
* @param myHISTFILE char**, myHISTFILe variable to edit
* @return int 0 on success
*/
int export(char *input, char **myHOME, char **myPATH, char **myHISTFILE) {
if (input == NULL)
return 0;
//copy everything jst in case
char *copyInput = malloc(strlen(input) + 1);
strcpy(copyInput, input);
char *copyInput2 = malloc(strlen(input) + 1);
strcpy(copyInput2, input);
char *copyHome = malloc(strlen(*myHOME) + 1);
strcpy(copyHome, *myHOME);
char *copyPath = malloc(strlen(*myPATH) + 1);
strcpy(copyPath, *myPATH);
char *copyHist = malloc(strlen(*myHISTFILE) + 1);
strcpy(copyHist, *myHISTFILE);
char *token = strtok(copyInput, "$");
char *parsedString = malloc(strlen(token) + 1);
strcpy(parsedString, token);
char **unparsedVars = malloc(sizeof(char *));
int counter = 0;
token = strtok(NULL, "$");
for (counter = 0; token != NULL; counter++) {
//store unparsed everything in unparsedVars
//example could be myHOME/something
//myHISTFILE
//myPATH:/usr/bin
unparsedVars = realloc(unparsedVars, sizeof(char *) * (counter + 1)); //realloc array of strings, and store the boi in there
unparsedVars[counter] = malloc(strlen(token) + 1);
strcpy(unparsedVars[counter], token);
// printf("%d %s\n", counter, token);
token = strtok(NULL, "$"); //keep going
}
for (int i = 0; i < counter; i++) { //go through every unparsedVar
int j;
int delimType = 0;
int varType = 0;
for (j = 0; j < strlen(unparsedVars[i]); j++) { //find the position of the delimiter: ':', '/' or \0
if (unparsedVars[i][j] == '/' || unparsedVars[i][j] == ':') {
delimType = (unparsedVars[i][j] == '/') ? 1 : 2; //indicate teh type to trim later
break;
}
}
if (!strncmp(unparsedVars[i], "myHOME", j)) { //at the delimiter, some variable Name is placed. check if it's one of the allowed ones
unparsedVars[i] = realloc(unparsedVars[i], strlen(unparsedVars[i]) + strlen(copyHome) + 3); //realloc accordingly
varType = 1;
} else if (!strncmp(unparsedVars[i], "myPATH", j)) {
unparsedVars[i] = realloc(unparsedVars[i], strlen(unparsedVars[i]) + strlen(copyPath) + 3);
varType = 2;
} else if (!strncmp(unparsedVars[i], "myHISTFILE", j)) {
unparsedVars[i] = realloc(unparsedVars[i], strlen(unparsedVars[i]) + strlen(copyHist) + 3);
varType = 3;
}
//leftovers to append after the variable surgey went successful
char *leftovers = strtok(unparsedVars[i], (delimType == 1) ? "/" : ((delimType == 2) ? ":" : "\n")); //find the delimiter, trim at it, NULL will contain leftovers
leftovers = strtok(NULL, "\n"); //can guarantee there will be no \n's, will go till the end
char *properLeftovers; //possible storing spot for the leftover (not on stack)
if (leftovers == NULL) {
properLeftovers = malloc(1); //leftovers are not guaranteed to exist
strcpy(properLeftovers, "");
} else {
properLeftovers = malloc(strlen(leftovers) + 1);
strcpy(properLeftovers, leftovers);
}
char *varName; //just prettier, not mandatory
switch (varType) {
case 1:
varName = copyHome;//depending on the variable type, replace the variable with that needs be
break;
case 2:
varName = copyPath;
break;
case 3:
varName = copyHist;
break;
default:
varName = ""; //if doesn;t match, replace with ""
break;
}
sprintf(unparsedVars[i], "%s%s%s", varName, (delimType == 1) ? "/" : ((delimType == 2) ? ":" : ""), properLeftovers); //reattach proper full variable, proper delim, and leftover (if any)
free(properLeftovers); //no need for the storage for leftovers anymore
}
for (int i = 0; i < counter; i++) { //go through all (un)parsedVars
parsedString = realloc(parsedString, strlen(parsedString) + strlen(unparsedVars[i]) + 2); //realloc and append the parsed vars
// sprintf(parsedString, "%s/%s", parsedString, unparsedVars[i])
strcat(parsedString, unparsedVars[i]);
free(unparsedVars[i]); //no need to store parsed vars anymore, already concat'ed, can free
}
free(unparsedVars);
// printf("parsed: %s\n", parsedString);
//checking what __= is
char *envVarType = strtok(parsedString, "=");
char *tempDest = strtok(NULL, "\n");
//don't keep env var on stack
char *destination = malloc(strlen(tempDest) + 1);
strcpy(destination, tempDest);
//check which one it is
if (!strcmp(envVarType, "myHOME")) {
free(*myHOME);
*myHOME = destination; //rePoint
} else if (!strcmp(envVarType, "myPATH")) {
// printf("here\n");
free(*myPATH);
*myPATH = destination;
} else if (!strcmp(envVarType, "myHISTFILE")) {
// printf("here2\n");
free(*myHISTFILE);
*myHISTFILE = destination;
}
//free, well, fucken everything
free(parsedString);
free(copyInput);
free(copyInput2);
free(copyHome);
free(copyPath);
free(copyHist);
return 0;
}
/**
* @brief custom `cd` command
*
* @param cmd CMD* struct, parsed from raw line
* @param myHOME char**, myHOME variable, used for cd _
* @param cwd char**, need to change cwd once change dirs
* @return int 1
*/
int changeDir(CMD *cmd, char **myHOME, char **cwd) {
if (cmd->args[1] == NULL || !strcmp(cmd->args[1], "")) {
chdir(*myHOME); //if no arg was given, cd to home
free(*cwd);
*cwd = getcwd(NULL, 0); //rePoint cwd
} else if (!chdir(cmd->args[1])) {
free(*cwd); //destination was given
*cwd = getcwd(NULL, 0); //rePoint cwd
} else {
perror("cd"); //didn't work :(
}
return 1;
}
/**
* @brief background Process append (to Linked List)
*
* @param head bg**, head node
* @param pid pid_t, pid
* @param line char*, raw cmd line
* @return int 1 for success
*/
int bgAppend(bg **head, pid_t pid, char *line) {
bg *node = malloc(sizeof(bg)); //create new node
bg *travNode = *head;
bg *prevNode;
int counter = 1;
if (*head == NULL) { //no head >:(
*head = node; //set this to be the new head
node->index = 1;
} else {
//yes head :)
//traverse til lthe end
for (counter = 1; travNode != NULL; travNode = travNode->next, counter++) {
prevNode = travNode;
}
//follow up
prevNode->next = node;
node->index = prevNode->index + 1;
}
//malloc accordingly for cmd
node->cmd = malloc(strlen(line) + 1);
strcpy(node->cmd, line);
//is at the end, points to null
node->next = NULL;
node->pid = pid;
//print the one that just got created, like bash
printf("[%d] %d\n", node->index, node->pid);
return 1;
}
/**
* @brief checks for any dead processes, deletes off LL, prints contents
*
* @param head bg**, head of LL
* @return int 1 for success
*/
int checkIfActive(bg **head) {
bg *temp = *head;
bg *prevNode = NULL;
int status;
while (temp != NULL) {
//check the pid and update the status
if (waitpid(temp->pid, &status, WNOHANG) > 0) {
if (WIFEXITED(status)) { //if the status changed (child done)
printf("[%d]\tDone\t\t%s", temp->index, temp->cmd); //print the child done and it's features
if (temp == *head) { //free if is teh head
if (temp->cmd != NULL)
free(temp->cmd);
temp = temp->next;
free(*head);
*head = temp; //repoint the Head
continue;
} else { //free otherwise
prevNode->next = temp->next;
if (temp->cmd != NULL)
free(temp->cmd);
free(temp);
}
}
}
prevNode = temp; //follow up
temp = temp->next; //traverse
}
}