-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipex_utils.c
81 lines (71 loc) · 1.77 KB
/
pipex_utils.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
76
77
78
79
80
81
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: melkholy <melkholy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/08 20:23:44 by melkholy #+# #+# */
/* Updated: 2022/09/10 15:21:22 by melkholy ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
int **ft_create_pipes(int proc)
{
int **pipes;
int *pip;
int count;
pipes = (int **)ft_calloc(proc, sizeof(int *));
if (!pipes)
return (NULL);
count = 0;
while (count < proc)
{
pip = (int *)ft_calloc(2, sizeof(int));
if (!pip)
return (NULL);
if (pipe(pip) < 0)
perror("Pipe creation failed");
pipes[count] = pip;
count ++;
}
return (pipes);
}
int *ft_create_pid(int proc)
{
int *pid;
pid = (int *)ft_calloc(proc, sizeof(int));
if (!pid)
return (NULL);
return (pid);
}
void ft_free_pth(char **cmd)
{
int count;
count = 0;
while (cmd[count])
{
free(cmd[count]);
count ++;
}
free(cmd);
}
void ft_free_pipes(int **pipes)
{
int count;
count = 0;
while (count < 2)
{
free(pipes[count]);
count ++;
}
free(pipes);
}
void ft_close_pipes(int **pipes, int count)
{
while (--count >= 0)
{
close(pipes[count][0]);
close(pipes[count][1]);
}
}