-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
75 lines (68 loc) · 2.15 KB
/
main.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: irychkov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/13 13:53:16 by irychkov #+# #+# */
/* Updated: 2024/09/29 17:53:45 by irychkov ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
static void get_error_fds(t_pipex *fds)
{
create_error_filename(&fds->error_filename1, 1, fds);
create_error_filename(&fds->error_filename2, 2, fds);
fds->error_fd1 = open(fds->error_filename1,
O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fds->error_fd1 < 0)
error_open(fds);
fds->error_fd2 = open(fds->error_filename2,
O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fds->error_fd2 < 0)
error_open(fds);
}
static void init_pipex(t_pipex *pipex)
{
pipex->pipex[0] = -1;
pipex->pipex[1] = -1;
pipex->fd[0] = -1;
pipex->fd[1] = -1;
pipex->error_fd1 = -1;
pipex->error_fd2 = -1;
pipex->error_filename1 = NULL;
pipex->error_filename2 = NULL;
}
static int pipex(char *av[], char **envp)
{
t_pipex fds;
pid_t pid1;
pid_t pid2;
init_pipex(&fds);
if (pipe(fds.fd) == -1)
error_pipe();
get_error_fds(&fds);
pid1 = fork();
if (pid1 == -1)
error_fork(&fds);
if (pid1 == 0)
first_child(av, &fds, envp);
pid2 = fork();
if (pid2 == -1)
error_fork(&fds);
if (pid2 == 0)
second_child(av, &fds, envp);
close_pipes(&fds);
return (wait_for_children(pid1, pid2, &fds));
}
int main(int ac, char *av[], char **envp)
{
int status;
status = 1;
if (ac == 5)
status = pipex(av, envp);
else
ft_putstr_fd("Usage: ./pipex file1 cmd1 cmd2 file2\n", 2);
return (status);
}