-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipex.c
More file actions
94 lines (87 loc) · 2.18 KB
/
pipex.c
File metadata and controls
94 lines (87 loc) · 2.18 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sakllam <sakllam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/01 17:10:49 by sakllam #+# #+# */
/* Updated: 2021/12/07 14:30:53 by sakllam ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
int ft_isparent(t_all_data *stock, t_commands **cmd)
{
if (dup2(stock->pipe_fds[0], 0) < 0)
exit(1);
if (close(stock->pipe_fds[1]))
exit(1);
*cmd = (*cmd)->next;
stock->count += 1;
return (0);
}
int ft_ischild(t_commands *cmd, t_all_data *stock, char *file)
{
if (cmd->next)
{
if (cmd->the_first)
{
if (ft_first_in_mobn(stock))
exit(1);
}
else
{
if (ft_second_in_mobn(stock))
exit(1);
}
}
else
{
if (ft_nonext(file, stock))
exit(1);
}
return (0);
}
int pipex(int ac, char **av, char **env)
{
t_commands *cmd;
t_all_data stock;
t_commands *tmp;
stock.fd_file1 = open(av[1], O_RDONLY);
ft_baliseexit(&stock);
cmd = get_all_cmds(ac, av, ft_get_executable_foulders(env));
ft_conditioncmd(cmd, &tmp);
while (cmd)
{
ft_fodu(&stock);
if (!stock.fork_respo)
{
ft_ischild(cmd, &stock, av[ac - 1]);
execve(cmd->pathcmd, cmd->cmd, NULL);
exit(1);
}
else
ft_isparent(&stock, &cmd);
}
while ((--stock.count + 1))
waitpid(stock.fork_respo, NULL, 0);
ft_tofree(&tmp);
return (0);
}
void ft_tofree(t_commands **tmp)
{
int i;
t_commands *pr;
while ((*tmp))
{
i = -1;
while ((*tmp)->cmd[++i])
free((*tmp)->cmd[i]);
free((*tmp)->pathcmd);
free((*tmp)->cmd);
pr = (*tmp);
(*tmp) = (*tmp)->next;
free(pr);
}
unlink(".her_doc");
}