-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipex.c
120 lines (110 loc) · 2.81 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajakob <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/04 14:02:20 by ajakob #+# #+# */
/* Updated: 2023/08/01 14:40:53 by ajakob ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
void ft_error(char *str)
{
perror("Error");
ft_putstr_fd(str, 2);
exit(EXIT_FAILURE);
}
void child(t_arg arg, int pipefd[], char *envp[])
{
char **arr;
char *cmd;
int fd;
fd = open(arg.infile, O_RDONLY);
if (fd == -1)
ft_error("can't open fd1");
if ((dup2(pipefd[1], STDOUT_FILENO)) == -1)
ft_error("pipe1dup");
dup2_close(fd, STDIN_FILENO, pipefd);
arr = alloc_arr(arg.cmd1);
if (arr == NULL)
ft_error("arr1");
cmd = find_path(arr[0], envp);
if (cmd == NULL)
{
ft_free(arr);
ft_error("cmd1");
}
if ((execve(cmd, arr, envp)) == -1)
{
ft_free(arr);
free(cmd);
ft_error("Wrong flags");
}
}
void second_child(t_arg arg, int pipefd[], char *envp[])
{
char **arr;
char *cmd;
int fd;
fd = open(arg.outfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (fd == -1)
ft_error("fd2");
if ((dup2(pipefd[0], STDIN_FILENO)) == -1)
ft_error("pipe2dup");
dup2_close(fd, STDOUT_FILENO, pipefd);
arr = alloc_arr(arg.cmd2);
if (arr == NULL)
ft_error("arr2");
cmd = find_path(arr[0], envp);
if (cmd == NULL)
{
ft_free(arr);
ft_error("cmd1");
}
if ((execve(find_path(arr[0], envp), arr, envp)) == -1)
{
ft_free(arr);
free(cmd);
ft_error("cmd2");
}
}
void ft_fork(t_arg arg, char *envp[])
{
int pipefd[2];
int pid2;
int pid;
pipe(pipefd);
pid = fork();
if (pid == 0)
child(arg, pipefd, envp);
else if (pid > 0)
{
pid2 = fork();
if (pid2 == 0)
second_child(arg, pipefd, envp);
}
if (pid > 0 && pid2 > 0)
{
close(pipefd[0]);
close(pipefd[1]);
waitpid(pid, NULL, 0);
waitpid(pid2, NULL, 0);
}
}
int main(int argc, char *argv[], char *envp[])
{
t_arg arg;
if (argc != 5)
return (ft_error("argc"), EXIT_FAILURE);
else if (*envp == NULL && !(access(argv[2], F_OK) == 0)
&& !(access(argv[3], F_OK) == 0))
return (ft_error("envp"), EXIT_FAILURE);
arg.infile = argv[1];
arg.cmd1 = argv[2];
arg.cmd2 = argv[3];
arg.outfile = argv[4];
ft_fork(arg, envp);
return (EXIT_SUCCESS);
}