-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_pipe.c
69 lines (62 loc) · 1.85 KB
/
handle_pipe.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_pipe.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: almelo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/31 23:45:23 by almelo #+# #+# */
/* Updated: 2023/04/06 14:27:20 by almelo ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void set_pipe_io(int *pipefd, int *prevpipe)
{
close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[1]);
dup2(*prevpipe, STDIN_FILENO);
close(*prevpipe);
}
static void set_output_to_prevpipe(int *pipefd, int *prevpipe)
{
close(pipefd[1]);
close(*prevpipe);
*prevpipe = pipefd[0];
}
void ft_pipe(char **argv, char **envp, t_envl *env_lst, int *prevpipe)
{
pid_t pid;
int pipefd[2];
pipe(pipefd);
pid = fork();
if (pid == 0)
{
set_pipe_io(pipefd, prevpipe);
try_execute(argv, envp, env_lst);
exit(0);
}
else
set_output_to_prevpipe(pipefd, prevpipe);
}
void ft_last(char **argv, char **envp, t_envl *env_lst, int *prevpipe)
{
pid_t pid;
int status;
pid = fork();
if (pid == 0)
{
dup2(*prevpipe, STDIN_FILENO);
close(*prevpipe);
if (g_exit_status == 0)
try_execute(argv, envp, env_lst);
exit(g_exit_status);
}
else
{
close(*prevpipe);
while (wait(&status) != -1)
;
}
g_exit_status = WEXITSTATUS(status);
}