-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipex_bonus.c
95 lines (88 loc) · 2.17 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
85
86
87
88
89
90
91
92
93
94
95
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kristori <kristori@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/13 11:59:25 by kristori #+# #+# */
/* Updated: 2023/01/17 11:16:56 by kristori ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
void ft_child_process(char *argv, char **envp)
{
pid_t pid;
int fd[2];
if (pipe(fd) == -1)
ft_error();
pid = fork();
if (pid == -1)
ft_error();
if (pid == 0)
{
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
ft_execute(argv, envp);
}
else
{
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
waitpid(pid, NULL, 0);
}
}
void ft_here_doc(char *limiter, int argc)
{
pid_t reader;
int fd[2];
char *line;
if (argc < 6)
return ;
if (pipe(fd) == -1)
ft_error();
reader = fork();
if (reader == 0)
{
close(fd[0]);
while (get_next_line(&line))
{
if (ft_strncmp(line, limiter, ft_strlen(limiter)) == 0)
exit(EXIT_SUCCESS);
write(fd[1], line, ft_strlen(line));
}
}
else
{
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
wait(NULL);
}
}
int main(int argc, char **argv, char **envp)
{
int i;
int file_in;
int file_out;
if (argc >= 5)
{
if (ft_strncmp(argv[1], "here_doc", 8) == 0)
{
i = 3;
file_out = ft_open_file(argv[argc - 1], 0);
ft_here_doc(argv[2], argc);
}
else
{
i = 2;
file_out = ft_open_file(argv[argc - 1], 1);
file_in = ft_open_file(argv[1], 2);
dup2(file_in, STDIN_FILENO);
}
while (i < argc - 2)
ft_child_process(argv[i++], envp);
dup2(file_out, STDOUT_FILENO);
ft_execute(argv[argc - 2], envp);
}
return (0);
}