-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirection.c
More file actions
177 lines (140 loc) · 4.51 KB
/
redirection.c
File metadata and controls
177 lines (140 loc) · 4.51 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
175
176
177
#include "headers.h"
void remove_spaces(char *string){
int i, j = 0;
for (i = 0; string[i]; i++)
if (string[i] != ' ' && string[i] != '\n' && string[i] != '\t')
string[j++] = string[i];
string[j] = '\0';
// printf("spaces : \"%s\"\n", string);
}
void redirection(char *command){
int input = 0, output = 0, append = 0;
char *output_list[2];
char *input_list[2];
input_list[0] = command;
char *output_file = NULL;
char *input_file = NULL;
int input_fd, output_fd;
int actual_stdout = dup(STDOUT_FILENO);
int actual_stdin = dup(STDIN_FILENO);
if(strstr(command, "<") != NULL){
input = 1;
// printf("Input\n");
}
if(strstr(command, ">>") != NULL){
append = 1;
// printf("Append\n");
}
else if(strstr(command, ">") != NULL){
output = 1;
// printf("Output\n");
}
if(output == 1 || append == 1){
output_list[0] = strtok(command, ">");
output_list[1] = strtok(NULL, ">");
input_list[0] = output_list[0];
}
// printf("0: %s, 1: %s\n", output_list[0], output_list[1]);
if(input == 1){
input_list[0] = strtok(input_list[0], "<");
input_list[1] = strtok(NULL, "<");
// printf("0: %s, 1: %s\n", input_list[0], input_list[1]);
}
// int nargs = 0;
// char *args[100];
// args[nargs] = strtok(input_list[0], " ");
// while (args[nargs] != NULL)
// {
// nargs++;
// args[nargs] = strtok(NULL, " ");
// }
// args[nargs] = NULL;
// for(int i=0; i<nargs; i++){
// printf("%s\n", args[i]);
// }
int pid;
pid = fork();
if(pid == -1){
printf("\033[0;31mError: Unable to fork\033[0m\n");
perror("fork");
return;
}
else if(pid == 0){
if(input == 1){
input_file = strtok(input_list[1], " \n\t");
// printf("input file: \"%s\"\n", input_file);
if(input_file == NULL){
printf("\033[0;31mError: File for input redirection not specified\033[0m\n");
return;
}
input_fd = open(input_file, O_RDONLY);
if (input_fd < 0)
{
printf("\033[0;31mError: Unable to open file for input redirection\033[0m\n");
perror("open:");
return;
}
dup2(input_fd, 0);
close(input_fd);
}
if(output == 1){
output_file = strtok(output_list[1], " \t\n");
// printf("output file: \"%s\"\n", output_file);
if(output_file == NULL){
printf("\033[0;31mError: File for output redirection not specified\033[0m\n");
return;
}
output_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (output_fd < 0)
{
printf("\033[0;31mError: Unable to open file for output redirection\033[0m\n");
perror("open:");
return;
}
dup2(output_fd, 1);
close(output_fd);
}
if(append == 1){
output_file = strtok(output_list[1], " \t\n");
// printf("output file: \"%s\"\n", output_file);
if(output_file == NULL){
printf("\033[0;31mError: File for output redirection not specified\033[0m\n");
return;
}
output_fd = open(output_file, O_WRONLY | O_CREAT | O_APPEND, 0644);
if (output_fd < 0)
{
printf("\033[0;31mError: Unable to open file for output redirection\033[0m\n");
perror("open:");
return;
}
dup2(output_fd, 1);
close(output_fd);
}
int redirect;
if(strstr(input_list[0], ">") != NULL || strstr(input_list[0], "<") != NULL ){
redirect = 1;
}
if(redirect == 1){
redirection(input_list[0]);
}
else{
execute_command(input_list[0]);
}
// int x = execvp(args[0], args);
// if(x == -1){
// printf("\033[0;31mError: Unable to execute\033[0m\n");
// perror("execvp");
// return;
// }
dup2(actual_stdin, 0);
close(actual_stdin);
dup2(actual_stdout, 1);
close(actual_stdout);
}
else{
int status;
while (wait(&status) != pid);
}
return;
}