-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirections.c
301 lines (278 loc) · 8.46 KB
/
redirections.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <wait.h>
#include <limits.h>
#include "redirections.h"
#include "split_string.h"
#include "utilslash.h"
#include "cd.h"
#include "pwd.h"
#include "star.h"
char mes_symboles[7][4] = {"<", ">", ">|", ">>", "2>>", "2>", "2>|"};
int exit_code = 0;
void handler(int sig){
exit_code = 255;
}
void commande_interne(char ** updated_args, int len_updated_args){
if (strcmp(updated_args[0], "cd") == 0){
exit_code = slash_cd(updated_args+1, len_updated_args-1);
}else if (strcmp(updated_args[0], "pwd") == 0){
exit_code = slash_pwd(updated_args+1, len_updated_args-1);
}else{
if (len_updated_args == 2){
if (is_number(updated_args[1])){
int exit_parameter = atoi(updated_args[1]);
free_2d_array(updated_args);
exit(exit_parameter);
}else{
write(STDERR_FILENO, "exit : error must be an integer\n", 32);
}
}else if (len_updated_args == 1){
free_2d_array(updated_args);
exit(exit_code);
}else{
write(STDERR_FILENO, "exit: too many arguments\n", 25);
}
}
}
void stdin_fd(char ** cmd, int fd){
dup2(fd, 0);
execvp(cmd[0], cmd);
}
void fd_stdout(char ** cmd, int fd){
dup2(fd, 1);
execvp(cmd[0], cmd);
}
void fd_stderr(char ** cmd, int fd){
dup2(fd, 2);
execvp(cmd[0], cmd);
}
int ind_sym(char * sym){
for(int i = 0; i < 7; i++){
if(strcmp(mes_symboles[i], sym) == 0){
return i;
}
}
return -1;
}
void parse_redirections(char **updated_args, int len){
int i, err = 0;
int fd_tmp0 = dup(0);
int fd_tmp1 = dup(1);
int fd_tmp2 = dup(2);
for(i = 0; i < len; i++){
switch(ind_sym(updated_args[i])){
case 0:
updated_args[i] = NULL;
err = lecture(updated_args[i+1]);
break;
case 1:
updated_args[i] = NULL;
err = sans_ecrasement_stdout(updated_args[i+1]);
break;
case 2:
updated_args[i] = NULL;
err = avec_ecrasement_stdout(updated_args[i+1]);
break;
case 3:
updated_args[i] = NULL;
err = en_concat_stdout(updated_args[i+1]);
break;
case 4:
updated_args[i] = NULL;
err = en_concat_stderr(updated_args[i+1]);
break;
case 5:
updated_args[i] = NULL;
err = sans_ecrasement_stderr(updated_args[i+1]);
break;
case 6:
updated_args[i] = NULL;
err = avec_ecrasement_stderr(updated_args[i+1]);
break;
}
}
i = 0;
while(updated_args[i]) i++;
if(err != 1){
if(is_intern(updated_args[0]) == 0) commande_interne(updated_args, i); else execvp(updated_args[0], updated_args);
}
dup2(fd_tmp0, 0);
dup2(fd_tmp1, 1);
dup2(fd_tmp2, 2);
}
// cmd < fic
int lecture(char *fic){
//for(int i = 0; i < len_cmd; i++) write(STDERR_FILENO, cmd[i], strlen(cmd[i]));
int fd = open(fic, O_RDONLY);
if(fd == -1){
exit_code = 1;
return 1;
}
dup2(fd, 0);
return 0;
}
// cmd > fic
int sans_ecrasement_stdout(char *fic){
int fd = open(fic, O_CREAT | O_EXCL | O_WRONLY , 0666);
if(fd == -1){
if(errno == EEXIST){
write(2, "le fichier existe déjà\n", strlen("le fichier existe déjà\n"));
}
exit_code = 1;
return 1;
}
else{
dup2(fd, 1);
return 0;
}
}
// cmd >| fic
int avec_ecrasement_stdout(char *fic){
int fd = open(fic, O_CREAT | O_TRUNC | O_WRONLY, 0666);
if(fd == -1){
exit_code = 1;
return 1;
}
dup2(fd, 1);
return 0;
}
// cmd >> fic
int en_concat_stdout(char * fic){
int fd = open(fic, O_WRONLY | O_CREAT | O_APPEND, 0666);
if(fd == -1){
exit_code = 1;
return 1;
}
dup2(fd, 1);
return 0;
}
// cmd 2>> fic
int en_concat_stderr(char *fic){
int fd = open(fic, O_WRONLY | O_CREAT | O_APPEND, 0666);
if(fd == -1){
exit_code = 1;
return 1;
}
dup2(fd, 2);
return 0;
}
// cmd 2> fic
int sans_ecrasement_stderr(char * fic){
int fd = open(fic, O_WRONLY | O_CREAT | O_EXCL, 0666);
if(fd < 0){
if(errno == EEXIST) write(2, "Le fichier existe déjà\n", strlen("Le fichier existe déjà\n"));
exit_code = 1;
return 1;
}
else {
dup2(fd, 2);
return 0;
}
}
// cmd 2>| fic
int avec_ecrasement_stderr(char * fic){
int fd = open(fic, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if(fd == -1){
exit_code = 1;
return 1;
}
dup2(fd, 2);
return 0;
}
void fork_tree(int *n, char ** splited_args){
char ** splited_cmd;
int len_cmd;
if( *n > 0){
pid_t pid;
int pipefd[2];
pipe(pipefd);
if((pid = fork()) < 0) write(STDERR_FILENO,"fork", strlen("fork"));
else{
if(pid == 0){
close(pipefd[0]);
dup2(pipefd[1], 1);
*n = *n - 1;
fork_tree(n, splited_args);
}
else{
close(pipefd[1]);
dup2(pipefd[0], 0);
int len_array = 0;
splited_cmd = allocate_splited_string();
len_cmd = split_string(splited_args[*n], " ", splited_cmd);
char ** star_path = malloc(10 * PATH_MAX * sizeof(char*));
for (int i=0;i<10*PATH_MAX;i++) star_path[i] = NULL;
star(splited_cmd, len_cmd, &len_array, star_path);
parse_redirections(star_path, len_array);
free_2d_array(star_path);
free_splited_string(splited_cmd);
exit(exit_code);
}
}
}
int len_array = 0;
splited_cmd = allocate_splited_string();
len_cmd = split_string(splited_args[0], " ", splited_cmd);
char ** star_path = malloc(10 * PATH_MAX * sizeof(char*));
for (int i=0;i<PATH_MAX;i++) star_path[i] = NULL;
star(splited_cmd, len_cmd, &len_array, star_path);
parse_redirections(star_path, len_array);
free_2d_array(star_path);
free_splited_string(splited_cmd);
exit(exit_code);
}
void pipeline(char *args){
char **splited_args = allocate_splited_string();
int len = split_string(args, " | ", splited_args);
int status, n = len - 1;
if(len > 1){
switch (fork()){
case -1:
write(STDERR_FILENO,"fork", strlen("fork"));
break;
case 0:
fork_tree(&n, splited_args);
break;
default:
wait(&status);
if (WEXITSTATUS(status) == 0) exit_code = 0; else exit_code = 1;
}
}
else{
struct sigaction sa = {0};
int len_array = 0;
char ** splited_cmd = allocate_splited_string();
int len_cmd = split_string(splited_args[0], " ", splited_cmd);
char ** star_path = malloc(10 * PATH_MAX * sizeof(char*));
for (int i=0;i<PATH_MAX;i++) star_path[i] = NULL;
star(splited_cmd, len_cmd, &len_array, star_path);
free_splited_string(splited_cmd);
if(is_intern(star_path[0]) == 0) parse_redirections(star_path, len_array);
else{
switch (fork()){
case -1:
write(STDERR_FILENO,"fork", strlen("fork"));
break;
case 0:
sa.sa_handler = handler;
sigaction(SIGINT,&sa,NULL);
sigaction(SIGTERM,&sa,NULL);
parse_redirections(star_path, len_array);
free_2d_array(star_path);
free_splited_string(splited_args);
exit(errno);
break;
default:
wait(&status);
if (!WIFSTOPPED(status) && WEXITSTATUS(status) == 0) exit_code = 0; else exit_code = 1;
}
}
free_2d_array(star_path);
}
free_splited_string(splited_args);
}