-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.cpp
More file actions
527 lines (394 loc) · 10.4 KB
/
backup.cpp
File metadata and controls
527 lines (394 loc) · 10.4 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
// JEEVESH KATARIA 2019201058
// History command implemented: syntax:: history (without any arguments)s // checked
//IMPLEMENT N pipelines
// REDIRECTIOn IMPLEMENTED > >>
//support for alias command
//bashrc file created
//support for INITIALIZING variables HOME,PATH,USER,HOSTNAME,PS1
//ASSOSIATION OF ~ WITH HOME VARIABLE
//LOOK OF PROMPT VIA PS1 TO BE HANDLED
//HANDLE SUPPORT FOR $$
#include<iostream>
#include"cppheader.h"
#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
#include<string>
#include<cstring>
#include<list>
#include<fcntl.h>
#include<unordered_map>
unordered_map<string,string> um;
//char *mapbuffer[20];
//char mapbuffer2[255];
char alb[10];
char mapar[255];
char albuff[255];
list<string> history;
void history_call()
{
list<string>:: iterator it;
for(it=history.end();it!=history.begin();it--)
cout<<*it<<endl;
cout<<*it<<endl;
}
// REDIRECTION STARTED
//Redirection SUCCESS > 8/9/19
void redirection(char **argv)
{
int r;
char *pi_jai[2][5];
int j=0;
r=0;
//int i=0;
int k=0;
while(argv[r]!=NULL)
{
if(strcmp(argv[r],">")==0)
{
pi_jai[j][k]=NULL;
++j;k=0;++r;
continue;
}
pi_jai[j][k]=argv[r];
++r;++k;
}
pi_jai[j][k]=NULL;
int pid;
if((pid=fork())==0)
{
int fd=open(pi_jai[1][0],O_CREAT | O_WRONLY,0755);
dup2(fd,STDOUT_FILENO);
// close(fd);
if(execvp(*pi_jai[0],pi_jai[0]) < 0)
{
printf("***Error: REDIRECTION NOT SUCCESS:Command Not found\n");
exit(1);
}
close(fd);
}
else
{
wait(NULL);
}
}
// REDIRECTION ENDED
//DOUBLE REDIRECTION STArTED
// REDIRECTION STARTED
//Redirection SUCCESS > 8/9/19
void doubleredirection(char **argv)
{
int r;
//for( r=0;argv[r]!=NULL;r++)
//printf("%s\n",argv[r]);
char *pi_jai[2][5];
int j=0;
r=0;
//int i=0;
int k=0;
while(argv[r]!=NULL)
{
if(strcmp(argv[r],">>")==0)
{
pi_jai[j][k]=NULL;
++j;k=0;++r;
continue;
}
pi_jai[j][k]=argv[r];
// printf("pi=%s ar=%s \n",pi_jai[j][k],argv[r]);
++r;++k;
}
pi_jai[j][k]=NULL;
// printf(" %s \n",pi_jai[1][0]);
int pid;
if((pid=fork())==0)
{
int fd=open(pi_jai[1][0],O_APPEND|O_CREAT | O_WRONLY,755);
dup2(fd,STDOUT_FILENO);
// close(fd);
if(execvp(*pi_jai[0],pi_jai[0]) < 0)
{
printf("***Error: DOUBLEREDIRECTION NOT SUCCESS:Command Not found\n");
exit(1);
}
close(fd);
}
else
{
wait(NULL);
}
}
// DOUBLE REDIRECTION ENDED
// PIPE STARTED
void parsing_pipe(char **argv,int count_pipes)
{
char *pi_jai[5][5];
int j=0;
int i=0;
int k=0;
while(argv[i]!=NULL)
{
if(strcmp(argv[i],"|")==0)
{
pi_jai[j][k]=NULL;
++j;k=0;++i;
continue;
}
pi_jai[j][k]=argv[i];
// printf("%s\n",pi_jai[j][k]);
++i;++k;
}
pi_jai[j][k]=NULL;
int fd[count_pipes][2];
for(k=0;k<count_pipes;k++)
{
if (pipe(fd[k]) < 0)
exit(1);
}
for(k=0;k<=count_pipes;k++)
{
if(k==0)
{
int pid;
if(( pid=fork())==0)
{
dup2(fd[0][1],1);
close(fd[0][1]);
close(fd[0][0]);
cout<<" loop 1"<<" ";
if(execvp(*pi_jai[k],pi_jai[k]) < 0)
{
printf("***Error: Command Not found\n");
exit(1);
}
cout<<" loop 1"<<" ";
close(fd[0][0]);
}
else
{
wait(NULL);
close(fd[0][1]);
//close(fd[0][0]);
// exit(1);
}
}
else if(k==count_pipes)
{
int pid;
if((pid=fork())==0)
{
dup2(fd[k-1][0],0);
close(fd[k-1][0]);//closed now
close(fd[k-1][1]);
cout<<" loop 2"<<" ";
if(execvp(*pi_jai[k],pi_jai[k])<0)
{
printf("***Error: Command Not found\n");
exit(1);
}
exit(1);
}
else
{
wait(NULL);
//close(fd[k-1][0]);
close(fd[k-1][1]);//closedow
//exit(1);
}
}
else
{
int pid;
if((pid=fork())==0)
{
dup2(fd[k][1],1);
dup2(fd[k-1][0],0);
close(fd[k-1][0]);
close(fd[k][1]);
cout<<" loop 3"<<" ";
if(execvp(*pi_jai[k],pi_jai[k]) < 0)
{
printf("***Error: Command Not found\n");
exit(1);
}
// cout<<" loop 3"<<" ";
}
else
{
wait(NULL);
//close(fd[k-1][1]);//close(fd[k-1][0]);
close(fd[k][1]);
//exit(1);
}
}
}
}
int main()
{
jairc();
command_set();
char mapar[255];
char alb[10];
char albuff[255];
char mapbuffer2[255];
//cout<<" PATH "<<getenv("PATH");
history.clear();
um.clear();
char buff[1000];
char *argv[64];
char *mapbuffer[20];
int h=0;
signal(SIGINT, sigintHandler);
while(1)
{
promptcall();
string str;
getline(cin,str);
if(str[0]=='$' && str[1]=='$') {cout<<getppid(); continue;}
if(str[0]=='$' && str[1]=='?') {cout<<ret; continue;}
history.push_back(str);// maintaining list for history
int l=str.size();
int i;
for(i=0;i<l;i++)
buff[i]=str[i];
buff[i]='\0';
char al[6]="alias";
int x=strncmp(buff,al,5);
if(x==0)
{
// char albuff[255];
int j=0;
for(int i=6;buff[i]!='\0';i++)
{
albuff[j]=buff[i];
++j;
}
albuff[j]='\0';
int i;
for( i=0;albuff[i]!='=';i++)
{ alb[i]=albuff[i];}
alb[i]='\0';
string str1(alb);//first value
i=i+1;
int k=0;
while(albuff[i]!='\0')
{
mapar[k]=albuff[i];
++i;
++k;
}
mapar[k]='\0';
string mapcommand(mapar);//second value
//cout<<mapcommand;
um[str1]=mapcommand;
continue;
}
//alias
jai(buff, argv);
//first check whether value exist in map or not
string test(argv[0]);
int mapflag=0;
unordered_map<string,string> ::iterator itr;
for(itr=um.begin();itr!=um.end();++itr)
{
//cout<<"iterate";
string ss1=itr->first;
//cout<<" itrrrrrrrr"<<sizeof(itr->first)<<" "<<itr->first[1]<<"h "<<test[1];
if((test.compare(itr->first))==0)
{
mapflag=1;
//cout<<"String matched , keep it up";
//cout<<"second argument is:"<<itr->second<<" ";
string strmap=itr->second;
//cout<<"variable saved is : "<<strmap;
int j=strmap.size();
for(int i=0;strmap[i]!='\0';i++)
mapbuffer2[i]=strmap[i];
mapbuffer2[i]='\0';
jai(mapbuffer2,mapbuffer);
// for(int i=0;mapbuffer[i]!=NULL;i++)
//printf("%s\n",mapbuffer[i]);
//bccout<<"\n";
jaiexecute(mapbuffer);
break;
}
}
if(mapflag==1) continue;
char s2[6]="echo";
int echo_flag=0;
//pr();
if((strcmp(argv[0],s2))==0)
{
//pr();
//printf("INSERTED in ECHO");
argv[1];
//printf("%s",argv[1]);
char temp1[6]="$USER";//printf("%s",temp1);
char temp2[20]="$HOSTNAME";
char temp3[10]="$HOME";
char temp4[10]="$PATH";
char temp5[10]="$PS1";
if((strcmp(argv[1],temp1))==0)
{printf("%s \n",USER); echo_flag=1;}
else if ((strcmp(argv[1],temp2))==0)
{printf("%s \n",HOSTNAME); echo_flag=1;}
else if((strcmp(argv[1],temp3))==0)
{printf("%s \n",HOME); echo_flag=1;}
else if((strcmp(argv[1],temp4))==0)
{printf("%s \n",PATH); echo_flag=1;}
else if((strcmp(argv[1],temp5))==0)
{printf("%s \n",PS1); echo_flag=1;}
}
if(echo_flag) continue;
// N PIPE IMPLEMENTED
int count_pipes=0;
for(int j=0;argv[j]!=NULL;j++)
{
if(*argv[j]=='|') ++count_pipes;
}
if(count_pipes)
{
//cout<<"pipe"<<count_pipes;
parsing_pipe(argv,count_pipes);
continue;
}
//IMPLEMENTING REDIRECTION
int flag_redirection=0;
for(int j=0;argv[j]!=NULL;j++)
{
string retest=argv[j];
if(retest[0]=='>' && retest[1]=='>')
{
cout<<"\n DOUBLEREDIRECTION FOUND \n";
doubleredirection(argv);
break;
}
else if(*argv[j]=='>'){
cout<<"\n REDIRECTION FOUND \n";
flag_redirection=1;
break;
}
}
if(flag_redirection==1)
{
redirection(argv);
continue;
}
//implementing history command
if(strcmp(argv[0], "history")==0)
{
history_call();
continue;
}
if(strcmp(argv[0],"cd")==0)
{
directory_ch(argv[1]);
continue;
}
if(strcmp(argv[0], "exit") == 0)
{ exit(0); }
jaiexecute(argv);
// free(temp);
}
return 0;
}