forked from Kinnune/minishell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe_re.c
110 lines (99 loc) · 2.11 KB
/
pipe_re.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipe_re.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: djames <djames@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/12 16:59:39 by djames #+# #+# */
/* Updated: 2023/07/18 12:31:37 by djames ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int close_pipe(int **fd, int i)
{
int j;
j = 0;
while (j <= i)
{
close(fd[j][0]);
close(fd[j][1]);
j++;
}
return (0);
}
int ft_free(int **fd, int *pid, int i)
{
int j;
j = 0;
while (j <= i)
{
if (fd[j])
free(fd[j]);
j++;
}
free(fd);
free(pid);
return (0);
}
int piepe_function(t_command *list, int i)
{
int **fd;
pid_t *pid;
int j;
fd = malloc(sizeof(int *) * (i + 1));
if (!fd)
return (0);
pid = malloc(sizeof(pid_t) * (i + 1));
if (!pid)
return (0);
j = 0;
while (j <= i)
{
fd[j] = malloc(sizeof(int) * 2);
if (!fd[j])
return (0);
j++;
}
j = ft_exec(list, i, fd, pid);
return (j);
}
int ft_exec(t_command *command, int i, int **fd, pid_t *pid)
{
int j;
int madona;
j = 0;
madona = 258;
while (j <= i)
{
if (pipe(fd[j]) == -1)
ft_free(fd, pid, i);
pid[j] = fork();
if (pid[j] == -1)
ft_free(fd, pid, i);
if (pid[j] == 0)
{
ft_exec35(command, i, fd, j);
}
if (command->next)
command = command->next;
j++;
}
return (ft_execaux(i, pid, fd));
}
int check_list(t_command *list)
{
int i;
int j;
t_command *temp;
i = 0;
temp = list;
j = 0;
while (temp != NULL)
{
temp = temp->next;
i++;
}
j = piepe_function(list, i - 1);
return (j);
}