-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
375 lines (363 loc) · 9 KB
/
shell.c
File metadata and controls
375 lines (363 loc) · 9 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
/*
ASSIGNMENT 2
GROUP ID-29
GROUP MEMBERS-
NAME-BHUPESH SINGH KAINTH
ROLL NO.-2017040
NAME- SHLOAK AGGARWAL
ROLL NO.-2017107
NAME-SHASHWAT JAIN
ROLL NO.-2017103
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include<errno.h>
#include<sys/wait.h>
#include <unistd.h>
#include <ctype.h>
#include<signal.h>
/* 0644 indicates permissions */
/*
* redirect stdout to file “filename”. If the file does not exist create one,
* otherwise, overwrite the existing file
*/
void redirectOutputToFile (char *file)
{
int o = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0644);
dup2(o, STDOUT_FILENO);
close(o);
}
/*
* redirect stderr to filename
*/
void redirectErrorToFile (char *file)
{
int o = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0644);
dup2(o, STDERR_FILENO);
close(o);
}
/*
* If the file already exists, appends the stdout output, otherwise, creates a new file.
*/
void appendOutputToFile (char *file)
{
int o = open(file, O_CREAT|O_APPEND|O_WRONLY, 0644);
dup2(o, STDOUT_FILENO);
close(o);
}
/*
* If the file already exists, appends the stderr output, otherwise, creates a new file.
*/
void appendErrorToFile (char *file)
{
int o = open(file, O_CREAT|O_APPEND|O_WRONLY, 0644);
dup2(o, STDERR_FILENO);
close(o);
}
/*
* Redirects stderr to stdout
*/
void redirectErrorToOutput ()
{
dup2(STDOUT_FILENO, STDERR_FILENO);
}
/*
* use file descriptor 0 (stdin) for filename. If command tries to read from
* stdin, effectively it will read from filename.
*/
void redirectFileToInput (char *file)
{
int o = open(file, O_RDONLY);
dup2(o, STDIN_FILENO);
close(o);
}
/*
* Function to check the type of redirection operator(">",">>",">&","<").It calls the corresponding operator function
*/
int checkRedirection (char *p)
{
int r = 0;
for(int i = 0; i < strlen(p); i++)
{
if (p[i] == '1')
{
if (p[i+1] == '>')
{
if (p[i+2] == '>')
{
appendOutputToFile(p+i+3);
}
else
{
redirectOutputToFile(p+i+2);
}
r = 1;
break;
}
}
else if (p[i] == '2')
{
if (p[i+1] == '>')
{
if (p[i+2] == '>')
{
appendErrorToFile(p+i+3);
}
else if (p[i+2] == '&')
{
redirectErrorToOutput();
}
else
{
redirectErrorToFile(p+i+2);
}
r = 1;
break;
}
}
else if (p[i] == '>')
{
if (p[i+1] == '>')
{
appendOutputToFile(p+i+2);
}
else
{
redirectOutputToFile(p+i+1);
}
r = 1;
break;
}
else if (p[i] == '<')
{
redirectFileToInput(p+i+1);
r = 1;
break;
}
}
return r;
}
/*
* Function to carry out pipelining. It takes a commands array and its length
* as input. An error is thrown if a pipe cannot be created or forking fails.
* "execvp" is used to execute commands with a variable number of arguments.
*/
void pipefn (char *const cmds[], int lenCmds)
{
int fd[2];
int pid;
int outputStorage = 0;
for(int i = 0; i < lenCmds-1; i++)
{
if (pipe(fd) == -1)
{
perror("Error creating pipe!");
}
pid = fork();
if (pid == -1)
{
perror("Error forking");
}
else if (pid == 0)
{
close(fd[0]);
dup2(outputStorage, 0);
dup2(fd[1], 1);
// dup2 lets you choose the file descriptor number that will be
// assigned and atomically closes and replaces it if it's already taken.
int j = 0;
char *p = strtok(cmds[i], " ");
char *temp[10];
while (p != NULL)
{
int r = checkRedirection(p);
if (r == 0)
{
temp[j++] = p;
}
p = strtok (NULL, " ");
}
temp[j] = NULL;
execvp(temp[0], temp);
}
else
{
if (i < lenCmds-2)
{
wait(NULL);
close(fd[1]);
outputStorage = fd[0];
}
else
{
wait(NULL);
close(fd[1]);
dup2(fd[0], 0);
int k = 0;
char *p = strtok(cmds[i+1], " ");
char *temp[10];
while (p != NULL)
{
int s = checkRedirection(p);
if (s == 0)
{
temp[k++] = p;
}
p = strtok (NULL, " ");
}
temp[k] = NULL;
execvp(cmds[i+1], temp);
}
}
}
}
/*
* Trims provided string to remove leading and trailing whitespace.
* Function referred from: cboard.cprogramming.com/c-programming/31839-trim-string-function-code.html
*/
char *trim (char *cmd)
{
int a = strlen ( cmd ) - 1;
while ( isspace ( cmd[a] ) && a >= 0 )
{
a--;
}
int b = 0;
while ( isspace ( cmd[b] ) && cmd[b] != '\0' )
{
b++;
}
int c = 0;
while ( b <= a )
{
cmd[c++] = cmd[b++];
}
cmd[c] = '\0';
return cmd;
}
int main ()
{
signal(SIGINT,SIG_IGN); /* Handles SIGINT i.e Ctrl+C interrupt.Kills the currently executing process,
and starts waiting for an input command. */
while(1)
{
char sentence[100];
printf("Shell$ ");
fgets(sentence, 100, stdin);
if(strcmp(trim(sentence),"exit")==0) /* Exit from the shell program */
{
exit(0);
}
int countpipe=0;
int countout=0;
int countin=0;
int countappend=0;
int countfd=0;
for(int i=0;i<strlen(sentence);++i)
{
if(sentence[i]=='|')
{
countpipe=countpipe+1;
}
else if(sentence[i]=='<')
{
countout=countout+1;
}
else if(sentence[i]=='>')
{
if(sentence[i+1]=='>')
{
countappend=countappend+1;
}
else if(sentence[i+1]=='&')
{
countfd=countfd+1;
}
else
{countin=countin+1;
}
}
}
// This is the case when there is an internal command.
if (countpipe==0 && countout==0 && countin==0 && countappend==0 && countfd==0)
{
int j=0;
char *p1=strtok(sentence," \n");
char *a1[10];
while(p1!=NULL)
{
a1[j++]=p1;
p1=strtok(NULL," \n");
}
for(int y=0;y<j;y++)
{
if(strcmp(a1[y],"cd")==0) // Implementing cd on my own.
{
char e[100];
chdir(a1[y+1]);
printf("%s \n",getcwd(e,100)); // Printing the directory to check function validity.Prints the current directory
}
else
{
char *temp[] = {sentence, NULL};
int pid = fork();
if (pid == 0)
{
execvp(temp[0], temp);
}
else
{
wait(NULL);
}
}
}
}
else if (countpipe == 0)
{
int pid = fork();
if (pid == 0)
{
int x = 0;
char *y = strtok(sentence, " ");
char *arr[10];
while (y != NULL)
{
int r = checkRedirection(trim(y));
if (r == 0)
{
arr[x++] = trim(y);
}
y = strtok (NULL, " ");
}
arr[x] = NULL;
execvp(arr[0], arr);
}
else
{
wait(NULL);
}
}
else
{
int i = 0;
char *p = strtok(sentence, "|");
char *array[50];
while (p != NULL)
{
array[i++] = trim(p);
p = strtok (NULL, "|");
}
int pid = fork();
if (pid == 0)
{
pipefn(array, i);
}
else
{
wait(NULL);
}
}
}
}