-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
328 lines (324 loc) · 6.56 KB
/
Copy pathshell.c
File metadata and controls
328 lines (324 loc) · 6.56 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
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<pwd.h>
#include<signal.h>
#include<wait.h>
#include<fcntl.h>
#include<signal.h>
#include<setjmp.h>
#include "global.h"
#include "execute.h"
#include "parse.h"
#include "pwd.h"
#include "cd.h"
#include "echo.h"
#include "pinfo.h"
#include "runpipe.h"
sigjmp_buf mark;
void controlc()
{
loop_flag=1;
glob=0;
num=0;
reset=0;
cntrlc=1;
printf("\n");
return;
}
void controlz()
{
if(glob==0)
{
kill(pro,SIGSTOP);
process *new=(process *)malloc(sizeof(process));
new->status=1;
new->pid=pro;
new->next=NULL;
int i=0;
while(argv[i]!=NULL)
{
strcpy(new->argv[i],argv[i]);
i++;
}
if(head==NULL)
head=new;
else
{
process *t=head;
while((t->next)!=NULL)
t=t->next;
t->next=new;
}
printf("\n");
siglongjmp(mark,-1);
}
}
int main()
{
glob=0; //global initializations
num=0;
par=1;
head=NULL;
cntrlc=0;
loop_flag=0;
struct passwd *pw; //structure in which all values are present
int uid,c;
uid=getuid();
pw=getpwuid(uid);
char host[1024]; //This array has the host name
gethostname(host,1024); //returns name in array
int len;
char cwd[1024]; //This array contains the cwd string
len=0; //reference length for home directory in which program is executed
if(getcwd(cwd,sizeof(cwd))!=NULL) //returns home dir name in array cwd
len=strlen(cwd); //Finding lenth of home dir to compare as ~
sigset(SIGINT,controlc);
//Main shell loop starts from here
while(1)
{
signal(SIGTSTP,&controlz);
fg_call=0;
int reset=0;
char cwd1[1024],cwd3[1024]; //cwd1 for replacing in it with ~
int i,swit=0; //To check if pwd has home directory as subpath
if(getcwd(cwd1,sizeof(cwd1))!=NULL) //checking string for home path
{
for(i=0;i<len;i++)
{
if(cwd[i]!=cwd1[i])
{
swit=1;
break;
}
}
}
if(swit==0) //changing accordingly if subpart is home path
{
cwd1[0]='~';
i=len;
while(cwd1[i]!='\0')
{
cwd1[i-len+1]=cwd1[i];
i++;
}
cwd1[i-len+1]='\0';
}
int returncode=sigsetjmp(mark,1);
printf("<%s@%s:%s>",pw->pw_name,host,cwd1); //printing prompt
char comm[1024]; //stores command entered
char *token; //This contains the divided commands seperated by ;
int inp_flag; //This flag is for determining whether input is taken or not
int pi; //used to check whether pipelining is present or not
char *line=fgets(comm,1024,stdin);
if((line==NULL)&&(cntrlc==0))
{
printf("\n");
break;
}
if(line)
inp_flag=1;
else
inp_flag=0;
token=strtok(comm,";");
int end=0; //To break out of loops
int j;
while(token !=NULL)
{
if((num==1)&&(glob==1))
{
num=0;
glob=0;
reset=1;
}
if(cntrlc==1)
{
num=0;
glob=0;
reset=0;
loop_flag=0;
cntrlc=0;
}
if(num==0)
{
glob=0;
for(i=0;i<20;i++)
argv[i]=NULL;
}
for(i=0;i<50;i++)
for(j=0;j<50;j++)
cmnd[i][j]=NULL;
if((num==0)&&((reset==0)||(comm[0]!='\0')))
{
int test=0;
while(token[test]!='\0')
test++;
if(token[test-2]=='|')
{
fprintf(stderr,"ERROR: Give proper command after pipe\n");
break;
}
pi=0;
for(char *p=strtok(token,"|");p;p=strtok(NULL,"|"))
{
parse(p,argv);
int j=0;
while(argv[j]!=NULL)
{
cmnd[pi][j]=argv[j];
j++;
}
pi++;
}
}
if(pi>1) //for piping
runpipe();
else if(argv[0]!=NULL) //if piping is not there
{
if(loop_flag==1)
loop_flag=0;
else
{
if((strcmp(argv[0],"quit")==0)||(strcmp(argv[0],"exit")==0)) //exit
{
end=1;
break;
}
if(strcmp(argv[0],"cd")==0) //for cd command
cd(cwd);
else if(strcmp(argv[0],"pwd")==0) //for pwd command
pwd();
else if(strcmp(argv[0],"echo")==0) //for echo command
echo();
else if(strcmp(argv[0],"pinfo")==0) //For pinfo command
{
pinfo();
break;
}
else if(strcmp(argv[0],"jobs")==0)
{
process *temp=head;
int count=1;
while(temp!=NULL)
{
if(temp->status==1)
{
printf("[%d] ",count);
int arg_count=0;
while(temp->argv[arg_count][0]!='\0')
{
printf("%s ",temp->argv[arg_count]);
arg_count++;
}
printf("[%d]\n",temp->pid);
count++;
}
temp=temp->next;
}
}
else if(strcmp(argv[0],"killallbg")==0)
{
process *temp1=head;
while(temp1!=NULL)
{
temp1->status=0;
kill(temp1->pid,9);
temp1=temp1->next;
}
}
else if((strcmp(argv[0],"sendsig")==0)&&(inp_flag==1))
{
process *temp2=head;
int l=strlen(argv[1])-1;
int ans=0;
int kk=0;
while(kk<=l)
{
ans+=((ans*10)+argv[1][kk]-'0');
kk++;
}
int move=ans; // move is the position of bg process
int kl=0;
while(temp2!=NULL)
{
if(temp2->status==1)
kl++;
if(kl==move)
break;
temp2=temp2->next;
}
if(kl<move)
fprintf(stderr,"ERROR: That many jobs don't exist\n");
else
{
int l1=strlen(argv[2])-1;
int ans1=0;
int kk1=0;
while(kk1<=l1)
{
ans1+=((ans1*10)+argv[2][kk1]-'0');
kk1++;
}
if(ans1==9)
temp2->status=0;
kill(temp2->pid,ans1);
}
}
else if((strcmp(argv[0],"fg")==0)&&(inp_flag==1))
{
fg_call=1;
int pik=0; // start of conversion of char to num
int ans2=0;
int len2=strlen(argv[1]);
while(pik<len2)
{
ans2+=((ans2*10)+argv[1][pik]-'0');
pik++;
} //ends here
int cc=0; //our rapid count
process *temp3=head;
while(temp3!=NULL)
{
if(temp3->status==1)
cc++;
if(cc==ans2)
break;
temp3=temp3->next;
}
if(cc<ans2)
fprintf(stderr,"ERROR: That many background process don't exist\n");
else
{
int st=0;
waitpid(temp3->pid,&st,0);
temp3->status=0;
break;
}
}
else //Implementing Background process and other executable programs
{
int i=1;
while(argv[i]!=NULL)
i++;
if(strcmp(argv[i-1],"&")==0)
{
glob=1; //Force it into Background Loop
argv[i-1]=NULL;
}
if((num==0)&&(argv[0]!=NULL)&&(inp_flag==1))
{
execute(argv);
reset=0;
}
}
}
}
token=strtok(NULL,";"); //Move to the next subcommand in delimiter
}
if(end==1)
break;
}
return 0;
}