-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_pipe.c
More file actions
157 lines (147 loc) · 4.31 KB
/
exec_pipe.c
File metadata and controls
157 lines (147 loc) · 4.31 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_pipe.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maustel <maustel@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 15:39:05 by maustel #+# #+# */
/* Updated: 2024/12/01 13:04:49 by maustel ### ########.fr */
/* */
/* ************************************************************************** */
#include "../minishell.h"
/*-------------------------------------------------------------
Close all opened filedescriptors from piping, we won't need
---------------------------------------------------------------*/
static int close_fds(t_shell *shell, int id, int nbr_pipes)
{
int i;
i = 0;
while (i < nbr_pipes)
{
if (id == 0 || i != id - 1)
{
if (close(shell->fd[i][0]) == -1)
return (errno);
}
if (i != id)
{
if (close(shell->fd[i][1]) == -1)
return (errno);
}
i++;
}
return (0);
}
/*-------------------------------------------------------------
Parent handler for pipechain
---------------------------------------------------------------*/
int pipe_parent(t_shell *shell, int nbr_pipes)
{
int wstatus;
int exit_code;
int i;
t_list *tmp;
if (close_fds(shell, -1, nbr_pipes))
exit (print_error(errno, NULL, PRINT));
tmp = shell->table;
exit_code = 0;
i = 0;
while (i <= nbr_pipes)
{
if (waitpid(shell->pid[i], &wstatus, 0) == -1)
return (1);
if (WIFEXITED(wstatus))
exit_code = WEXITSTATUS(wstatus);
if (WIFSIGNALED(wstatus))
exit_code = WTERMSIG(wstatus) + 128;
print_error(exit_code, NULL, NOTPRINT);
tmp = tmp->next;
i++;
}
return (exit_code);
}
/*-------------------------------------------------------------
Redirect input / output for pipechild
---------------------------------------------------------------*/
static int duplicate_fd(t_command *row, t_shell *shell, int nbr_pipes)
{
set_original_std(row);
if (row->id != 0 && row->final_infile == NULL)
{
if (dup2(shell->fd[row->id - 1][0], STDIN_FILENO) == -1)
return (print_error(errno, NULL, PRINT));
if (close(shell->fd[row->id - 1][0]) == -1)
return (print_error(errno, NULL, PRINT));
}
if (row->final_infile)
{
if (redirect_input(*row, &shell->fd[row->id][0]))
return (errno);
}
if (row->id != nbr_pipes && row->final_outfile == NULL)
{
if (redirect_output_pipe(&shell->fd[row->id][1]))
return (errno);
}
if (row->final_outfile)
{
if (redirect_output(*row, &shell->fd[row->id][1]))
return (errno);
}
return (0);
}
/*-------------------------------------------------------------
Child handler for pipechain
---------------------------------------------------------------*/
static void pipe_child(t_command *row, t_shell *shell)
{
int nbr_pipes;
int ret;
if (check_files(row))
free_child_exit(shell, print_error(-1, NULL, NOTPRINT));
nbr_pipes = ft_lstsize(shell->table) - 1;
if (close_fds(shell, row->id, nbr_pipes))
free_child_exit(shell, errno);
if (duplicate_fd(row, shell, nbr_pipes))
free_child_exit(shell, errno);
if (check_builtins(shell, row) < 1)
free_child_exit(shell, 0);
ret = get_check_path(row, shell->env);
if (ret)
free_child_exit(shell, ret);
if (!row->path)
free_child_exit(shell, 0);
if (execve(row->path, row->args, shell->env))
{
print_error(127, row->args[0], PRINT);
free_child_exit(shell, 127);
}
}
/*-------------------------------------------------------------
Loop through all the pipes
---------------------------------------------------------------*/
int pipechain_loop(t_shell *shell)
{
int n;
t_command *row;
t_list *tmp;
n = 0;
tmp = shell->table;
while (tmp != NULL)
{
row = (t_command *) tmp->content;
shell->pid[n] = fork();
if (shell->pid[n] == -1)
return (print_error(errno, NULL, PRINT));
if (shell->pid[n] == 0)
{
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
pipe_child(row, shell);
}
tmp = tmp->next;
n++;
}
return (0);
}