-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirect.c
131 lines (119 loc) · 3.48 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
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
#include "header.h"
int getIOFiles(char* command, bool islast, int newfd) {
char* store = (char *) malloc(sizeof(char)*strlen(command));
strcpy(store, command);
char* tokens[MAXLEN];
char* delims = " \n\t";
char* save_pointer;
char* args = strtok_r(removeSpace(command), delims, &save_pointer);
bool bg = false;
int inpointer = -1;
int outpointer = -1;
bool isAppend = false;
char* arguments[MAXLEN];
long argcount = 0;
long index = 0;
while(args != NULL) {
tokens[index] = args;
args = strtok_r(NULL, delims, &save_pointer);
index++;
}
tokens[index] = NULL;
index = 0;
while(tokens[index] != NULL) {
if (strcmp(tokens[index], "<") == 0) {
inpointer = index + 1;
index++;
} else if (strcmp(tokens[index], ">") == 0) {
outpointer = index + 1;
isAppend = false;
index++;
} else if (strcmp(tokens[index], ">>") == 0) {
outpointer = index + 1;
isAppend = true;
index++;
} else if (strcmp(tokens[index], "&") == 0) {
bg = true;
} else {
arguments[argcount] = tokens[index];
argcount++;
}
index++;
}
arguments[argcount] = NULL;
int fd[2];
pipe(fd);
int F_STDOUT = (islast)? 1: dup(fd[1]);
int F_STDIN = newfd;
int o_fd = 1;
int i_fd = 0;
int STDIN = -1;
int STDOUT = -1;
if (outpointer != -1) {
if (!isAppend) {
o_fd = open(tokens[outpointer], O_WRONLY | O_TRUNC | O_CREAT, 0644);
if (o_fd < 0) {
fprintf(stderr, "Error Number: %d\n", errno);
perror("Output File Append: ");
}
} else {
o_fd = open(tokens[outpointer], O_WRONLY | O_APPEND, 0644);
if (o_fd < 0) {
fprintf(stderr, "Error Number: %d\n", errno);
perror("Output File: ");
}
}
STDOUT = dup(STDOUT_FILENO);
int response = dup2(o_fd, 1);
if (response < 0) {
fprintf(stderr, "Error Number: %d\n", errno);
perror("Change input: ");
}
close(o_fd);
}
if (inpointer != -1) {
i_fd = open(tokens[inpointer], O_RDONLY);
if (i_fd < 0) {
fprintf(stderr, "Error Number: %d\n", errno);
perror("Input File: ");
}
// F_STDIN = dup(i_fd);
STDIN = dup(STDIN_FILENO);
int response = dup2(i_fd, 0);
if (response < 0) {
fprintf(stderr, "Error Number: %d\n", errno);
perror("Change input: ");
}
close(i_fd);
}
int save = dup(fd[0]);
runCommand(arguments, fd, &F_STDIN, &F_STDOUT, bg);
if (F_STDOUT != -1) {
int response = dup2(1, F_STDOUT);
if(response < 0) {
printf("Error Number: %d\n", errno);
perror("dup2 stdout restore: ");
}
// close(F_STDIN);
}
fflush(stdin);
if(STDOUT != -1) {
int response = dup2(STDOUT, 1);
if(response < 0) {
printf("Error Number: %d\n", errno);
perror("dup2 stdout restore: ");
}
}
if (STDIN != -1) {
int response = dup2(STDIN, 0);
if(response < 0) {
printf("Error Number: %d\n", errno);
perror("dup2 stdout restore: ");
}
}
if (F_STDIN != 0) {
close(F_STDIN);
}
// close(fd[1]);
return save;
}