Repository navigation
Expand file tree
/
Copy pathparse_redir.h
More file actions
53 lines (51 loc) · 1.72 KB
/
Copy pathparse_redir.h
File metadata and controls
53 lines (51 loc) · 1.72 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
//Artur Vargas Carrión
// -----------------------------------------------------------------------
// Parse redirections operators '<' '>' once args structure has been built.
// Include this file and call the function immediately after get_commad():
//
// #include "parse_redir.h"
// ...
// while(...){
// // Shell main loop
// ...
// get_command(...);
// char *file_in, *file_out;
// parse_redirections(args, &file_in, &file_out);
// ...
// }
//
// For a valid redirection, a blank space is required before and after
// redirection operators '<' or '>'.
// --------------------------------------------------------------
static void parse_redirections(char **args, char **file_in, char **file_out){
*file_in = NULL;
*file_out = NULL;
char **args_start = args;
while (*args) {
int is_in = !strcmp(*args, "<");
int is_out = !strcmp(*args, ">");
if (is_in || is_out) {
args++;
if (*args){
if (is_in) *file_in = *args;
if (is_out) *file_out = *args;
char **aux = args + 1;
while (*aux) {
*(aux-2) = *aux;
aux++;
}
*(aux-2) = NULL;
args--;
} else {
/* Syntax error */
fprintf(stderr, "syntax error in redirection\n");
args_start[0] = NULL; // Do nothing
}
} else {
args++;
}
}
// Debug:
// *file_in && fprintf(stderr, "[parse_redirections] file_in='%s'\n", *file_in);
// *file_out && fprintf(stderr, "[parse_redirections] file_out='%s'\n", *file_out);
}