-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipex.c
123 lines (112 loc) · 2.6 KB
/
pipex.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dvan-der <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/14 13:55:14 by dvan-der #+# #+# */
/* Updated: 2022/02/09 14:22:02 by dvan-der ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
static void waitpid_forks(t_fork *forks)
{
int status;
t_fork *tmp;
while (forks->next)
{
waitpid(forks->child, &status, 0);
tmp = forks;
forks = forks->next;
free(tmp);
}
waitpid(forks->child, &status, 0);
if (WIFEXITED(status))
exit(WEXITSTATUS(status));
return ;
}
static void for_child(t_pipe *p, int *end, int fd, int file2)
{
int input;
int output;
close(end[0]);
input = arrange_input(p, fd);
if (dup2(input, STDIN_FILENO) < 0)
{
perror("");
close(input);
}
output = arrange_output(p, end[1], file2);
if (dup2(output, STDOUT_FILENO) < 0)
{
perror("");
close(output);
}
execute_cmd(p);
return ;
}
static int handle_fork(t_pipe *p, t_fork *a_fork, int fd)
{
int file2;
int end[2];
if (p->cmd_i == p->cmd_lim)
file2 = 1;
else
file2 = 0;
if (pipe(end) == -1)
{
perror("");
exit(errno);
}
a_fork->child = fork();
if (a_fork->child < 0)
{
perror("");
exit(errno);
}
else if (a_fork->child == 0)
for_child(p, end, fd, file2);
else if (a_fork->child > 0)
close(end[1]);
return (end[0]);
}
static t_fork *new_fork(t_fork *a_fork)
{
t_fork *new_fork;
new_fork = (t_fork *)malloc(sizeof(t_fork));
ft_check_malloc(new_fork);
new_fork->next = NULL;
if (!a_fork)
a_fork = new_fork;
else
{
a_fork->next = new_fork;
a_fork = a_fork->next;
}
return (a_fork);
}
void pipex(t_pipe *p, int fd)
{
bool first;
t_fork *head_forks;
t_fork *a_fork;
a_fork = NULL;
first = true;
while (p->cmd_i <= p->cmd_lim)
{
a_fork = new_fork(a_fork);
if (first)
{
head_forks = a_fork;
first = false;
}
conv_info(p, p->argv);
fd = handle_fork(p, a_fork, fd);
ft_free_char_array(p->mycmd);
(p->cmd_i)++;
}
waitpid_forks(head_forks);
free_pipex(p);
close(fd);
}