-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirect.c
114 lines (105 loc) · 3 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redirect.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bde-sous <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/30 18:51:31 by bde-sous #+# #+# */
/* Updated: 2023/10/09 19:12:18 by bde-sous ### ########.fr */
/* */
/* ************************************************************************** */
#include "redirect.h"
int ft_process_heredoc(int i, t_info *info)
{
int fd;
fd = -1;
while ((++i < info->tokens[0].total) && (info->tokens[i].type != pipo))
{
if (info->tokens[i].type == dredirectL)
fd = ft_heredoc(info->tokens[i + 1].t);
}
return (fd);
}
int ft_process_input(int i, int fd_input, t_info *info, int fd_heredoc)
{
if (fd_input != info->fds[0] && fd_input != 0)
close(fd_input);
if (info->tokens[i].type == dredirectL)
{
if (info->flag_stop != 1)
fd_input = fd_heredoc;
}
else
{
if (info->flag_stop != 1)
{
if (!access(info->tokens[i + 1].t, F_OK))
fd_input = open(info->tokens[i + 1].t, O_RDONLY);
else
{
info->flag_stop = 1;
fd_input = -1;
perror(info->tokens[i + 1].t);
}
}
}
return (fd_input);
}
int ft_process_output(int i, int fd, t_info *info)
{
if (fd != info->fds[1] && fd != 1)
close(fd);
if (info->tokens[i].type == redirectR)
fd = open(info->tokens[i + 1].t, O_WRONLY | O_CREAT | O_TRUNC, 0644);
else
fd = open(info->tokens[i + 1].t, O_CREAT | O_RDWR | O_APPEND, 0666);
if (fd == -1)
{
perror(info->tokens[i + 1].t);
info->flag_stop = 1;
}
return (fd);
}
int ft_pre_preocess_fd(t_info *i, int fd_input, int fd_output, int fd_heredoc)
{
int j;
int erro;
erro = 1;
j = ft_get_id_pipe(i->tokens, i->ordem);
while ((++j < i->tokens[0].total) && (i->tokens[j].type != pipo) && erro)
{
if ((i->tokens[j].type == redirectL) || \
(i->tokens[j].type == dredirectL))
{
i->fds[0] = ft_process_input(j, fd_input, i, fd_heredoc);
j++;
}
if ((i->tokens[j].type == redirectR) || \
(i->tokens[j].type == dredirectR))
{
i->fds[1] = ft_process_output(j, fd_output, i);
if (i->tokens[j + 2].type == pipo)
return (erro);
j++;
}
if ((i->fds[0] == -1) || (i->fds[1] == -1))
erro = 0;
}
return (erro);
}
int ft_process_fd(t_info *info)
{
int fd_input;
int fd_output;
int i;
int erro;
int fd_heredoc;
erro = 1;
i = ft_get_id_pipe(info->tokens, info->ordem);
fd_input = info->fds[0];
fd_output = info->fds[1];
fd_heredoc = ft_process_heredoc(i, info);
erro = ft_pre_preocess_fd(info, fd_input, fd_output, fd_heredoc);
return (erro);
}