-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmini_pipe.c
75 lines (69 loc) · 2 KB
/
mini_pipe.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mini_pipe.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ggiannit <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/30 16:23:16 by ggiannit #+# #+# */
/* Updated: 2023/04/14 14:44:21 by ggiannit ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_exec_pipe(t_mish *meta, t_cmd *node, int fd_in, int fd_out)
{
int stat;
int pid;
int pexit;
pid = fork();
if (!pid)
{
pexit = ft_do_red(meta, node);
if (pexit == 0)
{
ft_redirect(meta, fd_in, fd_out);
pexit = ft_handle_commands(meta, node);
ft_close_n_ret(fd_in, fd_out, -1, -2);
}
ft_free_shell(meta);
exit(pexit);
}
wait(&stat);
if (stat == 65280)
return (-1);
pexit = WEXITSTATUS(stat);
return (pexit);
}
int ft_mini_pipe(t_mish *meta, t_cmd *node, int fd_write)
{
int pp[2];
if (!node->next)
{
pp[1] = -1;
pp[0] = meta->c_stdin;
}
else
{
pipe(pp);
ft_mini_pipe(meta, node->next, pp[1]);
}
if (pp[1] != -1)
close(pp[1]);
return (ft_exec_pipe(meta, node, pp[0], fd_write));
}
int ft_pipe_or_not(t_mish *meta, t_cmd *node)
{
signal(SIGINT, ft_sign_handler_exec);
if (node->next)
return (ft_mini_pipe(meta, node, 1));
else
{
meta->exit_code = ft_do_red(meta, node);
if (meta->exit_code)
return (meta->exit_code);
ft_redirect(meta, -1, -1);
meta->exit_code = ft_handle_commands(meta, node);
ft_rev_redirect(meta);
return (meta->exit_code);
}
}