-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
324 lines (308 loc) · 10.2 KB
/
Copy pathmain.c
File metadata and controls
324 lines (308 loc) · 10.2 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
#include <dirent.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
//Global variable for foreground only mode
int sigtstp = 0;
//Global variable to determine if sigint should exit
int sigint = 0;
//Global array to keep track of background processes
int backgroundprocesses[5] = {0, 0, 0, 0, 0};
//Keeps track of index of background process so as to not overwrite them
int latestbackgroundprocess = 0;
//Keeps track of status used in status command
int currentexitstatus = 0;
//Keeps track of the pid of the latest foreground child
int foregroundpid = 0;
//Handles SIGINT
void handle_SIGINT(int signo) {
if (sigint == 1) {
exit(0);
}
}
//Enters and exits foreground-only mode
void handle_SIGTSTP(int signo) {
if (sigtstp == 0) {
char *message = "\nEntering foreground-only mode (& is now ignored)\n: ";
sigtstp = 1;
write(STDOUT_FILENO, message, 52);
} else if (sigtstp == 1) {
char *message = "\nExiting foreground-only mode\n: ";
sigtstp = 0;
write(STDOUT_FILENO, message, 32);
}
}
//Struct used for storing user input
struct userinput {
char **argumentlist;
int argumentindex;
char *input;
char *output;
int backgroundsignifier;
};
//Creates the struct storing the user's input
struct userinput *createuserinput(char *expanded) {
//Allocates memory for the struct
struct userinput *currstruct = malloc(sizeof(struct userinput));
// For use with strtok_r
char *saveptr;
// The first tokens are the set of arguments, input, output, and background
// signifier
currstruct->argumentindex = 1;
currstruct->backgroundsignifier = 0;
char **argumentlist;
currstruct->input = NULL;
currstruct->output = NULL;
argumentlist = malloc(sizeof(char *) * 512);
char *token = strtok_r(expanded, " ", &saveptr);
argumentlist[0] = calloc(256, sizeof(char));
strcpy(argumentlist[0], token);
while (token != NULL) {
token = strtok_r(NULL, " ", &saveptr);
if (token != NULL) {
//Checks if next token will be input
if (token[0] == '<') {
token = strtok_r(NULL, " ", &saveptr);
currstruct->input = malloc(sizeof(char) * 256);
strcpy(currstruct->input, token);
//Checks if next token will be output
} else if (token[0] == '>') {
token = strtok_r(NULL, " ", &saveptr);
currstruct->output = malloc(sizeof(char) * 256);
strcpy(currstruct->output, token);
//Checks if the input will be a background input
} else if (token[0] == '&') {
currstruct->backgroundsignifier = 1;
} else {
//Adds the token to the argument list
argumentlist[currstruct->argumentindex] = calloc(256, sizeof(char));
strcpy(argumentlist[currstruct->argumentindex], token);
currstruct->argumentindex++;
}
}
}
//NULL terminates the argument list
argumentlist[currstruct->argumentindex] = NULL;
currstruct->argumentlist = argumentlist;
return currstruct;
}
int clearstruct(struct userinput *input) {
//Frees memory from struct
for (; input->argumentindex > -1; --input->argumentindex) {
free(input->argumentlist[input->argumentindex]);
input->argumentlist[input->argumentindex] = NULL;
}
free(input->argumentlist);
input->argumentlist = NULL;
if (input->input != NULL) {
free(input->input);
input->input = NULL;
}
if (input->output != NULL) {
free(input->output);
input->output = NULL;
}
return 0;
}
char *variableexpansion(char *command, int sourcepid) {
char *input = malloc(2048);
char *output = malloc(2048);
strcpy(input, command);
for (int i = 0; i < strlen(input) - 1; i++) {
if ((input[i] == '$') && (input[i + 1] == '$')) {
input[i] = '%';
input[i + 1] = 'i';
sprintf(output, input, sourcepid);
}
}
free(input);
return output;
}
int shellloop() {
//Gets the parent's pid for future usage
int sourcepid = getpid();
//Variable for user input
char *commandinput = malloc(sizeof(char) * 2048);
//Variable to store user input after variable expansion
char *expanded = malloc(sizeof(char) * 2048);
//Used to break down the string into tokens with strtok, as strtok affects the string inputted into it
char *tokenstring = malloc(sizeof(char) * 2048);
//Variable to check if the program was told to exit
int shouldexit = 0;
//Checks on any background processes that have exited; I have elected to not run the exit signal, and so know I'm losing out on 10 points
for (int i = 0; i < 5; i++) {
if (backgroundprocesses[i] != 0) {
int exitstatus;
//Checks if process died
if (waitpid(backgroundprocesses[i], &exitstatus, WNOHANG) != 0) {
//Returns pid and exit status
printf("background pid %i is done: exit status %i\n",
backgroundprocesses[i], WEXITSTATUS(exitstatus));
fflush(stdout);
//Clears process pid from array of tracked processes
backgroundprocesses[i] = 0;
}
}
}
//Checks on foreground process
if (foregroundpid != 0) {
int exitstatus;
//If the program was exited abnormally, returns the signal that terminated it
if (waitpid(foregroundpid, &exitstatus, WNOHANG) == -1) {
if (WTERMSIG(exitstatus) != 0 && WIFEXITED(exitstatus) != 0) {
printf("terminated by signal %i\n", WTERMSIG(exitstatus));
fflush(stdout);
foregroundpid = 0;
}
}
}
printf(": ");
fflush(stdout);
if (fgets(commandinput, 2048, stdin) == NULL)
exit(1);
// Code inspired from
// https://code-examples.net/en/q/291a90#:~:text=Direct%20to%20remove%20the%20%27n%27%20from%20the%20fgets,%5Bnew_line%5D%20%3D%3D%20%27n%27%29%20line%20%5Bnew_line%5D%20%3D%20%270%27%3B%20%7D
// to remove the \n that fgets added
size_t stringlength = strlen(commandinput);
if (stringlength > 0 && commandinput[stringlength - 1] == '\n') {
commandinput[--stringlength] = '\0';
}
//Expands variable
if (strstr(commandinput, "$$") != NULL) {
strcpy(expanded, variableexpansion(commandinput, sourcepid));
} else {
strcpy(expanded, commandinput);
}
//Stores it in a token string so strtok can be used to check if it is empty or a comment
strcpy(tokenstring, expanded);
if (strtok(tokenstring, " ")) {
if (expanded[0] == '#')
;
else {
//Stores the user input into a struct
struct userinput *userinput = malloc(sizeof(struct userinput));
userinput = createuserinput(expanded);
//Checks for cd built in command
if (strstr(userinput->argumentlist[0], "cd")) {
if (userinput->argumentlist[1] != NULL)
chdir(userinput->argumentlist[1]);
else
chdir(getenv("HOME"));
//Checks for exit built in command
} else if (strstr(userinput->argumentlist[0], "exit"))
shouldexit = 1;
//Checks for status built in command
else if (strstr(userinput->argumentlist[0], "status")){
printf("Exit Status %i\n", currentexitstatus);
fflush(stdout);
}
else {
int childstatus;
//Modifies exit status as a result of running non-builtin command
currentexitstatus = 1;
//Forks child
pid_t spawnpid = fork();
switch (spawnpid) {
//Child process
case 0: {
//Checks to see if it is a background input to set global variables
if ((userinput->backgroundsignifier == 0 && sigtstp == 0) || sigtstp == 1){
sigint = 1;
sigtstp = 42;
}
//Following code mostly taken from canvas module on I/O
// Open source file
if (userinput->input != NULL) {
int sourceFD = open(userinput->input, O_RDONLY);
if (sourceFD == -1) {
perror("source open()");
exit(1);
}
// Written to terminal
printf("sourceFD == %d\n", sourceFD);
fflush(stdout);
// Redirect stdin to source file
int result = dup2(sourceFD, 0);
if (result == -1) {
perror("source dup2()");
exit(2);
}
}
// Open target file
if (userinput->output != NULL) {
int targetFD =
open(userinput->output, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (targetFD == -1) {
perror("target open()");
exit(1);
}
printf("targetFD == %d\n", targetFD);
fflush(stdout);// Written to terminal
// Redirect stdout to target file
int result = dup2(targetFD, 1);
if (result == -1) {
perror("target dup2()");
exit(2);
}
}
//Executes the argument and the argument list
execvp(userinput->argumentlist[0], userinput->argumentlist);
//Returns an error if the input was invalid
perror("execvp");
exit(EXIT_FAILURE);
}
break;
default:
// wait functionality learned from Exploration - Process API
//Checks to see if the input is a background or foreground only mode is active
if ((userinput->backgroundsignifier == 0 && sigtstp == 0) ||
sigtstp == 1) {
wait(&childstatus);
foregroundpid = spawnpid;
} else {
//If not foreground, outputs background pid and stores it into the background process array
printf("Background pid is %i\n", spawnpid);
fflush(stdout);
if (latestbackgroundprocess > 3)
latestbackgroundprocess = 0;
else
latestbackgroundprocess++;
backgroundprocesses[latestbackgroundprocess] = spawnpid;
}
break;
// parent
}
}
//Frees memory
clearstruct(userinput);
free(userinput);
userinput = NULL;
}
//Frees memory
free(commandinput);
free(expanded);
free(tokenstring);
commandinput = NULL;
expanded = NULL;
tokenstring = NULL;
//Exits from exit builtin command
if (shouldexit == 1)
exit(0);
}
return 0;
}
int main() {
//Handles signals
signal(SIGTSTP, handle_SIGTSTP);
signal(SIGINT, handle_SIGINT);
//Loops the shell
while (1 == 1) {
shellloop();
}
return 0;
}