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