This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst_pipe.c
71 lines (66 loc) · 2.12 KB
/
first_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
70
71
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* first_pipe.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebouvier <ebouvier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/22 09:56:37 by mhoyer #+# #+# */
/* Updated: 2023/09/01 20:42:33 by ebouvier ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
void parent_first(int pipefd[2][2])
{
close_if(pipefd[0][0]);
pipefd[0][0] = pipefd[1][0];
pipefd[1][0] = -1;
close_if(pipefd[1][1]);
}
void child_first(t_command *cmd, int pipefd[2][2], t_minishell *ms)
{
if (!cmd->has_good_file)
close_all_pipe_free(pipefd, ms, cmd);
if (dup_in(cmd))
close_all_pipe_free(pipefd, ms, cmd);
if (cmd->mode & (M_APPEND | M_OUT))
{
close_if(pipefd[1][0]);
close_if(pipefd[1][1]);
if (dup_out(cmd))
close_all_pipe_free(pipefd, ms, cmd);
}
else
{
close_if(pipefd[1][0]);
dup2(pipefd[1][1], STDOUT_FILENO);
close_if(pipefd[1][1]);
}
close_if(ms->m_fd[0]);
close_if(ms->m_fd[1]);
}
int execute_first(t_command *cmd, t_minishell *minishell, int pipefd[2][2])
{
pid_t pid;
char **env;
pid = fork();
if (pid == -1)
return (1);
if (pid != 0)
ft_lstadd_back(&minishell->pids, ft_lstnew_int(pid));
if (pid == 0)
child_first(cmd, pipefd, minishell);
else
parent_first(pipefd);
if (pid == 0)
{
env = convert_env(minishell->env);
if (!env && !cmd->has_path && !cmd->builtin)
exec_failed(cmd, env, minishell, 1);
if (cmd->builtin)
builtin_pipe(cmd, env, minishell);
if (execve(cmd->command[0], cmd->command, env) == -1)
exec_failed(cmd, env, minishell, 1);
}
return (0);
}