-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipex.c
44 lines (40 loc) · 1.5 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abertran <abertran@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/08 17:56:57 by abertran #+# #+# */
/* Updated: 2023/06/05 16:38:14 by abertran ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
/* Child process that run inside a fork, take the filein, put the output inside
a pipe and then close with the exec function */
void child(char **av, char **envp, int *fd)
{
int filein;
filein = open(av[1], O_RDONLY, 0777);
if (filein == -1)
error();
dup2(fd[1], STDOUT_FILENO);
dup2(filein, STDIN_FILENO);
close(fd[0]);
}
/* If the args and everything works ok, runs the child and parent process */
int main(int ac, char **av, char **envp)
{
int fd[2];
pid_t pid;
if (ac == 5)
{
if (pipe(fd) == -1)
error();
pid = fork();
if (pid == -1)
error();
if (pid == 0)
child(av, envp, fd);
}
}