-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvanceShell.c
More file actions
144 lines (130 loc) · 3.95 KB
/
Copy pathAdvanceShell.c
File metadata and controls
144 lines (130 loc) · 3.95 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <fcntl.h>
#define MAX_COMMAND_LENGTH 100
#define MAX_NUM_ARGS 10
// Function to handle signals
void sigintHandler(int signum) {
signal(SIGINT, sigintHandler);
}
int main() {
printf("Unix Shell by K21-3843, K20-1901, and K21-3886\n");
signal(SIGINT, sigintHandler);
while (1) {
char input[MAX_COMMAND_LENGTH];
char *args[MAX_NUM_ARGS];
int inputRedirect = 0;
int outputRedirect = 0;
char inputFile[MAX_COMMAND_LENGTH];
char outputFile[MAX_COMMAND_LENGTH];
printf("Shell> ");
fgets(input, MAX_COMMAND_LENGTH, stdin);
int num_args = 0;
char *token = strtok(input, " \n");
while (token != NULL && num_args < MAX_NUM_ARGS - 1) {
if (strcmp(token, "<") == 0) {
token = strtok(NULL, " \n");
strcpy(inputFile, token);
inputRedirect = 1;
} else if (strcmp(token, ">") == 0) {
token = strtok(NULL, " \n");
strcpy(outputFile, token);
outputRedirect = 1;
} else {
args[num_args++] = token;
}
token = strtok(NULL, " \n");
}
args[num_args] = NULL;
if (num_args == 0) {
continue;
}
if (strcmp(args[0], "exit") == 0) {
exit(0);
}
pid_t pid = fork();
if (pid == 0) {
if (inputRedirect) {
int fd = open(inputFile, O_RDONLY);
if (fd < 0) {
printf("Error opening input file: %s\n", inputFile);
exit(1);
}
dup2(fd, STDIN_FILENO);
close(fd);
}
if (outputRedirect) {
int fd = open(outputFile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
printf("Error opening output file: %s\n", outputFile);
exit(1);
}
dup2(fd, STDOUT_FILENO);
close(fd);
}
int i;
int pipeIndex = -1;
for (i = 0; i < num_args; i++) {
if (strcmp(args[i], "|") == 0) {
pipeIndex = i;
break;
}
}
if (pipeIndex >= 0) {
args[pipeIndex] = NULL;
int pipefd[2];
if (pipe(pipefd) == -1) {
perror("pipe");
exit(1);
}
pid_t pid1 = fork();
if (pid1 == 0) {
close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[1]);
if (execvp(args[0], args) == -1) {
perror("execvp");
exit(1);
}
} else if (pid1 > 0) {
pid_t pid2 = fork();
if (pid2 == 0) {
close(pipefd[1]);
dup2(pipefd[0], STDIN_FILENO);
close(pipefd[0]);
if (execvp(args[pipeIndex + 1], args + pipeIndex + 1) == -1) {
perror("execvp");
exit(1);
}
} else if (pid2 > 0) {
close(pipefd[0]);
close(pipefd[1]);
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
} else {
perror("fork");
exit(1);
}
} else {
perror("fork");
exit(1);
}
} else {
if (execvp(args[0], args) == -1) {
perror("execvp");
exit(1);
}
}
} else if (pid > 0) {
waitpid(pid, NULL, 0);
} else {
perror("fork");
exit(1);
}
}
return 0;
}