-
Notifications
You must be signed in to change notification settings - Fork 0
/
q2_mod.c
374 lines (358 loc) · 9.84 KB
/
q2_mod.c
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
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>
void cd_command(char *input,char *input1){
char cwd[1000];
getcwd(cwd,sizeof cwd);
if(input==NULL || strcmp(input,"~")==0 || strcmp(input,"-P")==0){
char *home=getenv("HOME");
chdir(home);
if(errno==ENOENT){
printf("%s\n","The directory specified in path does not exist." );
}
else if(errno==EACCES){
printf("%s\n","Search permission is denied for one of the components of path." );
}
}
else if(strcmp(input,"-L")==0){
int x=chdir(input1);
if(x<0){
//printf("cd: %s: No such file or directory\n",input );
if(errno==ENOENT){
printf("%s\n","The directory specified in path does not exist." );
}
else if(errno==EACCES){
printf("%s\n","Search permission is denied for one of the components of path." );
}
}
}
else if(strcmp(input,"..")==0){
chdir("../");
if(errno==ENOENT){
printf("%s\n","The directory specified in path does not exist." );
}
else if(errno==EACCES){
printf("%s\n","Search permission is denied for one of the components of path." );
}
}
else{
int x=chdir(input);
if(x<0){
//printf("cd: %s: No such file or directory\n",input );
if(errno==ENOENT){
printf("%s\n","The directory specified in path does not exist." );
}
else if(errno==EACCES){
printf("%s\n","Search permission is denied for one of the components of path." );
}
}
}
getcwd(cwd,sizeof cwd);
printf("%s\n",cwd );
}
void pwd_command(char *input){
if(input==NULL || strcmp(input,"-L")==0){
char cwd[1000];
getcwd(cwd,sizeof cwd);
if(errno==ENOENT){
printf("%s\n","The current working directory has been unlinked." );
}
else if(errno==EACCES){
printf("%s\n","Permission to read or search a component of the filename was denied." );
}
printf("%s\n",cwd );
}
else if(strcmp(input,"-P")==0){
char *home=getenv("HOME");
printf("%s\n",home );
}
else{
if(strcmp(input,"--help")==0){
printf("%s\n","pwd: pwd [-LP]\nPrint the name of the current working directory.\n\n Options:\n-L print the value of $PWD if it names the current working\ndirectory\n-P print the physical directory, without any symbolic links\n\nBy default, `pwd' behaves as if `-L' were specified.\n\nExit Status:\nReturns 0 unless an invalid option is given or the current directory\ncannot be read.\n");
}//add more condition
}
}
void exit_command(){
exit(0);
}
void echo_command(char *input1, char *input2){
if(input1==NULL){
printf("\n");
}
else if(strcmp(input1,"-n")==0){
int i=0;
while(input2[i]!='\0'){
if(input2[i]=='\n'){
i++;
}
else{
printf("%c",input2[i]);//make some changes
i++;
}
}
//write(STDOUT_FILENO,input2,strlen(input2));
}
else if(strcmp(input1,"-E")==0){
int i=0;
while(i<strlen(input2) && input2[i]!='\0'){
if(input2[i]=='\a'){
i++;
}
else if(input2[i]=='\b'){
i++;
}
else if(input2[i]=='\e'){
i++;
}
else if(input2[i]=='\f'){
i++;
}
else if(input2[i]=='\n'){
i++;
}
else if(input2[i]=='\r'){
i++;
}
else if(input2[i]=='\t'){
i++;
}
else if(input2[i]=='\v'){
i++;
}
else if(input2[i]=='\\'){
i++;
}
else{
printf("%c",input2[i]);
//write(STDOUT_FILENO,input2[i],strlen(input2[i]));
i++;
}
}
}
else{
printf("%s\n",input1);
}
}
void addHist(char *input){
FILE *f=fopen("/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/history.txt","a");
if(f!=NULL){
fprintf(f, "%s\n",input);
}
fclose(f);
}
void history_command(char *input,char *input1){
if(input==NULL){
FILE *f=fopen("/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/history.txt","r");
if(f!=NULL){
char line[200];
int i=1;
while(fgets(line,sizeof line,f)!=NULL){
printf("%d. %s\n",i,line );
i++;
}
}
perror("/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/history.txt");//handles all errors based on all errnos generated by fopen
fclose(f);
}
else if(strcmp(input,"-c")==0){
FILE *f=fopen("/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/history.txt","w");
perror("/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/history.txt");
fclose(f);
}
else if(strcmp(input,"-s")==0){
FILE *f=fopen("/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/history.txt","a");
if(f!=NULL){
fprintf(f, "%s\n",input1);
}
perror("/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/history.txt");
fclose(f);
}
/*else if(strcmp(input,"-d")==0){
//delete input1 - 1,since one more entry is added
//printf("%s\n","hiiii" );
int x=atoi(input1);
//printf("%d\n", x);
FILE *fi=fopen("/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/history.txt","r");
int count=0;
if(fi!=NULL){
char line[200];
while(fgets(line,sizeof line,fi)!=NULL){
count++;
}
}
//printf("%d %s\n",count,"count" );
perror("/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/history.txt");
fclose(fi);
if(x>0 && x<=count){
}
else if(x<0){
//printf("%s\n","ajao" );
//int d=x-2;
int y=(count+x+1);//to make it work like actaul delete in bash;
x=y;
}
else if(x>count || x==0){
x=0;
}
//printf("%d %s\n",x,"hello" );
FILE *f=fopen("/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/history.txt","r");
FILE *f1=fopen("/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/history1.txt","w");
if(f!=NULL && f1!=NULL){
char line[200];
int i=1;
while(fgets(line,sizeof line,f)!=NULL){
//printf("%d. %s\n",i,line );
if(i==x){
//fprintf(f1, "%s\n",line);
}
else{
fprintf(f1, "%s",line);
}
i++;
}
}
fclose(f);
fclose(f1);
int l=remove("history.txt");
/*if(l==0){
printf("%s\n","thank" );
}*/
//int ret=rename("/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/history1.txt","history.txt");
/*if(ret==0){
printf("%s\n","yessss" );
}*/
//}*/
else{
int val=atoi(input);//also handles the case when a number is not passed since it atoi will return 0
FILE *fi=fopen("/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/history.txt","r");
int count=0;
if(fi!=NULL){
char line[200];
while(fgets(line,sizeof line,fi)!=NULL){
count++;
}
}
perror("/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/history.txt");
fclose(fi);
FILE *f=fopen("/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/history.txt","r");
if(f!=NULL){
char line[200];
int x=0;
while(fgets(line,sizeof line,f)!=NULL){
if(x>=(count-val)){
printf("%d. %s\n",x,line );
x++;
}
else{
x++;
}
}
}
perror("/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/history.txt");
fclose(f);
}
}
void main(){
char input[500];
printf("%s\n","Welcome to the Shell>>>>" );
//the %[^\n]%*c makes it read the input including scpaces until \n charachter,
//otherwise it would have read till space
//add command to history
while(1){
printf("%s",">>" );
scanf("%[^\n]%*c",input);
addHist(input);
char *sep_input[5];
int i=0;
sep_input[i]=strtok(input," ");//strtok returns a pointer to the location after " " in each successive call
while(sep_input[i]!=NULL){
i++;
sep_input[i]=strtok(NULL," ");
}
char abc[1]="\n";/*
if(sep_input[0]=="\n"){
printf("%s\n","oops" );
}*/
if(strcmp(sep_input[0],abc)==0){
printf("%s\n","oops" );
}
else if(strcmp(sep_input[0],"cd")==0){
cd_command(sep_input[1],sep_input[2]);
}
else if(strcmp(sep_input[0],"pwd")==0){
pwd_command(sep_input[1]);
}
else if(strcmp(sep_input[0],"echo")==0){
echo_command(sep_input[1],sep_input[2]);
}
else if(strcmp(sep_input[0],"history")==0){
history_command(sep_input[1],sep_input[2]);
}
else if(strcmp(sep_input[0],"exit")==0){
exit_command();
}
else{
pid_t pid;
pid=fork();
if(pid<0){
fprintf(stderr, "%s\n","Fork Failed" );
}
else if(pid==0){
//to open any file,first compile it with the name gcc filename.c -o filename.o
if(strcmp(sep_input[0],"date")==0 && sep_input[1]==NULL){
//execl()
char *args[]={"/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/date.o",sep_input[1],sep_input[2],NULL};
execv(args[0],args);
}
else if(strcmp(sep_input[0],"date")==0 && strcmp(sep_input[1],"-R")==0 ){
//execl()
char *args[]={"/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/date_R.o",sep_input[1],sep_input[2],NULL};
execv(args[0],args);
}
else if(strcmp(sep_input[0],"date")==0 && strcmp(sep_input[1],"-I")==0 ){
//execl()
char *args[]={"/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/date_I.o",sep_input[1],sep_input[2],NULL};
execv(args[0],args);
}
else if(strcmp(sep_input[0],"mkdir")==0){
//printf("%s\n", "success");
//execl("./mkdir.o",sep_input[1],NULL);
char *args[]={"/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/mkdir.o",sep_input[1],sep_input[2],NULL};
execv(args[0],args);
}
else if(strcmp(sep_input[0],"rm")==0){
char *args[]={"/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/rm.o",sep_input[1],sep_input[2],NULL};
execv(args[0],args);
}
else if(strcmp(sep_input[0],"cat")==0){
char *args[]={"/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/cat.o",sep_input[1],sep_input[2],NULL};
execv(args[0],args);
}
else if(strcmp(sep_input[0],"ls")==0 && sep_input[1]==NULL){
//printf("%s\n","okay" );
char *args[]={"/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/ls.o",sep_input[1],sep_input[2],NULL};
execv(args[0],args);
}
else if(strcmp(sep_input[0],"ls")==0 && strcmp(sep_input[1],"-a")==0){
//printf("%s\n","okay" );
char *args[]={"/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/ls_a.o",sep_input[1],sep_input[2],NULL};
execv(args[0],args);
}
else if(strcmp(sep_input[0],"ls")==0 && strcmp(sep_input[1],"-1")==0){
//printf("%s\n","okay" );
char *args[]={"/mnt/c/users/dell/dipankar/sem3/os/ass1/q2/ls_1.o",sep_input[1],sep_input[2],NULL};
execv(args[0],args);
}
exit(-1);
}
else{
waitpid(-1,NULL,0);
}
}
}
}