-
Notifications
You must be signed in to change notification settings - Fork 0
/
5mgmt.c
71 lines (65 loc) · 2.15 KB
/
5mgmt.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 5mgmt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: guilmira <guilmira@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/06 10:31:20 by guilmira #+# #+# */
/* Updated: 2021/12/10 09:37:52 by guilmira ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
/** PURPOSE : Output error with given value 1, close the program.
* If exit signal is 0, it will prevent the appeareance of message:
* "make: *** [exe] Error 1" */
void ft_shut(char *str, int i)
{
write(1, str, ft_strlen(str));
exit(i);
}
/** PURPOSE : clears linked list from first element to last, including content
* Also frees internal struct memory. */
void ft_structclear(t_list *lst)
{
t_list *tmp;
t_command *command_struct;
if (!lst)
return ;
tmp = NULL;
command_struct = NULL;
while (lst)
{
tmp = lst->next;
command_struct = lst->content;
if (lst->content)
{
ft_free_split(command_struct->command);
if (command_struct->path)
free(command_struct->path);
free(command_struct);
free(lst);
}
lst = tmp;
}
}
/** PURPOSE : Free memory of program and close file descriptors.
* For the function to properly work, all pointers have been
* initialized to NULL at the beginning of the program. */
void free_heap_memory(t_arguments *args)
{
if (args)
{
if (args->commands_lst)
ft_structclear(args->commands_lst);
if (args->fds)
free(args->fds);
free (args);
}
}
/** PURPOSE : shutdown program freeing heap allocated memory. */
void ft_shutdown(char *str, int i, t_arguments *args)
{
free_heap_memory(args);
ft_shut(str, i);
}