-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw_redirection.c
220 lines (182 loc) · 4.14 KB
/
w_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
217
218
219
220
#include "w_redirection.h"
#include "w_run.h"
/**
* This code is very ugly. Please look at w_pipe.c or any other files
* instead of this one. Thank you.
*/
int
simpleRedirect(FILE* ofp, char** tokens) {
char* cmd = tokens[0];
char* inputFN = NULL;
int inInd = 0;
char* outputFN = NULL;
int outInd = 0;
char* tmp = NULL;
pid_t cpid = 12345;
int Ntokens = 0;
// find the input and output files
for (int i = 0; tokens[i]; i++) {
if (!strcmp(tokens[i], ">")) {
outputFN = tokens[i+1];
outInd = i;
} else if (!strcmp(tokens[i], "<")) {
inputFN = tokens[i+1];
inInd = i;
}
Ntokens++;
}
// if there is no file after a ">" specify here
if (!inputFN && !outputFN) {
perror("Error with redirection files");
return -1;
}
if (inputFN && outputFN) {
// redirectTree only takes in multiple redirection in this order < >
// so I'm going to swap the input and output files if they're in > <
if (outputFN < inputFN) {
tokens[outInd+1] = inputFN;
tokens[inInd+1] = outputFN;
tokens[outInd][0] = '<';
tokens[inInd][0] = '>';
}
redirectTree(ofp, tokens);
} else if (inputFN || outputFN) {
redirectTree(ofp, tokens);
}
return 0;
}
// returns 0 if there are no redireciton characters
int
redirectTree(FILE* ofp, char** tokens)
{
char* arrow = NULL;
char* arrow2 = NULL;
int fd[2];
int i = 0;
int nullLoc = 0;
int nullLoc2 = 0;
int arrowCount = 0;
pid_t cpid;
int status = 0;
// iterate through looking for strings
for (i = 0; tokens[i] != NULL; i++) {
if (!strcmp(tokens[i], ">") || !strcmp(tokens[i], "<")) {
if (arrowCount == 0) {
arrow = tokens[i];
tokens[i] = NULL;
nullLoc = i;
} else if (!strcmp(tokens[i], ">")) {
arrow2 = tokens[i];
tokens[i] = NULL;
nullLoc2 = i;
}
arrowCount++;
}
}
if (arrow == NULL) {
return 0;
}
// perform redirection between a process and a file
if (arrowCount == 1) {
redirectChilds(ofp, tokens, arrow, nullLoc);
}
// put output into another file
else {
pipe(fd);
cpid = fork();
if (cpid == 0) {
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
redirectChilds(ofp, tokens, arrow, nullLoc);
_exit(0);
}
cpid = fork();
if (cpid == 0) {
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
redirection(ofp, tokens[nullLoc2+1], ">");
_exit(0);
}
close(fd[0]);
close(fd[1]);
waitpid(-1, &status, 0);
waitpid(-1, &status, 0);
}
return 1;
}
int
redirectChilds(FILE* ofp, char** tokens, char* arrow, int nullLoc) {
int fd[2];
int status = 0;
pid_t cpid;
int exitStatus = 0;
int statLoc = 0;
// create the pipe
pipe(fd);
cpid = fork();
if (cpid == 0 ) {
// first child - the child that makes output
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
if (arrow[0] == '>') {
exitStatus = runCmd(ofp, tokens, &statLoc);
}
else {
exitStatus = redirection(ofp, tokens[nullLoc+1], "<");
}
_exit(exitStatus);
}
cpid = fork();
if (cpid == 0) {
// second child - the child that reads output as input
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
if (arrow[0] == '>') {
exitStatus = redirection(ofp, tokens[nullLoc+1], ">");
}
else {
exitStatus = runCmd(ofp, &(tokens[0]), &statLoc);
}
_exit(exitStatus);
}
// redirection(ofp, "zoey.txt", ">");
close(fd[0]);
close(fd[1]);
waitpid(-1, &status, 0);
waitpid(-1, &status, 0);
return 1;
}
int
redirection(FILE* ofp, char* filename, char* arrow)
{
if (!strcmp(arrow, "<")) {
// piping from a file
FILE* ifp = fopen(filename, "r");
fileRedirect(ifp, ofp);
fclose(ifp);
}
else {
// piping to a file
FILE* ofp = fopen(filename, "w");
fileRedirect(stdin, ofp);
fclose(ofp);
}
return 0;
}
int
fileRedirect(FILE* ifp, FILE* ofp)
{
size_t len;
char* line;
// initialize getline variables
len = 0;
line = NULL;
if (ifp == NULL) {
return -1;
}
while (getline(&line, &len, ifp) != -1) {
fprintf(ofp, "%s", line);
}
free(line);
return 0;
}