-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiple_pipe.c
74 lines (71 loc) · 2.22 KB
/
multiple_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* multiple_pipe.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pmaimait <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/29 14:27:35 by pmaimait #+# #+# */
/* Updated: 2022/12/12 19:07:29 by pmaimait ### ########.fr */
/* */
/* ************************************************************************** */
#include"pipex_bonus.h"
void last_pipe(t_pipex *pipex, char *av, char *envp[])
{
if (pipe(pipex->end) == -1)
exit_perror("pipe error\n", 1);
pipex->pid2[2] = fork();
if (pipex->pid2[2] == -1)
exit_perror("fork error\n", 1);
if (pipex->pid2[2] == 0)
{
if (pipex->outfile == -1)
{
close(pipex->end[0]);
close(pipex->end[1]);
exit_perror("outfile", 1);
}
if (dup2(pipex->end[0], 0) == -1)
exit_perror("dup2 fail\n", 1);
if (dup2(pipex->outfile, 1) == -1)
exit_perror("dup2 fail\n", 1);
close(pipex->end[1]);
close(pipex->outfile);
close(pipex->end[0]);
execute(pipex, av, envp);
}
else
{
close(pipex->end[1]);
if (dup2(pipex->end[0], 0) == -1)
exit_perror("dup2 fail\n", 1);
close(pipex->end[0]);
if (pipex->outfile != -1)
close(pipex->outfile);
}
}
void multiple_pipe(t_pipex *pipex, char *av, char *envp[])
{
if (pipe(pipex->end) == -1)
exit_perror("pipe error\n", 1);
pipex->pid2[1] = fork();
if (pipex->pid2[1] == -1)
exit_perror("fork error\n", 1);
if (pipex->pid2[1] == 0)
{
if (dup2(pipex->end[1], 1) == -1)
exit_perror("dup2 fail\n", 1);
if (pipex->outfile != -1)
close(pipex->outfile);
close(pipex->end[0]);
close(pipex->end[1]);
execute(pipex, av, envp);
}
else
{
close(pipex->end[1]);
if (dup2(pipex->end[0], 0) == -1)
exit_perror("dup2 fail\n", 1);
close(pipex->end[0]);
}
}