-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_pipeline.c
138 lines (129 loc) · 4.17 KB
/
ft_pipeline.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
130
131
132
133
134
135
136
137
138
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pipeline.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenamar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/20 18:02:17 by abenamar #+# #+# */
/* Updated: 2023/11/21 01:34:40 by abenamar ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static uint8_t ft_pipe_setup(t_proc *prc, t_list *lst)
{
if (prc->readfd[0])
{
if (close(prc->readfd[1]) == -1)
return (ft_perror("close: "), 0);
if (!ft_tkn_count(lst, "<<", 0) && !ft_tkn_count(lst, "<", 0)
&& dup2(prc->readfd[0], STDIN_FILENO) == -1)
return (ft_perror("dup2: "), 0);
if (close(prc->readfd[0]) == -1)
return (ft_perror("close: "), 0);
}
if (prc->writefd[1])
{
if (close(prc->writefd[0]) == -1)
return (ft_perror("close: "), 0);
if (!ft_tkn_count(lst, ">>", 0) && !ft_tkn_count(lst, ">", 0)
&& dup2(prc->writefd[1], STDOUT_FILENO) == -1)
return (ft_perror("dup2: "), 0);
if (close(prc->writefd[1]) == -1)
return (ft_perror("close: "), 0);
}
return (1);
}
static uint8_t ft_fork(t_list **prcs, t_list **tkns, t_list **env)
{
pid_t cpid;
if (!(*prcs) || !((*prcs)->content))
return (0);
cpid = fork();
if (cpid == -1)
return (ft_perror("fork: "), ft_lstclear(tkns, &free), 0);
if (!cpid)
{
rl_clear_history();
if (!ft_pipe_setup((*prcs)->content, *tkns))
(ft_lstclear(prcs, &ft_prc_del), ft_lstclear(tkns, &free), \
ft_lstclear(env, &free), exit(EXIT_FAILURE));
ft_lstclear(prcs, &ft_prc_del);
return (ft_execute(tkns, env), 1);
}
if (((t_proc *)(*prcs)->content)->readfd[0])
{
if (close(((t_proc *)(*prcs)->content)->readfd[1]) == -1)
return (ft_perror("close: "), 0);
if (close(((t_proc *)(*prcs)->content)->readfd[0]) == -1)
return (ft_perror("close: "), 0);
}
return (((t_proc *)(*prcs)->content)->pid = cpid, 1);
}
static int ft_wait(t_list *prcs, int code)
{
pid_t cpid;
int wstatus;
if (!prcs)
return (code);
cpid = ((t_proc *) prcs->content)->pid;
(ft_wait(prcs->next, code), ft_lstdelone(prcs, &ft_prc_del));
if (cpid == -1)
return (code);
if (waitpid(cpid, &wstatus, WUNTRACED) == -1)
{
if (g_signum == SIGINT)
return (waitpid(cpid, NULL, WUNTRACED), printf("\n"), 130);
return (EXIT_FAILURE);
}
if (code != EXIT_SUCCESS)
return (code);
if (WIFSIGNALED(wstatus))
{
g_signum = WTERMSIG(wstatus);
if (g_signum == SIGQUIT)
printf("Quit\n");
return (128 + g_signum);
}
return (WEXITSTATUS(wstatus));
}
static uint8_t ft_next_pipe(t_list **prcs, t_list **tkns)
{
ft_lstadd_front(prcs, ft_lstnew(ft_prc_new()));
((t_proc *)(*prcs)->content)->readfd[0] = \
((t_proc *)(*prcs)->next->content)->writefd[0];
((t_proc *)(*prcs)->content)->readfd[1] = \
((t_proc *)(*prcs)->next->content)->writefd[1];
if (ft_tkn_count(*tkns, "|", 1))
{
if (pipe(((t_proc *)(*prcs)->content)->writefd) == -1)
return (ft_perror("pipe: "), 0);
}
return (1);
}
int ft_pipeline(t_list **tkns, t_list **env)
{
t_list *prcs;
prcs = ft_lstnew(ft_prc_new());
if (!ft_tkn_count(*tkns, "|", 1))
{
if (!ft_fork(&prcs, tkns, env))
return (ft_lstclear(tkns, &free), ft_wait(prcs, EXIT_FAILURE));
return (ft_wait(prcs, EXIT_SUCCESS));
}
while (*tkns)
{
if (!ft_next_pipe(&prcs, tkns))
return (ft_lstclear(tkns, &free), ft_wait(prcs, EXIT_FAILURE));
if (!ft_fork(&prcs, tkns, env))
return (ft_lstclear(tkns, &free), ft_wait(prcs, EXIT_FAILURE));
while (*tkns && ft_strncmp((*tkns)->content, "|", 2))
{
if (!ft_strncmp((*tkns)->content, "<<", 3))
ft_close(ft_atoi((*tkns)->next->content));
ft_lst_pop(tkns, &free);
}
ft_lst_pop(tkns, &free);
}
return (ft_wait(prcs, EXIT_SUCCESS));
}