-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbonus_utils2.c
56 lines (51 loc) · 1.92 KB
/
bonus_utils2.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bonus_utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ablabib <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 13:48:20 by ablabib #+# #+# */
/* Updated: 2024/12/23 13:48:21 by ablabib ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
void handle_first_cmd(t_pipex *pipex, int i);
void handle_last_cmd(t_pipex *pipex, int i);
void child_process(t_pipex *pipex, int i);
void handle_first_cmd(t_pipex *pipex, int i)
{
if (i == 0)
{
pipex->infile = open(pipex->argv[1], O_RDONLY);
if (pipex->infile == -1)
failed_to(pipex);
if (dup2(pipex->infile, STDIN_FILENO) == -1)
failed_to(pipex);
close(pipex->infile);
}
else if (dup2(pipex->pipefd[i - 1][0], STDIN_FILENO) == -1)
failed_to(pipex);
}
void handle_last_cmd(t_pipex *pipex, int i)
{
if (i == pipex->cmd_num - 1)
{
pipex->outfile = open(pipex->argv[pipex->argc - 1],
O_WRONLY | O_CREAT | O_TRUNC, 0644);
if ((pipex->outfile) == -1)
failed_to(pipex);
if (dup2(pipex->outfile, STDOUT_FILENO) == -1)
failed_to(pipex);
close(pipex->outfile);
}
else if (dup2(pipex->pipefd[i][1], STDOUT_FILENO) == -1)
failed_to(pipex);
}
void child_process(t_pipex *pipex, int i)
{
handle_first_cmd(pipex, i);
handle_last_cmd(pipex, i);
close_all_pipes(pipex);
exec_path(pipex->cmd_args[i], pipex->env);
}