-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd_ex.c
75 lines (66 loc) · 1.55 KB
/
cmd_ex.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
67
68
69
70
71
72
73
74
75
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_ex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yabdelgh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/13 17:06:42 by yabdelgh #+# #+# */
/* Updated: 2021/09/29 18:08:35 by yabdelgh ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int *create_pipes(int nbr_pipes)
{
int *pfds;
int i;
int j;
pfds = malloc(2 * nbr_pipes * sizeof(int));
i = 0;
while (i < nbr_pipes)
{
j = pipe(pfds + 2 * i);
if (j < 0)
{
perror("pipe: ");
exit(0);
}
i++;
}
return (pfds);
}
void close_pfds(int *pfds, int nbr)
{
int i;
i = 0;
while (i < 2 * nbr)
{
close(pfds[i]);
i++;
}
}
void wait_cmds(int nbr_cmds)
{
int i;
i = 0;
while (i < nbr_cmds)
{
wait(NULL);
i++;
}
}
int create_childs(t_list *cmds, int *pfds, t_list *envp)
{
int i;
int pid;
i = 0;
while (cmds)
{
pid = fork();
if (pid == 0)
exec_cmd(cmds, pfds, envp, i);
cmds = cmds->next;
i += 2;
}
return (pid);
}