-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmallsh.c
More file actions
345 lines (298 loc) · 12.4 KB
/
smallsh.c
File metadata and controls
345 lines (298 loc) · 12.4 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
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
//-- Global constants
#define ARGCOUNT 512 //-- Maximum supported number of arguments
#define CMDLENGTH 2048 //-- Maximum character length of command
//-- Struct to hold information regarding command
struct cmd{
char * args[ARGCOUNT]; //-- Last argument is NULL
int background; //-- 0 - foreground process, 1 - background process
char inputFile[CMDLENGTH];
char outputFile[CMDLENGTH];
};
//-- Global vars for SIGTSTP handler
volatile int SIGTSTP_count = 0; //-- Tracks how many CTRL + Z's there were
volatile int fgRunning = 0; //-- Boolean if foreground is running or not
volatile int SIGTSTP_sent = 0; //-- Tracks if SIGTSTP was sent in middle of foreground process
/*
* Function: processInput
* ----------------------------------------------------------
* Prompts user for input, performs comment detection and
* variable expansion, then parses it into an arguments array.
*
* cmd: cmd struct to be modified
*
* returns: N/A
*/
void processInput(char * input, size_t inputSize, struct cmd * cmd){
//-- Prompts user for input
int currPidNum = getpid();
char currPid[32];
sprintf(currPid, "%d", currPidNum);
printf(":");
fflush(stdout);
getline(&input, &inputSize, stdin);
//-- Strips the newline character from input
input[strcspn(input, "\n")] = 0;
//-- Variable expansion - $$ -> current process id
char * var = strstr(input, "$$");
while(var){
char * suffix = (char *) strdup(var + 2);
char * prefix = strcpy(var, currPid);
strcat(prefix, suffix);
free(suffix);
var = strstr(input, "$$");
}
//-- Command parsing
char * arg = strtok(input, " ");
int i = 0;
while(arg != NULL && i < ARGCOUNT){
if(strcmp(arg, "<") == 0){
//-- Redirect to input file
arg = strtok(NULL, " ");
if(arg != NULL){
strcpy(cmd->inputFile, arg);
arg = strtok(NULL, " ");
}
} else if(strcmp(arg, ">") == 0){
//-- Redirect to input file
arg = strtok(NULL, " ");
if(arg != NULL){
strcpy(cmd->outputFile, arg);
arg = strtok(NULL, " ");
}
} else if(strcmp(arg, "&") == 0){
//-- Check if its last character
arg = strtok(NULL, " "); //-- Sets arg to the next argument in the command
if(arg == NULL && SIGTSTP_count % 2 == 0){
cmd->background = 1;
}
} else if(strcmp(arg, "|") == 0 || strcmp(arg, "!") == 0){
//-- Ignore these shell-specific operators and move on
arg = strtok(NULL, " ");
} else {
cmd->args[i] = arg;
arg = strtok(NULL, " ");
i++;
}
}
}
/*
* Function: displayStatus
* ----------------------------------------------------------
* Decodes the exit method of a process and prints it to the
* screen.
*
* exitMethod: encoded exit method of specified process
*
* returns: N/A
*/
void displayStatus(int exitMethod){
if(WIFEXITED(exitMethod)){
//-- Foreground process exited normally
printf("exit value %d\n", WIFEXITED(exitMethod));
fflush(stdout);
} else if(WIFSIGNALED(exitMethod)){
//-- Foreground process killed by signal
printf("terminated by signal %d\n", exitMethod);
fflush(stdout);
}
}
/*
* Function: handleSIGTSTP
* ----------------------------------------------------------
* Handles the SIGTSTP signal (CTRL + Z) sent by user
*
* returns: N/A
*/
void handleSIGTSTP(int signal){
//-- Increment counter for SIGTSTP signals sent
SIGTSTP_count++;
if(fgRunning){
//-- If foreground process is running
SIGTSTP_sent = 1; //-- Toggle on SIGTSTP sent
return; //-- Exit the function to handle this stuff later
}
//-- Print necessary signal message
if(SIGTSTP_count % 2 == 1){
write(STDOUT_FILENO, "Entering foreground-only mode (& is now ignored)\n:", 50);
} else {
write(STDOUT_FILENO, "Exiting foreground-only mode\n:", 30);
}
return;
}
/*
* Function: main
* ----------------------------------------------------------
* Runs small shell
*
* returns: 0
*/
int main(){
//-- Foreground vars
//-- This section contains information of the latest foreground process
int fgExited = 0; //-- indicates if any foreground process has ran and exited.
int fgExitMethod; //-- stores exit method of latest foreground process.
//-- Background var
int bgProcesses[256] = {}; //-- stores PID's of background processes
//-- Signals
struct sigaction SIGINT_action = {0}, SIGTSTP_action = {0};
SIGINT_action.sa_handler = SIG_IGN;
sigfillset(&SIGINT_action.sa_mask);
SIGINT_action.sa_flags = 0;
sigaction(SIGINT, &SIGINT_action, NULL);
SIGTSTP_action.sa_handler = handleSIGTSTP;
sigfillset(&SIGTSTP_action.sa_mask);
SIGTSTP_action.sa_flags = SA_RESTART; //-- avoid nasty forever while loops (?)
sigaction(SIGTSTP, &SIGTSTP_action, NULL);
//-- Small shell implementation
int run = 1; //-- probably not necessary, but feels wrong to do while(1)
while(run){
//-- Check for background processes
for(int i = 0; i < 256; i++){
int bgExitMethod;
if(bgProcesses[i] > 0 && waitpid(bgProcesses[i], &bgExitMethod, WNOHANG) > 0){
//-- If background process exists AND has completed
printf("background pid %d is done: ", bgProcesses[i]);
fflush(stdout);
displayStatus(bgExitMethod); //-- Display status info on background process
bgProcesses[i] = 0; //-- Background process is completed, reset array element to 0
}
}
//-- Input handling
size_t inputSize = CMDLENGTH;
char * input = (char *) malloc(inputSize + 1);
struct cmd * cmd = malloc(sizeof(struct cmd));
processInput(input, inputSize, cmd);
//-- Process commands
if(cmd->args[0] == NULL || cmd->args[0][0] == '#'){
continue;
} else if(strcmp(cmd->args[0], "exit") == 0){
//-- Built-in command: exit
pid_t currGpid = getpgrp(); //-- gets group ID of parent (also group ID of children by default)
killpg(currGpid, SIGKILL);
run = 0;
return 0; //-- Deadchecks the shell
} else if(strcmp(cmd->args[0], "status") == 0){
//-- Built-in command: status
if(fgExited){
displayStatus(fgExitMethod);
} else {
printf("exit value 0\n");
fflush(stdout);
}
} else if(strcmp(cmd->args[0], "cd") == 0){
//-- Built-in command: cd
if(cmd->args[1] == NULL){
//-- No path -> use HOME environment
char * homeEnv = getenv("HOME");
chdir(homeEnv);
} else {
chdir(cmd->args[1]);
}
} else {
//-- Custom commands
pid_t pid = fork();
switch(pid){
case 0:
{
//-- Input file redirection
int sourceFD;
if(strcmp(cmd->inputFile, "")){
//-- Redirect stdin to read from input file
sourceFD = open(cmd->inputFile, O_RDONLY);
if(sourceFD == -1){ perror("source open()"); exit(1);}
int result = dup2(sourceFD, 0);
if(result == -1){ perror("source dup2()"); exit(2);}
} else if(cmd->background){
//-- Redirect stdin to read from /dev/null if it's a background process
sourceFD = open("/dev/null", O_RDONLY);
if(sourceFD == -1){ perror("source open()"); exit(1);}
int result = dup2(sourceFD, 0);
if(result == -1){ perror("source dup2()"); exit(2);}
}
//-- Output file redirection
int targetFD;
if(strcmp(cmd->outputFile, "")){
//-- Redirect stdout to output file
targetFD = open(cmd->outputFile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if(targetFD == -1){ perror("target open()"); exit(1);}
int result = dup2(targetFD, 1);
if(result == -1){ perror("target dup2()"); exit(2);}
} else if(cmd->background){
//-- Redirect stdout to /dev/null if it's a background process
targetFD = open("/dev/null", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if(targetFD == -1){ perror("target open()"); exit(1);}
int result = dup2(targetFD, 1);
if(result == -1){ perror("target dup2()"); exit(2);}
}
//-- Signal handling
if(!cmd->background){
SIGINT_action.sa_handler = SIG_DFL;
sigaction(SIGINT, &SIGINT_action, NULL);
}
SIGTSTP_action.sa_handler = SIG_IGN;
sigaction(SIGTSTP, &SIGTSTP_action, NULL);
//-- Custom command exectuion
execvp(cmd->args[0], cmd->args);
printf("bash: %s: command not found\n", cmd->args[0]);
fflush(stdout);
exit(1);
break;
}
case -1:
perror("pid not found\n");
exit(-1);
break;
}
if(!cmd->background){
//-- Foreground process
fgRunning = 1; //-- Toggles on foreground process running
waitpid(pid, &fgExitMethod, 0); //-- Immediately updates fgExitMethod upon finishing
fgRunning = 0; //-- Toggles off foreground process running
if(WTERMSIG(fgExitMethod) == SIGINT){
printf("terminated by %d\n", WTERMSIG(fgExitMethod));
fflush(stdout);
}
if(SIGTSTP_sent){
//-- If SIGTSTP was sent in middle of foreground process
//-- Toggle off SIGTSTP sent
SIGTSTP_sent = 0;
//-- Print necessary signal message
if(SIGTSTP_count % 2 == 1){
printf("Entering foreground-only mode (& is now ignored)\n");
fflush(stdout);
} else {
printf("Exiting foreground-only mode\n");
fflush(stdout);
}
}
fgExited = 1;
} else {
//-- Background process
//-- Display background process PID
printf("background pid is %d\n", pid);
fflush(stdout);
//-- Store background process PID in array
for(int i = 0; i < 256; i++){
if(bgProcesses[i] == 0){
bgProcesses[i] = pid;
break;
}
}
}
//-- End of command processing block
}
//-- End of giant while loop
}
return 0;
}