-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipex_multipipe3.c
66 lines (62 loc) · 2.39 KB
/
pipex_multipipe3.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex_multipipe3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pgober <pgober@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 17:13:15 by pgober #+# #+# */
/* Updated: 2024/04/17 12:10:39 by pgober ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex_multipipe.h"
#include "minishell.h"
static int child_helper(t_alloc *mllcd, int cmd_file, int **pipe_ends)
{
if (mllcd->pipex_m.cmdnum == 0 && \
mllcd->in_pars.cmd_table[mllcd->pipex_m.cmdnum][1] != NULL)
{
if (dup2(cmd_file, 0) == -1)
{
close(cmd_file);
return (pipex_error_handling(4, &mllcd->pipex_m));
}
}
else if (mllcd->pipex_m.cmdnum != 0)
{
if (dup2(pipe_ends[mllcd->pipex_m.cmdnum - 1][0], 0) == -1)
{
close(cmd_file);
return (pipex_error_handling(4, &mllcd->pipex_m));
}
}
if (dup2(pipe_ends[mllcd->pipex_m.cmdnum][1], 1) == -1)
return (pipex_error_handling(1, &mllcd->pipex_m));
return (-1);
}
int child(int **pipe_ends, t_alloc *mllcd, int *pid)
{
int cmd_file;
int i;
cmd_file = -1;
if (mllcd->in_pars.cmd_table[mllcd->pipex_m.cmdnum][1] != NULL)
cmd_file = open(mllcd->in_pars.cmd_table[mllcd->pipex_m.cmdnum][1], \
O_RDONLY);
if (cmd_file == -1 && \
mllcd->in_pars.cmd_table[mllcd->pipex_m.cmdnum][1] != NULL)
return (perror("minishell"), close_pipes(mllcd->pipex_m.pipenum, \
pipe_ends, mllcd->pipex_m.pipenum), \
pipex_free_all(&mllcd->pipex_m, NULL),
free_before_exit(mllcd, true), free(pid), exit(1), 1);
i = child_helper(mllcd, cmd_file, pipe_ends);
if (i != -1)
return (pipex_free_all(&mllcd->pipex_m, \
pipe_ends), pipex_error_handling(1, &mllcd->pipex_m));
close_pipes(mllcd->pipex_m.pipenum, pipe_ends, mllcd->pipex_m.pipenum);
if (cmd_file != -1)
close(cmd_file);
i = execute(mllcd);
free_before_exit(mllcd, true);
free(pid);
exit(i);
}