-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfree.c
111 lines (102 loc) · 2.51 KB
/
free.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
111
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vilibert <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 11:22:05 by vilibert #+# #+# */
/* Updated: 2024/01/29 11:47:00 by vilibert ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int g_signal;
/**
* @brief exit the whole minishell with exitcode 1
*
* @param data
*/
void ft_crash(t_data *data)
{
data->status = 1;
ft_exit_prog(data);
}
/**
* @brief free a string array
*
* @param board the array to free
* @param i the first index in array to be freed
* @param quit 0 or 1 for exit and -1 for doing nothing
*/
void ft_free_strs(char **board, int i, int quit)
{
while (board && board[i])
free(board[i++]);
free(board);
if (quit == 1 || quit == 0)
exit(quit);
}
/**
* @brief free linked list from the lexer
*
* @param list
*/
void ft_free_lexed(t_lexed **list)
{
t_lexed *tmp;
while (*list && (*list)->prev)
*list = (*list)->prev;
while (*list)
{
tmp = (*list)->next;
free((*list)->word);
free(*list);
*list = tmp;
}
*list = NULL;
}
/**
* @brief free and close data except env
*
* @param data
*/
void ft_free_cycle(t_data *data)
{
int i;
i = 0;
if (data->the_array)
ft_free_strs(data->the_array, 0, 2);
ft_free_lexed(&(data->list));
if (data->exec)
{
while (data->exec[i].argv)
{
ft_free_strs(data->exec[i].argv, 0, 2);
free(data->exec[i].path);
if (data->exec[i].infile > 2)
close(data->exec[i].infile);
if (data->exec[i].outfile > 2)
close(data->exec[i].outfile);
i++;
}
free(data->exec);
data->exec = NULL;
}
if (data->line)
free(data->line);
data->line = 0;
g_signal = 0;
}
/**
* @brief free the whole data struct and exit on data->status
*
* @param data
*/
void ft_exit_prog(t_data *data)
{
ft_free_strs(data->env, 0, 2);
rl_clear_history();
ft_free_cycle(data);
enable_signal_print();
exit (data->status % 256);
}