-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirection.c
216 lines (180 loc) · 5 KB
/
redirection.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
#include "headers.h"
#include "execute.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
char fullcommand[1000];
char inputfile[1000];
char outputfile[1000];
char firstcommand[1000];
void redirection(char *command)
{
int input_redirec_flag = 0;
int output_redirec_flag = 0;
for(int i=0;i<999;i++)
{
inputfile[i] = '\0';
outputfile[i] = '\0';
firstcommand[i] = '\0';
}
// Tokenizing full Command to get Flags
strcpy(fullcommand, command);
char *withincommands[1000];
withincommands[0] = strtok(fullcommand, "\r\t ");
int numwithincom = 0;
while (withincommands[numwithincom] != NULL)
{
if(strcmp(withincommands[numwithincom], "<") == 0)
{
input_redirec_flag = 1;
}
if(strcmp(withincommands[numwithincom], ">") == 0)
{
output_redirec_flag = 1;
}
if(strcmp(withincommands[numwithincom], ">>") == 0)
{
output_redirec_flag = 2;
}
numwithincom++;
withincommands[numwithincom] = strtok(NULL, "\r\t ");
}
// Getting Output File
if(output_redirec_flag != 0)
{
int oflag=0;
for(int i = 0; i < numwithincom; i++)
{
if(oflag==1)
{
strcpy(outputfile, withincommands[i]);
}
if(strcmp(withincommands[i], ">") == 0 || strcmp(withincommands[i], ">>") == 0)
{
oflag=1;
}
}
// Getting Input File
if(input_redirec_flag != 0)
{
int iflag=0;
int oflag=0;
for(int i = 0; i < numwithincom; i++)
{
if(strcmp(withincommands[i], ">") == 0 || strcmp(withincommands[i], ">>") == 0)
{
oflag=1;
}
if(iflag==1 && oflag==0)
{
strcpy(inputfile, withincommands[i]);
}
if(strcmp(withincommands[i], "<") == 0)
{
iflag=1;
}
}
}
}
else
{
// Getting Input File
if(input_redirec_flag != 0)
{
int iflag=0;
for(int i = 0; i < numwithincom; i++)
{
if(iflag==1)
{
strcpy(inputfile, withincommands[i]);
}
if(strcmp(withincommands[i], "<") == 0)
{
iflag=1;
}
}
}
}
// Getting command in new array args
int oflag=0;
int iflag=0;
for(int i = 0; i < numwithincom; i++)
{
if(strcmp(withincommands[i], "<") == 0)
{
iflag = 1;
}
if(strcmp(withincommands[i], ">") == 0 || strcmp(withincommands[i], ">>") == 0)
{
oflag = 1;
}
if(oflag == 0 && iflag == 0)
{
strcat(firstcommand, withincommands[i]);
strcat(firstcommand, " ");
}
}
// Saving stdin and stdout
int stdin_pointer = dup(STDIN_FILENO);
int stdout_pointer = dup(STDOUT_FILENO);
pid_t pid = fork();
// Fork failure
if(pid < 0)
{
perror("Forking Failed");
return;
}
// Child Process shall execute
else if(pid == 0)
{
// If input file present dup2 stdin to this file
if(input_redirec_flag == 1)
{
int input_fd = open(inputfile, O_RDONLY);
if(input_fd < 0)
{
printf("Input File does not exist\n");
return;
}
dup2(input_fd, 0);
close(input_fd);
}
// If output files are present dup2 stdout to this file
if(output_redirec_flag == 1)
{
int output_fd = open(outputfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if(output_fd < 0)
{
printf("Output File does not exist\n");
return;
}
dup2(output_fd, 1);
close(output_fd);
}
// If output files are present dup2 stdout to this file
if(output_redirec_flag == 2)
{
int output_fd = open(outputfile, O_WRONLY | O_CREAT | O_APPEND, 0644);
if(output_fd < 0)
{
printf("Output File does not exist\n");
return;
}
dup2(output_fd, 1);
close(output_fd);
}
// Execute the command
execute(firstcommand);
// Return original pointers to stdin and stdout
dup2(stdout_pointer, 1);
dup2(stdin_pointer, 0);
close(stdout_pointer);
close(stdin_pointer);
}
// Parent process waits for child to end
else
{
int check_child;
while (wait(&check_child) != pid);
}
}