Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yabtaour
GitHub Repository: yabtaour/Minishell-42
Path: blob/main/execution/my_mess/pipes.c
1407 views
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* pipes.c :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: ssabbaji <[email protected]> +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2022/08/09 14:12:33 by ssabbaji #+# #+# */
9
/* Updated: 2022/09/15 17:25:33 by ssabbaji ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
13
#include "../../minishell.h"
14
15
int **alloc_pipes(t_data *data)
16
{
17
int **fds;
18
int c;
19
int i;
20
21
c = c_lstcmd(data);
22
fds = malloc(sizeof(int *) * (c - 1));
23
if (!fds)
24
exit(1);
25
i = 0;
26
while (i < c - 1)
27
{
28
fds[i] = malloc(sizeof(int) * 2);
29
if (!fds[i])
30
exit(1);
31
pipe(fds[i]);
32
i++;
33
}
34
return (fds);
35
}
36
37
void init_fd(int **fd, t_cmd *cmd, int i)
38
{
39
if (i == 0 && cmd->next)
40
{
41
if (cmd->fd_out == 1)
42
cmd->fd_out = fd[i][1];
43
}
44
else if (i != 0 && cmd->next)
45
{
46
if (cmd->fd_in == 0)
47
cmd->fd_in = fd[i - 1][0];
48
if (cmd->fd_out == 1)
49
cmd->fd_out = fd[i][1];
50
}
51
else if (i != 0 && !cmd->next)
52
if (cmd->fd_in == 0)
53
cmd->fd_in = fd[i - 1][0];
54
}
55
56
int **initialize_pipes(t_data *data)
57
{
58
int i;
59
int **fd;
60
t_cmd *cmd;
61
62
i = 0;
63
cmd = data->lst_cmd;
64
fd = alloc_pipes(data);
65
if (!fd)
66
exit(1);
67
while (cmd)
68
{
69
init_fd(fd, cmd, i);
70
i++;
71
cmd = cmd->next;
72
}
73
return (fd);
74
}
75
76