6
6
/* By: ycho2 <[email protected] > +#+ +:+ +#+ */
7
7
/* +#+#+#+#+#+ +#+ */
8
8
/* Created: 2024/09/28 19:10:33 by ycho2 #+# #+# */
9
- /* Updated: 2024/10/02 19:24:44 by ycho2 ### ########.fr */
9
+ /* Updated: 2024/10/03 04:55:26 by ycho2 ### ########.fr */
10
10
/* */
11
11
/* ************************************************************************** */
12
12
13
13
#include "minishell.h"
14
14
15
- static int redir_input_fork (t_inner_block_list * redir_l ,
16
- t_child_util * child_util );
17
- static int redir_output_fork (t_inner_block_list * redir_l ,
18
- t_child_util * child_util );
19
- static void set_pipe (t_child_util * child_util );
20
- static int redir_in_fork_word (int * flag , int * fd ,
21
- t_inner_block * cur_redir );
15
+ static int ft_redir_append (int * fd_out , char * file_name );
16
+ static int ft_redir_heredoc (int * fd_in , char * delimeter );
17
+ static int ft_redir_output (int * fd_out , char * file_name );
18
+ static int ft_redir_input (int * fd_in , char * file_name );
19
+ static int ft_set_child_redir_word (int * flag , int * fd_in ,
20
+ int * fd_out , t_inner_block * cur_redir );
22
21
23
- int set_child_redir (t_inner_block_list * redirect_list , t_child_util * child_util )
24
- {
25
- int err_flag ;
26
-
27
- set_pipe (child_util );
28
- err_flag = redir_input_fork (redirect_list , child_util );
29
- if (err_flag )
30
- return (1 );
31
- err_flag = redir_output_fork (redirect_list , child_util );
32
- if (err_flag )
33
- return (1 );
34
- return (0 );
35
- }
36
-
37
- static int redir_input_fork (t_inner_block_list * redirect_list ,
22
+ int set_child_redir (t_inner_block_list * redirect_list ,
38
23
t_child_util * child_util )
39
24
{
40
- int fd ;
25
+ int fd_out ;
26
+ int fd_in ;
41
27
int flag ;
42
28
t_inner_block * cur_redir ;
43
29
44
- fd = -1 ;
30
+ fd_in = -1 ;
31
+ fd_out = -1 ;
45
32
flag = 0 ;
46
33
cur_redir = redirect_list -> head ;
47
34
while (cur_redir )
48
35
{
49
36
if (cur_redir -> type == WORD )
50
37
{
51
- if (redir_in_fork_word (& flag , & fd , cur_redir ))
38
+ if (ft_set_child_redir_word (& flag , & fd_in , & fd_out , cur_redir ) == 1 )
52
39
return (1 );
53
40
}
54
41
else
55
42
flag = cur_redir -> type ;
56
43
cur_redir = cur_redir -> next ;
57
44
}
58
- if (fd >= 0 )
59
- child_util -> childfd [0 ] = fd ;
45
+ if (fd_in >= 0 )
46
+ child_util -> childfd [0 ] = fd_in ;
47
+ if (fd_out >= 0 )
48
+ child_util -> childfd [1 ] = fd_out ;
60
49
return (0 );
61
50
}
62
51
63
- static int redir_in_fork_word (int * flag , int * fd ,
64
- t_inner_block * cur_redir )
52
+ static int ft_set_child_redir_word (int * flag , int * fd_in ,
53
+ int * fd_out , t_inner_block * cur_redir )
65
54
{
66
55
if (* flag == REDIR_IN )
67
56
{
68
- if (* fd > 0 )
69
- close (* fd );
70
- * fd = open (cur_redir -> str , O_RDONLY , 0 );
71
- if (* fd < 0 )
72
- {
73
- err_exit (cur_redir -> str , strerror (errno ));
57
+ if (ft_redir_input (fd_in , cur_redir -> str ))
74
58
return (1 );
75
- }
76
59
}
77
60
else if (* flag == HEREDOC )
78
61
{
79
- if (* fd > 0 )
80
- close (* fd );
81
- * fd = open ("/var/tmp/tmp.txt" , O_RDWR | O_CREAT | O_TRUNC , 0644 );
82
- if (ft_heredoc (cur_redir -> str , * fd ) == 1 )
62
+ if (ft_redir_heredoc (fd_in , cur_redir -> str ))
63
+ return (1 );
64
+ }
65
+ else if (* flag == REDIR_OUT )
66
+ {
67
+ if (ft_redir_output (fd_out , cur_redir -> str ))
68
+ return (1 );
69
+ }
70
+ else if (* flag == REDIR_APPEND )
71
+ {
72
+ if (ft_redir_append (fd_out , cur_redir -> str ))
83
73
return (1 );
84
- close (* fd );
85
- * fd = open ("/var/tmp/tmp.txt" , O_RDONLY );
86
74
}
87
75
return (0 );
88
76
}
89
77
90
- static int redir_output_fork (t_inner_block_list * redirect_list ,
91
- t_child_util * child_util )
78
+ static int ft_redir_append (int * fd_out , char * file_name )
92
79
{
93
- int fd ;
94
- int flag ;
95
- t_inner_block * cur_redir ;
80
+ if (* fd_out > 0 )
81
+ close (* fd_out );
82
+ * fd_out = open (file_name , O_WRONLY | O_CREAT | O_APPEND , 0644 );
83
+ if (* fd_out < 0 )
84
+ {
85
+ err_exit (file_name , strerror (errno ));
86
+ return (1 );
87
+ }
88
+ else
89
+ return (0 );
90
+ }
96
91
97
- flag = 0 ;
98
- fd = -1 ;
99
- cur_redir = redirect_list -> head ;
100
- while (cur_redir )
92
+ static int ft_redir_input (int * fd_in , char * file_name )
93
+ {
94
+ if (* fd_in > 0 )
95
+ close (* fd_in );
96
+ * fd_in = open (file_name , O_RDONLY , 0 );
97
+ if (* fd_in < 0 )
101
98
{
102
- if (cur_redir -> type == WORD )
103
- {
104
- if (redir_out_fork_word (& flag , cur_redir , & fd ))
105
- return (1 );
106
- }
107
- else
108
- flag = cur_redir -> type ;
109
- cur_redir = cur_redir -> next ;
99
+ err_exit (file_name , strerror (errno ));
100
+ return (1 );
110
101
}
111
- if (fd >= 0 )
112
- child_util -> childfd [1 ] = fd ;
113
- return (0 );
102
+ else
103
+ return (0 );
114
104
}
115
105
116
- static void set_pipe ( t_child_util * child_util )
106
+ static int ft_redir_heredoc ( int * fd_in , char * delimeter )
117
107
{
118
- if (child_util -> pipe_i != 0 )
119
- child_util -> childfd [0 ] = child_util -> prev_pipe ;
120
- if (child_util -> pipe_i != child_util -> pipecnt )
121
- child_util -> childfd [1 ] = child_util -> pipefd [1 ];
108
+ if (* fd_in > 0 )
109
+ close (* fd_in );
110
+ * fd_in = open ("/var/tmp/tmp.txt" , O_RDWR | O_CREAT | O_TRUNC , 0644 );
111
+ if (ft_heredoc (delimeter , * fd_in ) == 1 )
112
+ return (1 );
113
+ close (* fd_in );
114
+ * fd_in = open ("/var/tmp/tmp.txt" , O_RDONLY );
115
+ return (0 );
122
116
}
117
+
118
+ static int ft_redir_output (int * fd_out , char * file_name )
119
+ {
120
+ if (* fd_out > 0 )
121
+ close (* fd_out );
122
+ * fd_out = open (file_name , O_WRONLY | O_CREAT | O_TRUNC , 0644 );
123
+ if (* fd_out < 0 )
124
+ {
125
+ err_exit (file_name , strerror (errno ));
126
+ return (1 );
127
+ }
128
+ return (0 );
129
+ }
0 commit comments