-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_redirection.c
More file actions
174 lines (174 loc) · 4.18 KB
/
io_redirection.c
File metadata and controls
174 lines (174 loc) · 4.18 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
#include "headers.h"
#include "io_redirection.h"
#include "main.h"
int check_file(char *path)
{
struct stat f;
if (stat(path, &f) == 0 && S_ISDIR(f.st_mode) == 0) //exists and not a directory
{
return 1;
}
else
{
return 0;
}
}
int create_if_not(char *str, int type)
{
int fd;
if (type == 2)
{
fd = open(str, O_WRONLY | O_CREAT | O_APPEND, 0644);
if (fd == -1)
{
return -1;
}
}
else
{
fd = open(str, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1)
{
return -1;
}
}
return fd;
}
int check_inpred(char *str)
{
char *ptr = (char *)malloc(1000 * sizeof(char));
ptr = strstr(str, "<");
int fl = 1;
if (ptr == NULL)
{
fl = 0;
}
return fl;
}
int check_outred(char *str)
{
char *ptr = (char *)malloc(1000 * sizeof(char));
ptr = strstr(str, ">");
int fl = 0;
if (ptr != NULL)
{
if (strstr(str, ">>") != NULL)
{
fl = 2; // 2 for append
}
else
{
fl = 1;
}
}
return fl;
}
void redirection(char *command)
{
int org_stdout = dup(1);
int org_stdin = dup(0);
int status, in, out;
char *pt = (char *)malloc(1000 * sizeof(char));
in = check_inpred(command);
out = check_outred(command);
char *copy_command = (char *)malloc(1000 * sizeof(char));
char *copy_command2 = (char *)malloc(1000 * sizeof(char));
char *copy_command3 = (char *)malloc(1000 * sizeof(char));
char *outputf = (char *)malloc(1000 * sizeof(char));
char *inputf = (char *)malloc(1000 * sizeof(char));
char *fin_outf = (char *)malloc(1000 * sizeof(char));
char *fin_inf = (char *)malloc(1000 * sizeof(char));
char *com = (char *)malloc(1000 * sizeof(char));
strcpy(copy_command, command);
strcpy(copy_command2, command);
strcpy(copy_command3, copy_command2);
pt = strtok(copy_command2, "><");
strcpy(com, pt);
if (in == 1)
{
pt = strtok(copy_command3, "<");
pt = strtok(NULL, ">");
strcpy(inputf, pt);
fin_inf = strtok(inputf, " \t");
if (fin_inf == NULL)
{
printf("\x1B[1;31mPlease specify the name of the file for input\n\x1B[1;0m");
prestat = 'f';
return;
}
else if (check_file(fin_inf) == 0)
{
printf("\x1B[1;31mInput file does not exist\n\x1B[1;0m");
prestat = 'f';
return;
}
}
char *send = (char *)malloc(1000 * sizeof(char));
strcpy(send, com);
if (out == 1 || out == 2)
{
pt = strtok(command, ">");
pt = strtok(NULL, "<");
strcpy(outputf, pt);
fin_outf = strtok(outputf, " \t");
if (fin_outf == NULL)
{
printf("\x1B[1;31mEnter output file\n\x1B[1;0m");
prestat = 'f';
return;
}
}
pid_t pid = fork();
if (pid == -1)
{
perror("\x1B[1;31mError \x1B[1;0m");
prestat = 'f';
return;
}
else if (pid == 0)
{
if (in == 1)
{
int fdin = open(fin_inf, O_RDONLY);
if (fdin == -1)
{
printf("\x1B[1;31mError reading the input file\n\x1B[1;0m");
prestat = 'f';
return;
}
else if (fdin >= 0)
{
dup2(fdin, 0);
close(fdin);
}
}
if (out == 1 || out == 2)
{
int fdout;
fdout = create_if_not(fin_outf, out);
if (fdout == -1)
{
printf("\x1B[1;31mError writing to the output file\n\x1B[1;0m");
prestat = 'f';
return;
}
else if (fdout >= 0)
{
dup2(fdout, 1);
close(fdout);
}
}
int y = 0;
strcpy(colonsep[y], send);
call_command(1, 1);
dup2(org_stdin, 0);
close(org_stdin);
dup2(org_stdout, 1);
close(org_stdout);
exit(0);
}
else if (pid >= 1)
{
waitpid(pid, &status, 0);
}
}