-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirection.h
More file actions
92 lines (89 loc) · 1.9 KB
/
redirection.h
File metadata and controls
92 lines (89 loc) · 1.9 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
void redirection()
{
char s_temp[10000],final_s[10000];
strcpy(s_temp,s);
char* endstr;
char* tok = strtok_r(s,"<>",&endstr);
strcpy(final_s,tok);
// int original_in,original_out;
// original_in = dup(0);
// original_out = dup(1);
if(strstr(s_temp,"<")!=NULL && strstr(s_temp,">")!=NULL)
{
int flg=0;
if(strstr(s_temp,"<")<strstr(s_temp,">")) flg=1;
int in, out;
tok = strtok_r(NULL,"<>",&endstr);
trim_r(tok);
if(flg==1)
{
in = open(tok, O_RDONLY);
}
else
{
if(strstr(s_temp,">>"))
out = open(tok, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
else
out = open(tok, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
}
tok = strtok_r(NULL,"<>",&endstr);
trim_r(tok);
if(flg==0)
{
in = open(tok, O_RDONLY);
}
else
{
if(strstr(s_temp,">>"))
out = open(tok, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
else
out = open(tok, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
}
if(in<0 || out<0)
{
perror("Error '<>'");
return;
}
dup2(in, 0);
dup2(out, 1);
}
else if(strstr(s_temp,"<")!=NULL)
{
int in;
tok = strtok_r(NULL,"<>",&endstr);
trim_r(tok);
in = open(tok, O_RDONLY);
if(in<0)
{
perror("Error '<'");
return;
}
dup2(in, 0);
}
else if(strstr(s_temp,">")!=NULL)
{
int out;
tok = strtok_r(NULL,"<>",&endstr);
trim_r(tok);
if(strstr(s_temp,">>"))
{
out = open(tok, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
}
else
out = open(tok, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
if(out<0)
{
perror("Error '>'");
return;
}
dup2(out, 1);
}
strcpy(s,final_s);
strcpy(s_save,final_s);
functions();
// dup2(original_in,0);
// dup2(original_out,1);
// dup2(0,original_in);
// dup2(1,original_out);
// printf("**%s -- %s\n", final_s,s);
}