-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc_pipes.c
65 lines (59 loc) · 1.95 KB
/
proc_pipes.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* proc_pipes.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abdsalah <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 08:53:47 by abdsalah #+# #+# */
/* Updated: 2024/10/31 10:52:05 by abdsalah ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
void create_pipe(int pipefd[2], int i, int argc, int *files)
{
if (i != argc - 2)
{
if (pipe(pipefd) == -1)
error_exit("pipe failed", files, 2);
}
else
pipefd[1] = files[1];
}
void process_child(int *nums, int pipefd[2], char *cmd, char **envp)
{
if (nums[0] != nums[1] - 2)
close(pipefd[0]);
handle_command(nums[2], pipefd[1], cmd, envp);
}
void process_parent(int *prev_pipe_read_end, int pipefd[2], int i, int argc)
{
close(*prev_pipe_read_end);
if (i != argc - 2)
{
close(pipefd[1]);
*prev_pipe_read_end = pipefd[0];
}
}
void execute_pipeline(int argc, char *argv[], char **envp, int *files)
{
int prev_pipe_read_end;
int pipefd[2];
pid_t pid;
int i;
prev_pipe_read_end = files[0];
i = 1;
while (++i < argc - 1)
{
create_pipe(pipefd, i, argc, files);
pid = fork();
if (pid < 0)
error_exit("fork failed", (int []){prev_pipe_read_end, pipefd[0],
pipefd[1]}, 3);
if (pid == 0)
process_child((int []){i, argc, prev_pipe_read_end}, pipefd,
argv[i], envp);
else
process_parent(&prev_pipe_read_end, pipefd, i, argc);
}
}