-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipex_bonus.c
115 lines (105 loc) · 2.28 KB
/
pipex_bonus.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
#include "header/pipex_bonus.h"
void ft_for_first_child(int **pipe, char **argv, char **envp, int i)
{
char **cmd;
int fd1;
int d1;
int d2;
fd1 = open(argv[1], O_RDONLY);
if (fd1 == -1)
ft_call_cant_open();
d1 = dup2(fd1, 0);
if (d1 == -1)
ft_call_exit();
d2 = dup2(pipe[0][1], 1);
if (d2 == -1)
ft_call_exit();
close(pipe[0][0]);
cmd = ft_delenie_cmd(argv[i]);
cmd = ft_change_cmd(cmd, envp);
execve(cmd[0], cmd, envp);
ft_call_cant_exe();
}
void ft_for_last_child(int **pipe, char **argv, char **envp, int i)
{
char **cmd;
int fd2;
int d1;
int d2;
fd2 = open(argv[i + 3], O_CREAT | O_TRUNC | O_RDWR, 0777);
if (fd2 == -1)
ft_call_exit();
d1 = dup2(pipe[i - 1][0], 0);
if (d1 == -1)
ft_call_exit();
d2 = dup2(fd2, 1);
if (d2 == -1)
ft_call_exit();
close(pipe[i - 1][0]);
close(pipe[i - 1][1]);
cmd = ft_delenie_cmd(argv[i + 2]);
cmd = ft_change_cmd(cmd, envp);
execve(cmd[0], cmd, envp);
ft_call_cant_exe();
}
void ft_for_circle_child(int **pipe, char **argv, char **envp, int i)
{
char **cmd;
int d1;
int d2;
d1 = dup2(pipe[i - 1][0], 0);
if (d1 == -1)
ft_call_exit();
d2 = dup2(pipe[i][1], 1);
if (d2 == -1)
ft_call_exit();
close(pipe[i - 1][0]);
close(pipe[i - 1][1]);
close(pipe[i][0]);
cmd = ft_delenie_cmd(argv[i + 2]);
cmd = ft_change_cmd(cmd, envp);
execve(cmd[0], cmd, envp);
ft_call_cant_exe();
}
pid_t ft_circle_function(int **pipes, int argc, char **argv, char **envp)
{
int i;
pid_t pid;
i = -1;
while (++i <= (argc - 4))
{
if (i < (argc - 4))
pipe(pipes[i]);
if (i > 0)
close(pipes[i - 1][1]);
pid = fork();
if (pid < 0)
ft_call_exit();
else if (pid == 0)
{
if (i < (argc - 4))
argc -= ft_choose_child(pipes, argv, envp, i);
else
ft_choose_last_child(pipes, argv, envp, i);
}
if (i > 0)
close(pipes[i - 1][0]);
}
return (pid);
}
int main(int argc, char **argv, char **envp)
{
int **pipes;
int status;
pid_t pid;
if (argc <= 4)
ft_call_exit();
pipes = ft_create_pipes(argc);
if (!pipes)
return (1);
pid = ft_circle_function(pipes, argc, argv, envp);
ft_close_all(pipes, argc);
waitpid(pid, &status, 0);
ft_free_all(pipes, argc);
return (WEXITSTATUS(status));
}