-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathms_pipe_exec2.c
More file actions
95 lines (88 loc) · 2.33 KB
/
ms_pipe_exec2.c
File metadata and controls
95 lines (88 loc) · 2.33 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_pipe_exec2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jinsecho <jinsecho@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/19 18:20:05 by jinsecho #+# #+# */
/* Updated: 2024/12/20 20:23:01 by jinsecho ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int pipe_exec_begin(t_cmd *cmd, t_plst *tmp, int *fd, int **fds)
{
int i;
int pid;
i = 0;
pid = fork();
if (pid == 0)
{
dup2(fd[1], STDOUT_FILENO);
while (i < cmd->pipe_cnt)
{
close(fds[i][0]);
close(fds[i][1]);
i++;
}
if (tmp->heredoc_true)
dup2(tmp->heredoc_fd[0], STDIN_FILENO);
if (tmp->rdr_true)
dup2(tmp->file_fd, STDOUT_FILENO);
if (ms_builtin_func(cmd, tmp))
cmd_path_cat_exec(cmd, tmp);
exit (EXIT_SUCCESS);
}
return (pid);
}
int pipe_exec_end(t_cmd *cmd, t_plst *tmp, int *fd, int **fds)
{
int i;
int pid;
i = 0;
pid = fork();
if (pid == 0)
{
dup2(fd[0], STDIN_FILENO);
while (i < cmd->pipe_cnt)
{
close(fds[i][0]);
close(fds[i][1]);
i++;
}
if (tmp->heredoc_true)
dup2(tmp->heredoc_fd[0], STDIN_FILENO);
if (tmp->rdr_true)
dup2(tmp->file_fd, STDOUT_FILENO);
if (ms_builtin_func(cmd, tmp))
cmd_path_cat_exec(cmd, tmp);
exit (EXIT_SUCCESS);
}
return (pid);
}
int pipe_exec_middle(t_cmd *cmd, t_plst *tmp, t_pipefd *fd, int **fds)
{
int i;
int pid;
i = 0;
pid = fork();
if (pid == 0)
{
dup2(fd->p[0], STDIN_FILENO);
dup2(fd->c[1], STDOUT_FILENO);
while (i < cmd->pipe_cnt)
{
close(fds[i][0]);
close(fds[i][1]);
i++;
}
if (tmp->heredoc_true)
dup2(tmp->heredoc_fd[0], STDIN_FILENO);
if (tmp->rdr_true)
dup2(tmp->file_fd, STDOUT_FILENO);
if (ms_builtin_func(cmd, tmp))
cmd_path_cat_exec(cmd, tmp);
exit (EXIT_SUCCESS);
}
return (pid);
}