-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_cmd.c
More file actions
93 lines (86 loc) · 2.14 KB
/
Copy pathexec_cmd.c
File metadata and controls
93 lines (86 loc) · 2.14 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alborghi <alborghi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/21 16:02:07 by alborghi #+# #+# */
/* Updated: 2025/03/24 09:42:44 by alborghi ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void pipe_child(int *fd, t_data *data)
{
close(fd[0]);
if (dup2(fd[1], STDOUT_FILENO) == -1)
{
close(fd[1]);
ft_exit(data, 1);
}
close(fd[1]);
if (call_function(data) == -1)
{
ft_exit(data, 130);
}
ft_exit(data, data->out);
}
void pipe_parent(int *fd, t_data *data)
{
close(fd[1]);
if (dup2(fd[0], STDIN_FILENO) == -1)
{
close(fd[0]);
ft_exit(data, 1);
}
if (!is_builtin(data->cmds->cmd))
close(fd[0]);
else
data->fds = add_int_list(data->fds, fd[0]);
data->cmds = data->cmds->next;
exec_cmd(data);
if (!check_pipe(data->cmds))
{
close(fd[0]);
}
}
void do_pipe(t_data *data)
{
int fd[2];
int pid;
if (pipe(fd) == -1)
return (perror("pipe"));
pid = fork();
if (pid == -1)
return (perror("fork"));
data->pids = add_int_list(data->pids, pid);
if (pid == 0)
pipe_child(fd, data);
else
pipe_parent(fd, data);
}
void exec_cmd(t_data *data)
{
int pid;
if (check_pipe(data->cmds))
do_pipe(data);
else
{
if (!data->head->next)
call_function(data);
else
{
pid = fork();
if (pid == -1)
return (perror("fork"));
data->pids = add_int_list(data->pids, pid);
if (pid == 0)
{
if (call_function(data) == -1)
ft_exit(data, 130);
ft_exit(data, data->out);
}
}
reset_std(data);
}
}