-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsc60mshell.c
More file actions
251 lines (228 loc) · 6.79 KB
/
csc60mshell.c
File metadata and controls
251 lines (228 loc) · 6.79 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#define MAXLINE 80
#define MAXARGS 20
pid_t pid = -1;
static void sig_Handler(int signum)
{
if(pid == 0) exit(0);
}
/* ----------------------------------------------------------------- */
/* Iterate through commands - Handle pipe and redirect */
/* ----------------------------------------------------------------- */
void process_input(int argc, char **argv) {
int count = argc;
int openFlags = O_CREAT | O_WRONLY | O_TRUNC;
int filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
char inFlag = 0;
char outFlag = 0;
int fd_out = fileno(stdout);
int fd_in = fileno(stdin);
/* Loop through arguments, detecting errors and redirecting as necessary */
while (count > 0) {
count--;
if (!strcmp(argv[count], "<") || !strcmp(argv[count], ">")) {
// No redirection symbols as argv[0]
if(count == 0) {
perror("ERROR - No command.\n");
_exit(EXIT_FAILURE);
}
// All redirections must have a source or target
if(count+1 >= argc) {
perror("ERROR - No redirection file specified\n");
_exit(EXIT_FAILURE);
}
}
if (!strcmp(argv[count], "<"))
{
// Only one input redirection per command
if(inFlag) {
perror("ERROR - Can't have two input redirects on one line.\n");
_exit(EXIT_FAILURE);
}
// Redirect input
int newIn = open(argv[count+1], O_RDONLY);
if(newIn == -1) {
perror("ERROR - Cannot open input redirection.\n");
_exit(EXIT_FAILURE);
}
else {
fd_in = dup(newIn);
close(newIn);
argv[count] = NULL;
argc = count;
}
inFlag = 1;
}
else if (!strcmp(argv[count], ">"))
{
// Only one output redirection per command
if(outFlag) {
perror("ERROR - Can't have two output redirects on one line.\n");
_exit(EXIT_FAILURE);
}
// Redirect output
int newOut = open(argv[count+1], openFlags, filePerms);
if(newOut == -1) {
perror("ERROR - Cannot open redirected output.\n");
_exit(EXIT_FAILURE);
}
else {
fd_out = dup(newOut);
close(newOut);
argv[count] = NULL;
argc = count;
}
outFlag = 1;
}
}
/* multipipe implementation */
int pipeCount = 0;
int pipePos = 0; //Position of previous pipe
char **commands[MAXARGS];
int pipefds[MAXARGS-1];
memset(&commands[0], 0, sizeof(commands));
/* parse argv into individual commands */
for (count = 0; count < argc; count++) {
if (!strcmp(argv[count], "|")) {
commands[pipeCount] = argv + pipePos;
commands[pipeCount][count - pipePos] = NULL;
pipeCount++;
pipePos = count + 1;
}
}
commands[pipeCount] = argv + pipePos;
int i = 0;
int p[2];
pid_t pid;
while (commands[i])
{
pipe(p);
if ((pid = fork()) == -1)
{
perror("ERROR - Pipe fork failed");
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
dup2(fd_in, 0); //change the input according to the old one
if (commands[i+1])
dup2(p[1], 1);
else
dup2(fd_out, 1);
close(p[0]);
execvp(commands[i][0], commands[i]);
perror("ERROR - Execution failure");
exit(EXIT_FAILURE);
}
else
{
wait(NULL);
close(p[1]);
fd_in = p[0]; //save the input for the next command
i++;
}
}
exit(EXIT_SUCCESS);
}
/* ----------------------------------------------------------------- */
/* parse input line into argc/argv format */
/* ----------------------------------------------------------------- */
int parseline(char *cmdline, char **argv)
{
int count = 0;
char *separator = " \n\t";
argv[count] = strtok(cmdline, separator);
while ((argv[count] != NULL) && (count+1 < MAXARGS)) {
argv[++count] = strtok((char *) 0, separator);
}
return count;
}
/* ----------------------------------------------------------------- */
/* The main program starts here */
/* ----------------------------------------------------------------- */
int main(void)
{
struct sigaction parent_handler;
parent_handler.sa_handler = SIG_IGN;
sigemptyset(&parent_handler.sa_mask);
parent_handler.sa_flags = 0;
sigaction(SIGINT, &parent_handler, NULL);
struct sigaction child_handler;
child_handler.sa_handler = SIG_DFL;
sigemptyset(&child_handler.sa_mask);
child_handler.sa_flags = 0;
char cmdline[MAXLINE];
char *argv[MAXARGS];
int argc;
int status;
/* Loop forever to wait and process commands */
while (1) {
// Display prompt and wait for a command
printf("csc60mshell> ");
fgets(cmdline, MAXLINE, stdin);
// Restart loop on carriage return
argc = parseline(cmdline, argv);
if (argc == 0) continue;
// Handle exit command
if (argc == 1 && !strcmp(argv[0], "exit")) { exit(0); }
// Handle pwd command
if (argc == 1 && !strcmp(argv[0], "pwd")) {
char cwd[1024];
getcwd(cwd, sizeof(cwd));
printf("%s\n", cwd);
continue;
}
// Handle cd command
if (!strcmp(argv[0], "cd")) {
// Check for acceptable argc
if(argc > 2) perror("ERROR - Too many arguments for cd command\n");
// If there are no additional arguments, change to the home directory.
else if(!argv[1]) chdir(getenv("HOME"));
// Change to the argument directory, using a relative or absolute path
else {
char dir[1024] = { 0 };
// Enable the relative path
if(argv[1][0] != '/') {
getcwd(dir, sizeof(dir));
strcat(dir, "/");
}
strcat(dir, argv[1]);
chdir(dir);
}
// Update the PWD environment variable
char dir[1024];
getcwd(dir, sizeof(dir));
setenv("PWD", dir, 1);
continue;
}
/* Step 1: Else, fork off a process */
pid = fork();
if (pid == -1) {
perror("Shell Program fork error");
_exit(EXIT_FAILURE);
}
else if (pid == 0) {
/* I am child process. I will execute the command, call: execvp */
sigaction(SIGINT, &child_handler, NULL);
process_input(argc, argv);
}
else {
/* I am parent process */
if (wait(&status) == -1)
perror("ERROR - Child return error");
if (WIFEXITED(status)) /* process exited normally */
printf("child process exited with value %d\n", WEXITSTATUS(status));
else if (WIFSIGNALED(status)) /* child exited on a signal */
printf("child process exited due to signal %d\n", WTERMSIG(status));
else if (WIFSTOPPED(status)) /* child was stopped */
printf("child process was stopped by signal %d\n", WIFSTOPPED(status));
}
}
}