-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipex.c
More file actions
64 lines (57 loc) · 1.83 KB
/
Copy pathpipex.c
File metadata and controls
64 lines (57 loc) · 1.83 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebmarque < ebmarque@student.42porto.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/24 19:55:18 by ebmarque #+# #+# */
/* Updated: 2023/05/25 12:53:16 by ebmarque ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
void child_process(char *argv[], char *envp[], int *fd)
{
int filein;
filein = open(argv[1], O_RDONLY, 0777);
if (filein == -1)
std_error();
dup2(fd[1], STDOUT_FILENO);
dup2(filein, STDIN_FILENO);
close(fd[0]);
run(argv[2], envp);
}
void parent_process(char *argv[], char *envp[], int *fd)
{
int fileout;
fileout = open(argv[4], O_WRONLY | O_CREAT | O_TRUNC, 0777);
if (fileout == -1)
std_error();
dup2(fd[0], STDIN_FILENO);
dup2(fileout, STDOUT_FILENO);
close(fd[1]);
run(argv[3], envp);
}
int main(int argc, char *argv[], char *envp[])
{
int fd[2];
pid_t process;
if (argc == 5)
{
if (pipe(fd) == -1)
std_error();
process = fork();
if (process == -1)
std_error();
if (process == 0)
child_process(argv, envp, fd);
waitpid(process, NULL, 0);
parent_process(argv, envp, fd);
}
else
{
ft_putstr_fd("Error: Bad arguments\n\e", 2);
ft_putstr_fd("Ex: ./pipex <file1> <cmd1> <cmd2> <file2>\n", 1);
}
return (0);
}