-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpipes.c
67 lines (62 loc) · 2.06 KB
/
pipes.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipes.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simao <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/19 19:59:31 by simao #+# #+# */
/* Updated: 2023/08/09 12:20:28 by simao ### ########.fr */
/* */
/* ************************************************************************** */
#include "../minishell.h"
/*
- If node is the last in the list
- and previous node is a pipe this function will run.
*/
void output_from_pipe(t_list *node)
{
int pid;
int status;
get_data()->executing_cmd = 1;
pid = fork();
if (pid == 0)
{
redirect_stdin_to_pipe(node);
execute_command(node);
cmd_exit(ft_itoa(errno), 0);
}
close(get_pipe()->fd[1]);
close(get_pipe()->fd[0]);
waitpid(pid, &status, 0);
if (WIFEXITED(status))
get_data()->exit = WEXITSTATUS(status);
get_data()->executing_cmd = 0;
dup2(get_pipe()->stdin, STDIN_FILENO);
}
/*
- Executes the given command and output it to the write end of the pipe.
- It will read from the read end of the pipe if the previous node is a pipe.
*/
void write_to_pipe(t_list *node)
{
int status;
if (its_heredoc(node->prev))
return ;
if (its_input(node->prev))
return ;
redirect_stdin_to_pipe(node);
pipe(get_pipe()->fd);
get_data()->executing_cmd = 1;
get_data()->pid = fork();
if (get_data()->pid == 0)
{
redirect_stdout_to_pipe();
execute_command(node);
cmd_exit(ft_itoa(errno), 0);
}
waitpid(get_data()->pid, &status, 0);
if (WIFEXITED(status))
get_data()->exit = WEXITSTATUS(status);
get_data()->executing_cmd = 0;
}