-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell4.c
More file actions
385 lines (355 loc) · 6.64 KB
/
shell4.c
File metadata and controls
385 lines (355 loc) · 6.64 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
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/wait.h>
#include<signal.h>
#include<string.h>
/*void command_filename_overwrite(char* command[],char filename[]){
int pid;
if((pid=fork())==0){
dup2(open(filename,O_RDWR|O_CREAT,0666),1);
signal(SIGINT,SIG_DFL);
int status=execvp(command[0],command);
if(status==-1){
write(1,"Error in executing command \n",28);
}
exit(0);
}
else{
wait(NULL);
}
}
void command_filename_append(char* command[],char filename[]){
int pid;
if((pid=fork())==0){
dup2(open(filename,O_RDWR|O_APPEND|O_CREAT,0666),1);
signal(SIGINT,SIG_DFL);
int status=execvp(command[0],command);
if(status==-1){
write(1,"Error in executing command \n",28);
}
exit(0);
}
else{
wait(NULL);
}
}*/
void redirectstdout(char filename[],int app){
close(1);
if(app==1)
dup(open(filename,O_RDWR|O_APPEND|O_CREAT,0666));
else if (app==0)
dup(open(filename,O_RDWR|O_CREAT,0666));
}
void redirectstderr(char filename[],int app){
close(2);
if(app==1)
dup(open(filename,O_RDWR|O_APPEND|O_CREAT,0666));
else if (app==0)
dup(open(filename,O_RDWR|O_APPEND|O_CREAT,0666));
}
void redirectstderr_stdout(){
close(2);
dup(1);
}
void redirectstdin(char filename[]){
close(0);
dup(open(filename,O_RDONLY));
}
/*void Pipe(char outcommand[], char incommand[]){
int fd[2];
pipe(fd);
close(1);
dup(fd[1]);
close(0);
dup(fd[0]);
close(fd[1]);
close(fd[0]);
system(outcommand);
system(incommand);
}*/
void Exit(){
exit(0);
}
void removemultiplespaces(char input[]){
//CHeck this out geeksforgeeks.org/remove-extra-spaces-string
int n = strlen(input);
char s[n];
s[0]='\0';
//remove the first spaces
int i = 0;
for(i;i<n && input[i]==' ';i++);
//remove multiple spaces
char prev = input[i];
int j = 0;
for(j;i<n;i++)
{
if(input[i]!=' ')
s[j++]=input[i];
else if (input[i]==' ' && prev!=' ')
s[j++]=input[i];
prev = input[i];
}
n = strlen(s);
//remove the ending spaces
int e = n-1;
for(e;e>=0 && s[e]==' ';e--);
s[e]='\0';
//printf("%s\n",s);
//printf("%d\n",n);
strcpy(input,s);
}
void parseInput(char input[],char*list[]){
int x=0;
int i=0;
char *rest=input;
char *token;
while(token=strtok_r(rest,"|12",&rest)){
//removemultiplespaces(token);
list[x]=token;
x++;
}
}
void runCommandsWrapper(char input[],int l,int n);
//function to execute the commands
int runCommands(char input[],int l,int n)
{
int status = -1;
signal(SIGINT,SIG_DFL);
char *var[n];
//j is for the arguments
int j = 0;
//k is counter for temp
int k = 0;
char temp[n];
//to check the lowerbound for multiple pipes
int i = 0;
//string parsing
for(i=l;i<n;i++)
{
if(input[i]=='|')
{
break;
}
if(input[i]=='<')
{
int flag = 0;
i++;
if(input[i]=='<')
{
flag=1;
i++;
}
i++;
char s[100];
int j=0;
for(i;i<n && input[i]!=' ';i++)
{
s[j] =input[i];
j++;
}
s[j] = '\0';
redirectstdin(s);
//check for the space problem
if(i==n)
break;
}
if(input[i]=='>')
{
//printf("is this entering thsi\n");
int fds = 1;
//printf("%s",input[i]);
if(input[i-1]!=' ')
{
fds = input[i-1];
fds = fds - 48;
if(input[i-2]=='&')
{
fds = 3;
}
}
int flag = 0;
i++;
if(input[i]=='>')
{
flag=1;
i++;
}
i++;
char s[100];
int j = 0;
for(i;i<n && input[i]!=' ';i++)
{
s[j] = input[i];
j++;
}
s[j] = '\0';
//printf("%d\n",fds);
//printf("just before redir\n");
if(fds==1)
{
redirectstdout(s,flag);
}
if(fds==2)
{
redirectstderr(s,flag);
}
if(fds==3)
{
redirectstdout(s,flag);
redirectstderr_stdout();
}
//printf("just after redir\n");
k=0;
if(i==n)
break;
}
if(input[i]!=' ')
{
temp[k] = input[i];
k++;
}
else if(input[i]==' ')
{
temp[k] = '\0';
var[j] = (char*)malloc(sizeof(char)*k);
strcpy(var[j],temp);
j++;
k=0;
}
//if we are processing the last character in input
if(i==(n-1))
{
temp[k] = '\0';
var[j] = (char*)malloc(sizeof(char)*k);
strcpy(var[j],temp);
j++;
k=0;
if(input[i]=='|')
break;
}
}
//end of string parsing and setting up var and redirections
var[j]=NULL;
printf("%d",j);
/*for(int p=0;p<j;p++)
{
printf("%s\n",var[p]);
}*/
/*printf("these are the values\n");
printf("%d\n",getpid());
printf("%d\n",n);
printf("%d\n",i);*/
//if we have no pipe
if(i==n)
{
//printf("if the command executed\n");
status = execvp(var[0],var);
}
//if have have a pipe and commands to execute
else if(i<n)
{
//printf("all hell broke loose\n");
//this means we have used a pipe
int pid;
int fd[2];
int ret = pipe(fd);
if(ret==-1)
return -1; //error in making pipe
pid = fork();
if(pid==0)
{
//this is the child process
//this process would give input to parent
//close the reading end
close(fd[0]);
//highjack the writing end
close(1);
dup(fd[1]);
status = execvp(var[0],var);
//command(var);
}
else if (pid>0)
{
//this is the parent process
//this will accept the input from the child
//close the writing end
close(fd[1]);
//highjack the reading end
close(0);
dup(fd[0]);
runCommands(input,i+2,n);
//its parent command has been executed
status = 0;
}
else
{
printf("unable to fork\n");
}
}
return status;
}
//function to fork
void runCommandsWrapper(char input[],int l,int n)
{
int pidt;
int status;
if((pidt=fork())==0){
status = runCommands(input,l,n);
if(status==-1){
write(STDOUT_FILENO,"Error in executing command \n",28);
}
exit(0);
}
else
wait(NULL);
//printf(" lkjsdflkslkfslkfsfkdlslkf ;sknflksdfmsklf %d\n",getpid());
}
int main(){
//to stop the sigint from closing the shell itself
signal(SIGINT,SIG_IGN);
while(1){
int t[2];//Saving initial values of fds which Im loading at end.
pipe(t);
dup2(0,t[0]);
dup2(1,t[1]);
int u[2];
pipe(u);
dup2(2,u[0]);
//command prompt
printf("XD ");
//input string
char input[512];
//scanf("%s",input);
//changing the delimiter to newline only
scanf ("%[^\n]%*c", input);
int n = strlen(input);
//printf("command lenght %d",n);
//shell functions
if(strncmp(input,"exit",4)==0)
exit(0);
if(strncmp(input,"cd",2)==0)
{
char s[100];
// printing current working directory
for(int i=3;i<n;i++)
{
s[i-3] = input[i];
}
//changing the directory
chdir(s);
}
//running executables after creating a new process
runCommandsWrapper(input,0,n);
//resetting the fds
dup2(t[0],0);
dup2(t[1],1);
dup2(u[0],2);
close(t[0]);
close(t[1]);
close(u[0]);
close(u[1]);
}
return 0;
}