-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell24.c
851 lines (779 loc) · 24.4 KB
/
shell24.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
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#define MAX_ARGS 5
#define MAX_COMMAND_LENGTH 1000
#define MAX_NUMBER_OF_COMMANDS 20
#define MAX_BG_PROCESSES 100
// This KeyValuePair array holds all the commands
struct KeyValuePair
{
char **cmd; // Pointer to the command array
int cmdLen; // Length of the command array
char *cmdSuffix; // This will determine the special character joiner
};
int isCommandValid;
int bgProcessArr[MAX_BG_PROCESSES];
int bgProcessCount = 0;
int isFgProcess = 0;
// Function to expand tilde character to user's home directory
char *expandTilde(const char *path)
{
if (path[0] == '~')
{
const char *homeDir = getenv("HOME");
char *expandedPath = malloc(strlen(homeDir) + strlen(path));
strcpy(expandedPath, homeDir);
strcat(expandedPath, path + 1); // Skip '~'
return expandedPath;
}
else
{
return strdup(path);
}
}
int isMultiCharOp(char c)
{
return c == '>' || c == '&' || c == '|';
}
void addSpaces(char *command)
{
// Length of the command
size_t len = strlen(command);
// Buffer to store the modified command
char modified[MAX_COMMAND_LENGTH * 2]; // Assuming worst case scenario for length
// Initialize the buffer index
size_t j = 0;
// Iterate through each character in the command
for (size_t i = 0; i < len; i++)
{
char currentChar = command[i];
// Check if the current character is a symbol
if (currentChar == '#' || currentChar == '<' || currentChar == ';')
{
// Add a space before the symbol if necessary
if (i > 0 && command[i - 1] != ' ')
{
modified[j++] = ' ';
}
// Add the symbol itself
modified[j++] = currentChar;
// Add a space after the symbol if necessary
if (i < len - 1 && command[i + 1] != ' ')
{
modified[j++] = ' ';
}
}
else if (isMultiCharOp(currentChar))
{
// Check if the current character is part of a multi-character operator
if (i < len - 1 && command[i + 1] == currentChar)
{
// Multi-character operator found, add a space before and after if necessary
if (i > 0 && command[i - 1] != ' ')
{
modified[j++] = ' ';
}
modified[j++] = currentChar;
modified[j++] = currentChar;
// Skip the next character
i++;
// Add a space after the symbol if necessary
if (i < len - 1 && command[i + 1] != ' ')
{
modified[j++] = ' ';
}
}
else
{
// Single character operator found, add spaces around it if necessary
if (i > 0 && command[i - 1] != ' ')
{
modified[j++] = ' ';
}
modified[j++] = currentChar;
// Add a space after the symbol if necessary
if (i < len - 1 && command[i + 1] != ' ')
{
modified[j++] = ' ';
}
}
}
else
{
// Copy non-symbol characters as-is
modified[j++] = currentChar;
}
}
// Null-terminate the modified string
modified[j] = '\0';
// Copy the modified string back to the original command
strcpy(command, modified);
}
void parseInput(char *input, struct KeyValuePair *keyValuePairs, int *keyValuePairSize)
{
int argc = 0;
keyValuePairs[*keyValuePairSize].cmd = malloc((MAX_ARGS + 1) * sizeof(char *));
if (keyValuePairs[*keyValuePairSize].cmd == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
// Iterate through the command to divide the command based on spaces
char *saveptr; // Pointer used by strtok_r for thread safety
char *token = strtok_r(input, " ", &saveptr);
while (token != NULL)
{
// Expland the path if it exists in the command
if (strstr(token, "~/") != NULL)
{
token = expandTilde(token);
}
if (token[0] == '\"')
{
// Token starts with a quote, indicating the start of a quoted string
char *endQuote = strchr(token + 1, '\"'); // Find the end quote
if (endQuote == NULL)
{
// If end quote is not found, it means the quoted string spans multiple tokens
// Concatenate tokens until the end quote is found
char *concatToken = malloc(strlen(token) + 1); // Allocate memory for the concatenated token
if (concatToken == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
strcpy(concatToken, token + 1); // Copy the substring after the opening quote
while (endQuote == NULL)
{
token = strtok_r(NULL, " ", &saveptr);
if (token == NULL)
{
printf(stderr, "Syntax error: Unmatched double quote\n");
return;
}
if (strchr(token, '\"') != NULL)
{
endQuote = strchr(token, '\"');
token[endQuote - token] = '\0'; // Null-terminate the string at the end quote
}
concatToken = realloc(concatToken, strlen(concatToken) + strlen(token) + 1);
if (concatToken == NULL)
{
perror("realloc");
exit(EXIT_FAILURE);
}
strcat(concatToken, " ");
strcat(concatToken, token);
}
token = concatToken;
}
else
{
// Token is a single quoted string
*endQuote = '\0'; // Null-terminate the string at the end quote
// Remove the first character (quote) from the token
memmove(token, token + 1, strlen(token)); // Shift the string one character to the left
}
}
if (strcmp(token, "#") == 0 || strcmp(token, "|") == 0 || strcmp(token, ">>") == 0 || strcmp(token, ">") == 0 || strcmp(token, "<") == 0 || strcmp(token, "&&") == 0 || strcmp(token, "||") == 0 || strcmp(token, ";") == 0)
{
// Add NULL pointer to terminate the argument list
keyValuePairs[*keyValuePairSize].cmd[argc] = NULL;
keyValuePairs[*keyValuePairSize].cmdLen = argc;
// check if the arguments are greater than 5 and less than 1, if yes then exit
if (argc > MAX_ARGS || argc < 1)
{
if (*keyValuePairSize != 0 || (*keyValuePairSize == 0 && argc > MAX_ARGS))
{
printf("Individual commands cannot be greater than 5 and less than 1 arguments\n");
}
isCommandValid = 0;
break;
}
keyValuePairs[*keyValuePairSize].cmdSuffix = strdup(token);
(*keyValuePairSize)++;
argc = 0;
// Dynamically assign value to (*keyValuePairSize)++;
keyValuePairs[*keyValuePairSize].cmd = malloc((MAX_ARGS + 1) * sizeof(char *));
}
else
{
keyValuePairs[*keyValuePairSize].cmd[argc] = strdup(token);
argc++;
}
token = strtok_r(NULL, " ", &saveptr);
}
keyValuePairs[*keyValuePairSize].cmd[argc] = NULL;
keyValuePairs[*keyValuePairSize].cmdLen = argc;
keyValuePairs[*keyValuePairSize].cmdSuffix = NULL;
// check if the arguments are greater than 5 and less than 1, if yes then exit
if (isCommandValid && (argc > MAX_ARGS || argc < 1))
{
if (*keyValuePairSize != 0 || (*keyValuePairSize == 0 && argc > MAX_ARGS))
{
printf("Individual commands cannot be greater than 5 and less than 1 arguments\n");
}
isCommandValid = 0;
}
(*keyValuePairSize)++;
}
void printKeyValuePair(struct KeyValuePair kvp)
{
printf("cmdLen: %d\n", kvp.cmdLen);
printf("cmdSuffix: %s\n", kvp.cmdSuffix);
printf("cmd:");
for (int i = 0; i < kvp.cmdLen; i++)
{
printf(" %s", kvp.cmd[i]);
}
printf("\n");
}
void openNewTerminal()
{
pid_t pid = fork();
if (pid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
if (pid > 0)
{
// Parent process does not wait for the child to complete immediately
return;
}
else if (pid == 0)
{
// Launch a new instance of shell24 in a new Bash terminal
execlp("x-terminal-emulator", "x-terminal-emulator", "-e", "./shell24", NULL);
perror("execlp");
exit(EXIT_FAILURE);
}
}
// This function will execute the command
void executeCommand(char *args[], int argc)
{
pid_t pid = fork();
if (pid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
if (pid > 0)
{
// Parent process waitinng for child to complete
wait(NULL);
}
else if (pid == 0)
{
// Execute commands
if (execvp(args[0], args) == -1)
{
perror("execvp");
}
}
}
// Checks if there is a combination of special characters used, If yes returns 0
int ifValidSpecialChar(struct KeyValuePair *keyValuePairs, int keyValuePairSize, char *specialChar)
{
for (int i = 0; i < keyValuePairSize - 1; i++)
{
if (strcmp(keyValuePairs[i].cmdSuffix, specialChar) != 0)
{
// return 0 because it conatins combinations
printf("Combination of special characters cannot be used %s and %s\n", specialChar, keyValuePairs[i].cmdSuffix);
return 0;
}
}
return 1;
}
// Concatenate contents of text files
void fileConcatenation(struct KeyValuePair *keyValuePairs, int keyValuePairSize, char *specialChar)
{
if (!ifValidSpecialChar(keyValuePairs, keyValuePairSize, specialChar))
{
return;
}
// Open output file for writing concatenated contents
FILE *outputFile = fopen("output.txt", "w");
if (outputFile == NULL)
{
perror("Failed to open output file");
exit(EXIT_FAILURE);
}
for (int i = 0; i < keyValuePairSize; i++)
{
// Open input file for reading
FILE *inputFile = fopen(keyValuePairs[i].cmd[0], "r");
if (inputFile == NULL)
{
printf("Failed to open input file %s\n", keyValuePairs[i].cmd[0]);
fclose(outputFile);
remove("output.txt");
return;
}
// Read contents of input file and write to output file
char buffer[1024];
while (fgets(buffer, sizeof(buffer), inputFile) != NULL)
{
fputs(buffer, outputFile);
}
// Close input file
fclose(inputFile);
// Close output file
if (i != keyValuePairSize - 1)
{
// Add newline between concatenated files
fputs(" ", outputFile);
}
}
// Close output file
fclose(outputFile);
// Open output file to print concatenated contents
FILE *resultFile = fopen("output.txt", "r");
if (resultFile == NULL)
{
perror("Failed to open output file");
exit(EXIT_FAILURE);
}
// Print concatenated contents
char buffer[1024];
while (fgets(buffer, sizeof(buffer), resultFile) != NULL)
{
printf("%s", buffer);
}
printf("\n");
// Close output file
fclose(resultFile);
// Remove temporary output file
remove("output.txt");
}
// Piping operation
void pipeOperation(struct KeyValuePair *keyValuePairs, int keyValuePairSize, char *specialChar)
{
if (!ifValidSpecialChar(keyValuePairs, keyValuePairSize, specialChar))
{
return;
}
// Initialize pipes
int pipes[keyValuePairSize - 1][2];
for (int i = 0; i < keyValuePairSize - 1; i++)
{
if (pipe(pipes[i]) == -1)
{
perror("pipe");
exit(EXIT_FAILURE);
}
}
// Execute commands
for (int i = 0; i < keyValuePairSize; i++)
{
pid_t pid = fork();
if (pid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
// Child process
if (i > 0)
{
// Connect input to previous pipe
if (dup2(pipes[i - 1][0], STDIN_FILENO) == -1)
{
perror("dup2");
exit(EXIT_FAILURE);
}
}
if (i < keyValuePairSize - 1)
{
// Connect output to next pipe
if (dup2(pipes[i][1], STDOUT_FILENO) == -1)
{
perror("dup2");
exit(EXIT_FAILURE);
}
}
// Close all pipe descriptors
for (int j = 0; j < keyValuePairSize - 1; j++)
{
close(pipes[j][0]);
close(pipes[j][1]);
}
// Execute command
if (execvp(keyValuePairs[i].cmd[0], keyValuePairs[i].cmd) == -1)
{
perror("execvp");
}
}
}
// Close all pipe descriptors in parent
for (int i = 0; i < keyValuePairSize - 1; i++)
{
close(pipes[i][0]);
close(pipes[i][1]);
}
// Wait for all child processes to finish
for (int i = 0; i < keyValuePairSize; i++)
{
wait(NULL);
}
}
// Redirection
void redirection(struct KeyValuePair *keyValuePairs, int keyValuePairSize, char *specialChar)
{
// Open the file based on the redirection operator
int fileDescriptor;
if (strcmp(specialChar, ">") == 0)
{
fileDescriptor = open(keyValuePairs[1].cmd[0], O_WRONLY | O_CREAT | O_TRUNC, 0777);
}
else if (strcmp(specialChar, ">>") == 0)
{
fileDescriptor = open(keyValuePairs[1].cmd[0], O_WRONLY | O_CREAT | O_APPEND, 0777);
}
else if (strcmp(specialChar, "<") == 0)
{
fileDescriptor = open(keyValuePairs[1].cmd[0], O_RDONLY);
}
if (fileDescriptor == -1)
{
perror("open");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
// Child process
// Redirect stdin or stdout to the file
if (strcmp(specialChar, ">") == 0 || strcmp(specialChar, ">>") == 0)
{
if (dup2(fileDescriptor, STDOUT_FILENO) == -1)
{
perror("dup2");
exit(EXIT_FAILURE);
}
}
else if (strcmp(specialChar, "<") == 0)
{
if (dup2(fileDescriptor, STDIN_FILENO) == -1)
{
perror("dup2");
exit(EXIT_FAILURE);
}
}
// Close the file descriptor
close(fileDescriptor);
// Execute the command
if (execvp(keyValuePairs[0].cmd[0], keyValuePairs[0].cmd) == -1)
{
perror("execvp");
exit(EXIT_FAILURE);
}
}
// Close the file descriptor in the parent process
close(fileDescriptor);
// Wait for the child process to finish
wait(NULL);
}
void conditionalExecution(struct KeyValuePair *keyValuePairs, int keyValuePairSize)
{
int status; // Used to store the exit status of the executed commands
// Iterate through each command in the key-value pairs
for (int i = 0; i < keyValuePairSize; i++)
{
// Execute the command
pid_t pid = fork();
if (pid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
// Child process
if (execvp(keyValuePairs[i].cmd[0], keyValuePairs[i].cmd) == -1)
{
perror("execvp");
}
}
else
{
// Parent process
waitpid(pid, &status, 0); // Wait for child process to complete
if (WIFEXITED(status))
{
int exit_status = WEXITSTATUS(status);
// Check for conditional execution operators
if ((keyValuePairs[i].cmdSuffix != NULL) && (strcmp(keyValuePairs[i].cmdSuffix, "&&") == 0))
{
// If the previous command succeeded, proceed to the next command
if (exit_status != 0)
{
i++; // Skip the next command
}
}
else if ((keyValuePairs[i].cmdSuffix != NULL) && strcmp(keyValuePairs[i].cmdSuffix, "||") == 0)
{
// If the previous command failed, proceed to the next command
if (exit_status == 0)
{
i++; // Skip the next command
}
}
}
}
}
}
void sequentialExecution(struct KeyValuePair *keyValuePairs, int keyValuePairSize, char *specialChar)
{
if (!ifValidSpecialChar(keyValuePairs, keyValuePairSize, specialChar))
{
return;
}
// Iterate through each command in the key-value pairs
for (int i = 0; i < keyValuePairSize; i++)
{
pid_t pid = fork();
if (pid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
if (pid > 0)
{
// Parent process waiting for child to complete
int status;
if (waitpid(pid, &status, 0) == -1)
{
perror("waitpid");
exit(EXIT_FAILURE);
}
}
else if (pid == 0)
{
// Execute commands
executeCommand(keyValuePairs[i].cmd, keyValuePairs[i].cmdLen);
// Exit the child process after command execution
exit(EXIT_SUCCESS);
}
}
}
// Method to add a value to the array
void addToBgProcessArr(int value)
{
if (bgProcessCount < MAX_BG_PROCESSES)
{
bgProcessArr[bgProcessCount++] = value;
}
else
{
printf("Error: Background process array is full.\n");
}
}
// Method to read the last element of the array
int readLastBgProcess()
{
if (bgProcessCount > 0)
{
return bgProcessArr[bgProcessCount - 1];
}
else
{
printf("Error: Background process array is empty.\n");
return -999; // Return a default value indicating an error
}
}
// Method to remove the last element of the array
void removeLastBgProcess()
{
if (bgProcessCount > 0)
{
bgProcessCount--;
}
else
{
printf("Error: Background process array is empty.\n");
}
}
void pushBackground(char *args[], int argc)
{
// args[argc - 1] = NULL;
pid_t pid = fork();
if (pid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
if (pid > 0)
{
addToBgProcessArr(pid);
// Set the process group ID of the current process to its own PID
if (setpgid(pid, pid) == -1)
{
perror("setpgid");
exit(EXIT_FAILURE);
}
printf("Program is running in the background with PID: %d\n", pid);
}
else if (pid == 0)
{
// Execute commands
if (execvp(args[0], args) == -1)
{
perror("execvp");
}
}
}
void sigint_handler(int signum)
{
// Handle SIGINT signal (Ctrl+C) in the parent process
if (isFgProcess && kill(readLastBgProcess(), signum) == 0)
{
printf("Process with PID %d killed successfully", readLastBgProcess());
removeLastBgProcess();
}
}
int main()
{
// Register SIGINT signal handler for the parent process
if (signal(SIGINT, sigint_handler) == SIG_ERR)
{
perror("signal");
exit(EXIT_FAILURE);
}
// This will be the whole command as a string
char command[MAX_COMMAND_LENGTH];
while (1)
{
isCommandValid = 1;
printf("shell24$ ");
if (fgets(command, sizeof(command), stdin) == NULL)
{
perror("fgets");
exit(EXIT_FAILURE);
}
// Remove newline character
command[strcspn(command, "\n")] = '\0';
// Add spaces in between commands if it does not exist
addSpaces(command);
// Initialize an array of KeyValuePair with max size 5
struct KeyValuePair *keyValuePairs = malloc(MAX_NUMBER_OF_COMMANDS * sizeof(struct KeyValuePair));
int keyValuePairSize = 0;
// Parse input into arguments
parseInput(command, keyValuePairs, &keyValuePairSize);
if (!isCommandValid)
{
continue;
}
// Print each KeyValuePair
// for (int i = 0; i < keyValuePairSize; i++) {
// printf("KeyValuePair %d:\n", i + 1);
// printKeyValuePair(keyValuePairs[i]);
// printf("\n");
// }
// Execute command
if (strcmp(keyValuePairs[0].cmd[0], "newt") == 0)
{
// If there is junk values along with newt
if (keyValuePairs[0].cmdLen > 1)
{
printf("Invalid Command\n");
}
else
{
printf("Creating a new shell24 session...\n");
// Fork and execute a new instance of shell24
openNewTerminal();
}
}
else if ((keyValuePairSize == 1) && (keyValuePairs[0].cmdLen == 2) && (strcmp(keyValuePairs[0].cmd[1], "&") == 0))
{
// Push the process to the background
pushBackground(keyValuePairs[0].cmd, keyValuePairs[0].cmdLen);
}
else if ((keyValuePairSize == 1) && (keyValuePairs[0].cmdLen == 1) && (strcmp(keyValuePairs[0].cmd[0], "fg") == 0))
{
isFgProcess = 1;
waitpid(readLastBgProcess(), NULL, 0);
isFgProcess = 0;
}
else if (keyValuePairs[0].cmdSuffix == NULL)
{
// There is only one command without any spacial characters
executeCommand(keyValuePairs[0].cmd, keyValuePairs[0].cmdLen);
}
else if (strcmp(keyValuePairs[0].cmdSuffix, "#") == 0)
{
if (keyValuePairSize > 6)
{
printf("More than 5 operations are not allowed\n");
}
else
{
// Txt file concatenation upto 5 concatinations
fileConcatenation(keyValuePairs, keyValuePairSize, "#");
}
}
else if (strcmp(keyValuePairs[0].cmdSuffix, "|") == 0)
{
if (keyValuePairSize > 7)
{
printf("More than 6 pipes are not allowed\n");
}
else
{
// Implement code for piping
pipeOperation(keyValuePairs, keyValuePairSize, "|");
}
}
else if ((strcmp(keyValuePairs[0].cmdSuffix, ">>") == 0) || (strcmp(keyValuePairs[0].cmdSuffix, ">") == 0) || (strcmp(keyValuePairs[0].cmdSuffix, "<") == 0))
{
if (keyValuePairSize > 2)
{
printf("More than 2 commands not allowed for %s\n", keyValuePairs[0].cmdSuffix);
}
else
{
// Implement code for redirection
redirection(keyValuePairs, keyValuePairSize, keyValuePairs[0].cmdSuffix);
}
}
else if ((strcmp(keyValuePairs[0].cmdSuffix, "&&") == 0) || (strcmp(keyValuePairs[0].cmdSuffix, "||") == 0))
{
if (keyValuePairSize > 6)
{
printf("More than 5 conditional operations are not allowed\n");
}
else
{
// Conditional execution
conditionalExecution(keyValuePairs, keyValuePairSize);
}
}
else if (strcmp(keyValuePairs[0].cmdSuffix, ";") == 0)
{
if (keyValuePairSize > 5)
{
printf("More than 5 commands are not allowed\n");
}
else
{
// Sequential execution
sequentialExecution(keyValuePairs, keyValuePairSize, ";");
}
}
}
return 0;
}