-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipex_redirect.c
129 lines (119 loc) · 3.29 KB
/
pipex_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex_redirect.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bsyvasal <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/17 12:03:01 by bsyvasal #+# #+# */
/* Updated: 2024/02/20 10:26:22 by bsyvasal ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char *cut_filename(char *str, char symbol, t_pipe *data)
{
int i;
int start;
char *filename;
char *tmp;
i = 0;
while (str[i] == symbol)
i++;
while (ft_isspace(str[i]))
i++;
start = i;
while (str[i] && str[i] != '<' && str[i] != '>' && !ft_isspace(str[i]))
{
if (str[i] == '\'' || str[i] == '"')
i += get_quote_length(str + i, str[i]);
else
i++;
}
tmp = ft_substr(str, start, i - start);
filename = interpret(tmp, data);
free(tmp);
ft_memset(str, ' ', i);
return (filename);
}
static int get_fd(char *cmd, char **filename, t_pipe *data)
{
int type;
int fd;
char symbol;
symbol = cmd[0];
fd = 0;
type = count_lead_chars(cmd, symbol);
if (type == 1 || type == 2)
{
if (*filename)
free(*filename);
*filename = cut_filename(cmd, symbol, data);
fd = openfile(*filename, symbol, type, data);
}
if (type >= 3)
return (-3);
return (fd);
}
static char *find_redirections(t_pipe *data, char *cmd, int *fd, char **file)
{
int i;
int index;
i = -1;
while (cmd[++i])
{
if (cmd[i] == '\'' || cmd[i] == '"')
i += get_quote_length(cmd + i, cmd[i]) - 1;
if (cmd[i] == '<' || cmd[i] == '>')
{
index = cmd[i] == '>';
if (fd[index] >= 2)
close(fd[index]);
fd[index] = get_fd(cmd + i, &file[index], data);
if (fd[index] == -3 && (ft_isspace(cmd[i + 2]) || !cmd[i + 2]))
return (ft_strdup("newline'"));
if (fd[index] == -3)
return (ft_strjoin(cmd + i + 2, "'"));
if (fd[index] < 0)
return (ft_strdup(file[index]));
}
}
return (NULL);
}
//infile
//type 0 = pipe / std-in
//outfile
//type 0 = pipe / std-out
static void set_basic_fd(int i, t_pipe *data, int *fd)
{
if (i == 0)
fd[0] = STDIN_FILENO;
else
{
fd[0] = data->pipe[(i + 1) % 2][0];
data->pipe[(i + 1) % 2][0] = -1;
}
if (i == data->cmdc - 1)
fd[1] = STDOUT_FILENO;
else
{
fd[1] = data->pipe[i % 2][1];
data->pipe[i % 2][1] = -1;
}
}
//fd[0] = input (generally pipe, but can be file/heredoc/stdin)
//fd[1] = output (generally pipe, but can be file/stdout)
void set_direction(t_pipe *data, int i, int *fd)
{
char *filename[2];
char *errormsg;
filename[0] = NULL;
filename[1] = NULL;
errormsg = NULL;
set_basic_fd(i, data, fd);
errormsg = find_redirections(data, data->cmds[i], fd, filename);
free_filenames(filename[0], filename[1]);
if (errormsg)
redirect_error(errormsg, fd, data);
else if (fd[0] < 0 || fd[1] < 0)
errormsg_exit("malloc error", 1, data);
}