-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_free.c
More file actions
108 lines (100 loc) · 2.83 KB
/
exec_free.c
File metadata and controls
108 lines (100 loc) · 2.83 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maustel <maustel@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/03 14:50:18 by maustel #+# #+# */
/* Updated: 2024/11/29 10:16:09 by maustel ### ########.fr */
/* */
/* ************************************************************************** */
#include "../minishell.h"
/*-------------------------------------------------------------
free double-pointer
---------------------------------------------------------------*/
void free_double(char **to_free)
{
int i;
i = 0;
while (to_free[i])
{
free (to_free[i]);
to_free[i] = NULL;
i++;
}
if (to_free)
free (to_free);
to_free = NULL;
}
/*-------------------------------------------------------------
Used in find_path
---------------------------------------------------------------*/
void free_paths(char **split_paths, char **append, char *big_path)
{
if (split_paths)
free_double (split_paths);
if (append)
free_double(append);
if (big_path)
{
free(big_path);
big_path = NULL;
}
}
/*-------------------------------------------------------------
delete all temporary heredoc files
---------------------------------------------------------------*/
void delete_heredoc_files(char *path)
{
if (access(path, F_OK) == 0)
unlink(path);
}
/*-------------------------------------------------------------
free all pointers of a row
---------------------------------------------------------------*/
int free_row(t_command *cmd)
{
if (cmd->args)
free_double(cmd->args);
if (cmd->filename)
free_double(cmd->filename);
if (cmd->red_symbol)
free_double(cmd->red_symbol);
if (cmd->path)
{
free (cmd->path);
cmd->path = NULL;
}
if (cmd->heredoc_file_path)
{
delete_heredoc_files(cmd->heredoc_file_path);
free (cmd->heredoc_file_path);
cmd->heredoc_file_path = NULL;
}
if (cmd)
free (cmd);
cmd = NULL;
return (1);
}
/*-------------------------------------------------------------
Free and set to NULL all pointers of whole table
---------------------------------------------------------------*/
int free_table(t_list *table)
{
t_list *tmp;
t_list *del;
t_command *cmd;
tmp = table;
while (tmp != NULL)
{
cmd = (t_command *) tmp->content;
if (cmd)
free_row(cmd);
del = tmp;
tmp = tmp->next;
free(del);
}
table = NULL;
return (1);
}