-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipex_bonus.c
84 lines (76 loc) · 1.95 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mac <mac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/17 14:03:01 by mac #+# #+# */
/* Updated: 2022/02/21 18:24:23 by mac ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
void error(void)
{
perror("Error");
exit(EXIT_FAILURE);
}
void execute(char *argv, char **env)
{
char **cmd;
char *path;
int i;
i = -1;
cmd = ft_split(argv, ' ');
path = path_checker(cmd[0], env);
if (!path)
{
// while (cmd[++i])
// free(cmd[i]);
// free(cmd);
error();
}
if (execve(path, cmd, env) == -1)
error();
}
void process(char *argv, char **env)
{
pid_t pid;
int fd[2];
if (pipe(fd) == -1)
error();
pid = fork();
if (pid == -1)
error();
if (pid == 0)
{
close(fd[0]);
dup2(fd[1], STDOUT_FILENO); //dup2 copies the content of its arg 2 to its arg 1!
execute(argv, env);
}
else
{
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
waitpid(pid, NULL, 0);
}
}
int main(int argc, char **argv, char **env)
{
int i;
int filein;
int fileout;
if (argc >= 5)
{
i = 2;
filein = open(argv[1], O_RDONLY);
fileout = open(argv[argc - 1], O_CREAT | O_RDWR, 0644);
dup2(filein, STDIN_FILENO);
while (i < argc - 2)
process(argv[i++], env);
dup2(fileout, STDOUT_FILENO);
execute(argv[argc - 2], env);
}
else
error();
}