-
Notifications
You must be signed in to change notification settings - Fork 0
/
free_arg.c
91 lines (83 loc) · 2.04 KB
/
free_arg.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* free_arg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkassand <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/27 01:28:39 by jkassand #+# #+# */
/* Updated: 2021/11/27 01:28:42 by jkassand ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void unlink_heredoc(t_red *red)
{
while (red)
{
if (red->in && red->two)
unlink(red->name);
red = red->next;
}
}
static void free_fd_env(t_arg *arg)
{
t_env *envp;
int i;
i = 0;
while (arg->fd && arg->fd[i])
free(arg->fd[i++]);
free(arg->fd);
i = 0;
while (arg->env && arg->env[i])
free(arg->env[i++]);
free(arg->env);
while (arg->envp)
{
envp = arg->envp;
arg->envp = arg->envp->next;
free(envp->key);
free(envp->sep);
free(envp->value);
free(envp);
}
}
static void free_arg(t_arg *arg)
{
t_red *red;
t_cmd *cmd;
int i;
unlink_heredoc(arg->red);
free_fd_env(arg);
while (arg->red)
{
red = arg->red;
arg->red = arg->red->next;
free(red->name);
free(red->limiter);
free(red);
}
while (arg->cmd)
{
cmd = arg->cmd;
arg->cmd = arg->cmd->next;
i = -1;
while (cmd->cmd[++i])
free(cmd->cmd[i]);
free(cmd->cmd);
free(cmd);
}
}
void ft_exit(int errnum, char *text, t_arg *arg)
{
char *errmsg;
if (text)
{
errmsg = strerror(errnum);
write(2, text, ft_strlen(text));
write(2, ": ", 2);
write(2, errmsg, ft_strlen(errmsg));
write(2, "\n", 1);
}
free_arg(arg);
exit(errnum);
}