-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean_exit.c
More file actions
91 lines (83 loc) · 1.95 KB
/
clean_exit.c
File metadata and controls
91 lines (83 loc) · 1.95 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* clean_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ferenc <ferenc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/26 12:07:17 by ferenc #+# #+# */
/* Updated: 2025/07/22 19:32:53 by ferenc ### ########.fr */
/* */
/* ************************************************************************** */
#include "Minishell.h"
void deallocate(t_token **root)
{
t_token *curr;
t_token *token;
int count;
count = 0;
if (!root || !*root)
return ;
curr = *root;
while (curr != NULL)
{
token = curr;
curr = curr->next;
if (token->com)
free(token->com);
free(token);
count++;
}
*root = NULL;
}
void free_array(char **arr)
{
int i;
if (!arr)
return ;
i = 0;
while (arr[i])
{
free(arr[i]);
arr[i] = NULL;
i++;
}
free(arr);
}
void close_free(t_token **tokens, t_shell *shell)
{
if (tokens)
deallocate(tokens);
if (shell)
{
if (shell->env_var)
{
free_array(shell->env_var);
shell->env_var = NULL;
}
if (shell->pwd)
free(shell->pwd);
if (shell->infile)
free(shell->infile);
if (shell->outfile)
free(shell->outfile);
}
}
void ft_exit(char **cmd, t_shell *shell)
{
if (size_cmd_arg(cmd) > 1)
{
printf("too many args\n");
return ;
}
else
shell->exit = 1;
}
void cleanup(char **args, t_shell *px, t_token **tokens, char **cmds)
{
free_array(args);
free_array(px->env_var);
free(px->pwd);
deallocate(tokens);
free_array(cmds);
}