-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyshell.c
More file actions
80 lines (69 loc) · 1.75 KB
/
myshell.c
File metadata and controls
80 lines (69 loc) · 1.75 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
#include <linux/limits.h>
#include <stdio.h>
#include "LineParser.h"
#include <signal.h>
int main(int argc, char **argv)
{
char path[PATH_MAX];
char input[2048];
cmdLine *cmd;
FILE * inputStream = stdin;
int debug = 0;
for (int i = 1; i < argc; i++){
if (strcmp(argv[i], "-d") == 0){
debug = 1;
}
}
while (1)
{
printf("Current working directory is: %s\n", getcwd(path, PATH_MAX));
fgets(input, 2048, inputStream);
cmd = parseCmdLines(input);
if (debug)
{
fprintf(stderr, "PID: %d\n", getpid());
fprintf(stderr, "Executing command: %s\n", cmd->arguments[0]);
}
if (strcmp(cmd->arguments[0], "quit") == 0)
{
freeCmdLines(cmd);
break;
}
else if(strcmp(cmd->arguments[0], "cd") == 0){
if (chdir(cmd->arguments[1]) == -1) {
perror("Error");
}
}
else if(strcmp(cmd->arguments[0], "nuke") == 0){
kill(atoi(cmd->arguments[1]), SIGINT);
}
else if(strcmp(cmd->arguments[0], "wakeup") == 0){
kill(atoi(cmd->arguments[1]), SIGCONT);
}
else{
execute(cmd);
}
}
}
void execute(cmdLine *pCmdLine)
{
int pid = fork();
if (!pid)
{
if(pCmdLine->inputRedirect)
{
close(stdin);
fopen(pCmdLine->inputRedirect, "r");
}
if(pCmdLine->outputRedirect)
{
close(stdout);
fopen(pCmdLine->outputRedirect, "w");
}
execvp(pCmdLine->arguments[0], pCmdLine->arguments);
perror("Error");
exit(1);
}
if(pCmdLine->blocking)
waitpid(pid, NULL, 0);
}