-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy-shell.c
More file actions
456 lines (377 loc) · 14.6 KB
/
my-shell.c
File metadata and controls
456 lines (377 loc) · 14.6 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
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
// C Program to design a shell in Linux, Project 2
// CS 4348.004
// David Nguyen : dxn180015
// Dueñes Gomez : lxd180007
// Luiz Astorga : laa180001
// fixes implicit declaration of functions error (getline, strdup)
#define _GNU_SOURCE
// fd for stdin
#define READ_END 0
// fd for stdout
#define WRITE_END 1
// used for arrays
#define MAX 100
#include <stdio.h> // fgets() , stdin, stderr
#include <stdlib.h> // basic
#include <string.h> // strcmp()
#include <sys/types.h> // pid_t
#include <sys/wait.h> // wait()
#include <unistd.h> // getpid(), execvp()
#include <sys/stat.h> // mkdir
#include <fcntl.h> // visible from sys/stat.h, unistd.h
// prototypes
/**
printShellPromt : this simply prints out the directory and prompts the user like a command
prompt would.
**/
void printShellPrompt();
/**
tokenizeInput: Takes in the user input, which is a shell command, and tokenizes it. This
Will count the number of arguments and store the arguements in an array.
**/
int tokenizeInput(char* input, char* argv[MAX]);
/**
checkPipes: parses the user input and documents each '|' location. Then sets the
location to where '|' was to NULL
**/
void checkPipes(char* argv[MAX], int* numPipes, int pipeIndexes[MAX], int numArgs);
/**
checkInOut : parses the user input and documents the location of each '<', '>', and '>>'.
then sets the location to where I/O commands were to NULL. Also determines to
which command section the redirection operations are applied to. Each section is separated by a pipe '|'.
**/
void checkInOut(char* argv[MAX], int* numInOut, int inOutIndexes[MAX], int numArgs, int inOutType[MAX], int pipeIndexes[MAX], int inOutSection[MAX]);
/**
checkCustomCommands: for commands that execvp doesn't cover, a custom approach was taken
to cover for that, such as 'cd', 'exit', 'quit', ctrl + d, pushd, popd, or dirs
**/
int checkCustomCommands(char* argv[MAX], char* dirs[MAX], int* numDirs);
/**
executeCommand: This is the main program that parses through the pipe(s) and redirection(s)
operetors to execute the commands
**/
void executeCommand(char* argv[MAX], int numPipes, int pipeIndexes[MAX], int numInOut, int inOutIndexes[MAX], int inOutType[MAX], int inOutSections[MAX]);
// Driver function
int main() {
// holds user input and arguments
char inputBuffer[MAX];
char* argv[MAX];
// for pushd, popd, dirs stack
char* dirs[MAX];
int numDirs = 0;
dirs[0] = NULL;
// infinite loop until the user chooses to exit
while (1) {
// reset start of both arrays to null
inputBuffer[0] = '\0';
argv[0] = NULL;
// prompts user for input
printShellPrompt();
int numPipes = 0;
int pipeIndexes[MAX] = { 0 };
// holds how many I/O operations are in the entire input
int numInOut = 0;
// holds the index of each of the I/O operators
int inOutIndexes[MAX] = { 0 };
// hold the "type" ie <, >, >> of each I/O operator
int inOutType[MAX] = { 0 };
// holds what section the I/O operator is being applied to
int inOutSection[MAX] = { 0 };
// gets user input
if (fgets(inputBuffer, sizeof(inputBuffer), stdin) != 0) {
// tokenizes input string
int numArgs = tokenizeInput(inputBuffer, argv);
// go to next loop interration when user enters \n only
if (numArgs == 0)
continue;
// check for pipes
checkPipes(argv, &numPipes, pipeIndexes, numArgs);
// check for I/O
checkInOut(argv, &numInOut, inOutIndexes, numArgs, inOutType, pipeIndexes, inOutSection);
// if no custom user command, execute command
if (!checkCustomCommands(argv, dirs, &numDirs))
executeCommand(argv, numPipes, pipeIndexes, numInOut, inOutIndexes, inOutType, inOutSection);
}
else
{
// exits program when users enter ctrl + d (Null/EOF)
if (strcmp(inputBuffer, "\0") == 0) {
printf("\nGoodbye\n");
exit(0);
}
}
}
return 0;
}
// print shell prompt
void printShellPrompt() {
// get username
char* username = getenv("USER");
// get current working directory
char currWorkDir[MAX];
getcwd(currWorkDir, sizeof(currWorkDir));
// print shell
printf("@%s:%s$ ", username, currWorkDir);
}
// tokenizes the input into words
int tokenizeInput(char* input, char* argv[MAX]) {
int i = 0;
char* words[MAX];
char* piece;
// places next word into piece
piece = strtok(input, " \n");
// transfer word pieces into words[]
while (piece != NULL) {
words[i++] = strdup(piece);
//printf("|%s|\n", piece);
piece = strtok(NULL, " \n");
}
// each word copied to argv[] (index 0 contains command)
int j;
for (j = 0; j < i; j++)
argv[j] = words[j];
// NULL terminate argv array
argv[i] = NULL;
return i;
}
// finds the number and location of pipes
void checkPipes(char* argv[MAX], int* numPipes, int pipeIndexes[MAX], int numArgs)
{
// first command index
pipeIndexes[0] = 0;
// finds number of pipes
int i;
for (i = 0; i < numArgs; i++) {
if (strcmp(argv[i], "|") == 0) {
// set | to null
argv[i] = NULL;
// increment numPipes and use that as the index
// in pipeIndexes array and set value there to i + 1
// (where command word would be at)
numPipes[0] = numPipes[0] + 1;
pipeIndexes[numPipes[0]] = i + 1;
}
}
}
// finds the location of I/O redirection, saves their index, type, and applied section
void checkInOut(char* argv[MAX], int* numInOut, int inOutIndexes[MAX], int numArgs, int inOutType[MAX], int pipeIndexes[MAX], int inOutSection[MAX])
{
// first command index
inOutIndexes[0] = 0;
// starts at first section 0, ie cdm0 would be section 0 in the input cdm0 | cdm1 | ... | cdmn
int section = 0;
// finds number of I/O operators
int i, j;
for (i = 0, j = 1; i < numArgs; i++) {
// checks if there is an I/O in input, skips indexes where pipes are located at do avoid strcmp(NULL)
// and determines which pipe section the redirection operators are in
if (i == pipeIndexes[j] - 1) {
j++;
section++;
}
else {
if ((strcmp(argv[i], "<") == 0) || (strcmp(argv[i], ">") == 0) || (strcmp(argv[i], ">>") == 0))
{
// keeps track of what TYPE of I/O argument was used with the follwoing values:
// < : 1, > : 2, >> : 3
if (strcmp(argv[i], "<") == 0)
{
inOutType[*numInOut] = 1;
inOutSection[*numInOut] = section;
}
else if (strcmp(argv[i], ">") == 0)
{
inOutType[*numInOut] = 2;
inOutSection[*numInOut] = section;
}
else
{
inOutType[*numInOut] = 3;
inOutSection[*numInOut] = section;
}
// set I/O to null
argv[i] = NULL;
// increment numInOut and use that as the index
// in inOutIndexes array and set value there to i + 1
// (where command word would be at)
(*numInOut)++;
inOutIndexes[*numInOut] = i + 1;
}
}
}
}
// checks for custom commands
int checkCustomCommands(char* argv[MAX], char* dirs[MAX], int* numDirs) {
int hasCustomCommand = 0;
// doesn't catch ctrl + d for some reason
// exits program when users enter ctrl + d (Null/EOF)
if (argv[0] == NULL || strcmp(argv[0], "\0") == 0) {
printf("\nGoodbye\n");
exit(0);
}
// exits program when user enters exit or quit
if (argv[1] == NULL) {
if (strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "quit") == 0) {
printf("Goodbye\n");
exit(0);
}
}
// changes the directory in parent when command is 'cd'
if (strcmp(argv[0], "cd") == 0) {
hasCustomCommand = 1;
chdir(argv[1]);
}
// pushd
if (strcmp(argv[0], "pushd") == 0) {
hasCustomCommand = 1;
// push current working directory to stack and increment numDirs
char currWorkDir[MAX];
dirs[(*numDirs)] = strdup(get_current_dir_name());
(*numDirs)++;
// changes directory to user's inputted path
chdir(argv[1]);
}
// popd
if (strcmp(argv[0], "popd") == 0) {
hasCustomCommand = 1;
// nothing in stack
if ((*numDirs) == 0) {
printf("popd: directory empty\n");
}
// pop stack and chdir
else {
(*numDirs)--;
chdir(dirs[(*numDirs)]);
//printf("|%s|\n", dirs[(*numDirs)]);
}
}
// dirs
if (strcmp(argv[0], "dirs") == 0) {
hasCustomCommand = 1;
// get current working directory
char currWorkDir[MAX];
getcwd(currWorkDir, sizeof(currWorkDir));
// stack empty
if ((*numDirs) == 0) {
// print current directory
printf("%s\n", currWorkDir);
}
// stack not empty
else {
// print current directory
printf("%s ", currWorkDir);
// print dirs array stack (top to bottom)
for (int i = (*numDirs) - 1; i >= 0; i--) {
printf("%s ", dirs[i]);
}
printf("\n");
}
}
return hasCustomCommand;
}
// fork and execute command in child process
void executeCommand(char* argv[MAX], int numPipes, int pipeIndexes[MAX], int numInOut, int inOutIndexes[MAX], int inOutType[MAX], int inOutSection[MAX]) {
int fdRightPipe[2] = { -1, -1 };
int fdLeftPipe[2] = { -1, -1 };
pid_t pid;
// hold file descriptor for inout file and output file
int fdin;
int fdout;
// go through X total number of commands and execute
int i;
for (i = 0; i < numPipes + 1; i++) {
// create pipe if have multible commands and is not last command
if (i != numPipes) {
if (pipe(fdRightPipe) == -1) {
fprintf(stderr, "Pipe creation failed.\n");
exit(1);
}
}
// split into 2 processes (parent and child have same copy of the pipe)
pid = fork();
// check if fork worked
if (pid < 0) {
perror("forking error");
exit(1);
}
// child process
else if (pid == 0) {
// if is not first command
if (i != 0) {
// link file descriptor for std input to read end of left pipe
dup2(fdLeftPipe[READ_END], 0);
// close the unneeded descriptor
close(fdLeftPipe[READ_END]);
}
// if have multible commands and is not last command,
if (i != numPipes) {
// close read end of right pipe
close(fdRightPipe[READ_END]);
// link file descriptor for std output to write end of right pipe
dup2(fdRightPipe[WRITE_END], 1);
// close the unneeded descriptor
close(fdRightPipe[WRITE_END]);
}
// get starting index of next command
int cmdIndex = pipeIndexes[i];
// checks if a redirection operator is in the current pipe/command section
// b used to iterate though inOutSection to check if the currect section being
// exectued, is the same section as where I/O operator was found
int b;
for (b = 0; b < numInOut; b++) {
if (inOutSection[b] == i)
{
// argv[inOutIndexes[i+1]] hold the name of the file
// switch input from STDIN to input file if '<' is present
if (inOutType[(b)] == 1) {
fdin = open(argv[inOutIndexes[(b)+1]], O_RDONLY, 0644);
dup2(fdin, 0);
close(fdin);
// if < and > are in the same command
if (numPipes < numInOut) {
if (inOutType[(b)+1] == 2) {
fdout = open(argv[inOutIndexes[(b)+2]], O_WRONLY | O_CREAT | O_TRUNC, 0644);
dup2(fdout, 1);
close(fdout);
}
if (inOutType[(b)+1] == 3) {
fdout = open(argv[inOutIndexes[(b)+2]], O_WRONLY | O_CREAT | O_APPEND, 0644);
dup2(fdout, 1);
close(fdout);
}
}
}
// if '>' redirection is present
if (inOutType[(b)] == 2) {
fdout = open(argv[inOutIndexes[(b)+1]], O_WRONLY | O_CREAT | O_TRUNC, 0644);
dup2(fdout, 1);
close(fdout);
}
// if '>>' redirection is present
if (inOutType[(b)] == 3) {
fdout = open(argv[inOutIndexes[(b)+1]], O_WRONLY | O_CREAT | O_APPEND, 0644);
dup2(fdout, 1);
close(fdout);
}
}
}
// executes the shell command
if (execvp(argv[cmdIndex], &argv[cmdIndex]) < 0)
perror("exec error");
exit(0); // exits if error
}
// parent waits for child process to finish
else
{
wait(NULL);
// printf("\n%d\n", (*x));
// close write end of right pipe descriptor in the parent process
close(fdRightPipe[WRITE_END]);
// if not first command, close previous left pipe's read end
if (i != 0)
close(fdLeftPipe[READ_END]);
// saves right pipe with only read end open as left pipe
fdLeftPipe[READ_END] = fdRightPipe[READ_END];
}
}
}