-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
262 lines (231 loc) · 6.33 KB
/
shell.c
File metadata and controls
262 lines (231 loc) · 6.33 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
#include "syswrappers.h"
#include "sighandlers.h"
#include "shell.h"
void initJob(job *j) {
j->command = NULL;
j->pgid = 0;
j->first = NULL;
j->inFile = NULL;
j->outFile = NULL;
j->append = 0;
j->background = 0;
}
void delProcess(process *p) {
int i;
for (int i = 0; i < p->argc; ++i) {
if (p->argv[i]) free(p->argv[i]);
}
free(p->argv);
}
void delJob(job *j) {
if (j->command) free(j->command);
if (j->inFile) free(j->inFile);
if (j->outFile) free(j->outFile);
process *p = j->first;
process *tmp;
while (p) {
delProcess(p);
tmp = p->next;
free(p);
p = tmp;
}
}
void addToJob(job *j, process* p) {
process *tmp = j->first;
if (tmp == NULL) {
j->first = p;
return;
}
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = p;
}
int parseCommand(char *command, job *j) {
char *token;
command = strdup(command); // copy of command
strtok(command, "\n"); // remove newline
char *toFree = command;
int i, argc_;
size_t alloc;
argc_ = 0;
char **argv_;
token = strtok(command, " ");
while(token != NULL) {
if (argc_ == 0) {
argv_ = (char**)malloc(1 * sizeof(char*));
alloc = 1;
}
else if (argc_ == alloc) { // resize
argv_ = realloc(argv_, (alloc + 1) * sizeof(char*));
alloc += 1;
}
if (strcmp(token, "|") == 0) {
process *p = (process*)malloc(sizeof(process));
argv_[argc_] = NULL;
p->next = NULL;
p->argv = argv_;
p->argc = argc_;
argc_ = 0;
addToJob(j, p);
}
else if (strcmp(token, "&") == 0) {
j->background = 1;
}
else if (strcmp(token, ">>") == 0) {
j->append = 1;
token = strtok(NULL, " ");
j->outFile = strdup(token);
}
else if (strcmp(token, ">") == 0) {
j->append = 0;
token = strtok(NULL, " ");
j->outFile = strdup(token);
}
else if (strcmp(token, "<") == 0) {
token = strtok(NULL, " ");
j->inFile = strdup(token);
}
else {
argv_[argc_] = strdup(token);
argc_++;
}
token = strtok(NULL, " ");
}
argv_ = realloc(argv_, (alloc + 1) * sizeof(char*));
alloc += 1;
process *p = (process*)malloc(sizeof(process));
argv_[argc_] = NULL;
p->next = NULL;
p->argv = argv_;
p->argc = argc_;
argc_ = 0;
addToJob(j, p);
free(toFree);
}
int stringsEqual(char *s1, char *s2) {
return strcmp(s1, s2) == 0;
}
int pwd() {
// Get PATH_MAX limit for current directory
long size = pathconf(".", _PC_PATH_MAX);
char *path = (char*)malloc(size * sizeof(char));
if (path != NULL) {
path = getcwd(path, (size_t) size);
printf("%s\n", path);
free(path);
return 0;
}
else { // memory allocation error
return -1;
}
}
int commandHandler(char *command) {
job job;
initJob(&job);
parseCommand(command, &job);
Signal(SIGTTIN, SIG_IGN);
Signal(SIGTTOU, SIG_IGN);
pid_t parent_pgid = getpid();
pid_t child_pgid;
Setpgid(parent_pgid, parent_pgid);
int i = 0;
int fdin, fdout, tmpin, tmpout;
sigset_t mask, prev;
tmpin = Dup(STDIN_FILENO);
tmpout = Dup(STDOUT_FILENO);
if (job.inFile != NULL) { // input file is defined
fdin = Open(job.inFile, O_RDONLY);
}
else { // no input file defined
fdin = Dup(tmpin);
}
Tcsetpgrp(tmpin, parent_pgid);
process *currProcess = job.first;
Sigemptyset(&mask);
Sigaddset(&mask, SIGCHLD);
Sigprocmask(SIG_BLOCK, &mask, &prev); /* Block SIGCHLD */
while (currProcess) { // loop over all commands
Dup2(fdin, STDIN_FILENO);
Close(fdin);
if (currProcess->next == NULL) { // last
if (job.outFile != NULL) { // output redirect file defined
if (job.append == 1)
fdout = open(job.outFile, O_CREAT|O_APPEND|O_WRONLY, 0666);
else
fdout = open(job.outFile, O_CREAT|O_WRONLY, 0666);
}
else { // no output redirect file
fdout = dup(tmpout);
}
}
else {
int fd[2];
Pipe(fd);
fdout = fd[1];
fdin = fd[0];
}
Dup2(fdout, STDOUT_FILENO);
Close(fdout);
if (stringsEqual(currProcess->argv[0], "pwd")) {
pwd();
}
else if (stringsEqual(currProcess->argv[0], "cd")) {
chdir(currProcess->argv[1]);
}
else if (stringsEqual(currProcess->argv[0], "exit")) {
delJob(&job);
exit(0);
}
else {
int p_ = Fork();
if (p_ == 0) {
p_ = getpid();
Setpgid(p_, p_);
if (!job.background) {
Tcsetpgrp(tmpin, p_);
}
Execvp(currProcess->argv[0], currProcess->argv);
_exit(1);
}
else {
Setpgid(p_, p_);
FLAG_PID = 0;
if (!job.background) {
while (!FLAG_PID)
Sigsuspend(&prev);
}
}
}
currProcess = currProcess->next;
}
Sigprocmask(SIG_SETMASK, &prev, NULL);
Dup2(tmpin, STDIN_FILENO);
Dup2(tmpout, STDOUT_FILENO);
Close(tmpin);
Close(tmpout);
Tcsetpgrp(STDIN_FILENO, parent_pgid);
delJob(&job);
return true;
}
void mainInputLoop() {
char input[129];
while (true) {
printf("user >");
fgets(input, 129, stdin);
commandHandler(input);
}
}
int main() {
init_handlers();
mainInputLoop();
return 0;
}