-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirect.c
106 lines (105 loc) · 1.58 KB
/
redirect.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
#include "redirect.h"
void redirect()
{
int saved_stdout;
int saved_stdin;
saved_stdout = dup(1);
saved_stdin=dup(0);
int lt=-1;
int gt=-1;
int a=0;
int rndm=i;
int error=0;
char* forexe[101]={NULL};
int fx=0;
while(arg[rndm][0]!=';')
{
if(arg[rndm][0]=='<')
{
lt=rndm;
FILE* f1=fopen(arg[rndm+1],"r");
if(!f1)error=1;
else {
fclose(f1);
}
rndm++;
}
else if(arg[rndm][0]=='>')
{
gt=rndm;
if(arg[rndm][1]=='>')
{
a=1;
FILE* f1=fopen(arg[rndm+1],"a");
if(!f1)error=1;
else fclose(f1);
rndm++;
}
else
{
a=0;
FILE* f1=fopen(arg[rndm+1],"w");
if(!f1)error=1;
else fclose(f1);
rndm++;
}
}
else if(arg[rndm][0]!='&')
forexe[fx++]=arg[rndm];
rndm++;
}
if(error==1)
{
printf(RED "No such file exsists" RESET "\n");
return;
}
int in,out,status;
if(lt>=0)
{
in = open(arg[lt+1],O_RDONLY);
dup2(in,0);
char ar[1000];
scanf("%s",ar);
int x,z,asd=0;
char temp[10000];
for(z=0;z<strlen(ar);z++)
{
if(ar[z]=='~')
{
for(x=0;x<strlen(pr_dir);x++)
temp[asd++]=pr_dir[x];
}
else
temp[asd++]=ar[z];
}
forexe[fx++]=temp;
close(in);
}
if(gt>=0)
{
out = open(arg[gt+1],O_WRONLY | O_APPEND);
dup2(out,1);
close(out);
}
pid_t pid;
if((pid=fork())==0)
{
if(execvp(forexe[0],forexe)==-1)
{
printf(RED "Not a valid command" RESET "\n");
kill(getpid(),SIGKILL);
}
close(0);
close(1);
}
else
{
while(wait(&status)!=pid);
dup2(saved_stdout, 1);
close(saved_stdout);
dup2(saved_stdin, 0);
close(saved_stdin);
while(arg[i][0]!=';')
i++;
}
}