-
Notifications
You must be signed in to change notification settings - Fork 0
/
freeFunc.c
97 lines (79 loc) · 1.54 KB
/
freeFunc.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
#include "main.h"
/**
* freeCommandInfo - add descr
* @shellInfo: add descr
* @freeAll: add descr
*/
void freeCommandInfo(commandInfo *shellInfo, int freeAll)
{
stringFree(shellInfo->command_arguments);
shellInfo->command_arguments = NULL;
shellInfo->executable_path = NULL;
if (freeAll)
{
if (!shellInfo->command_buffer)
free(shellInfo->current_argument);
if (shellInfo->environment_variables)
freeList(&(shellInfo->environment_variables));
if (shellInfo->command_history)
freeList(&(shellInfo->command_history));
if (shellInfo->alias_list)
freeList(&(shellInfo->alias_list));
stringFree(shellInfo->environment);
shellInfo->environment = NULL;
buffFree((void **)shellInfo->command_buffer);
if (shellInfo->input_file_descriptor > 2)
close(shellInfo->input_file_descriptor);
_putchar(FLUSH_BUFFER);
}
}
/**
* stringFree - add descr
* @pp: add descr
*/
void stringFree(char **pp)
{
char **a = pp;
if (!pp)
return;
while (*pp)
free(*pp++);
free(a);
}
/**
* buffFree - add descr
* @ptr: add descr
* Return: add descr
*/
int buffFree(void **ptr)
{
if (ptr && *ptr)
{
free(*ptr);
*ptr = NULL;
return (1);
}
return (0);
}
/**
* freeList - frees all nodes of a list
* @head_ptr: address of pointer to head node
*
* Return: void
*/
void freeList(listNode **head_ptr)
{
listNode *node, *next_node, *head;
if (!head_ptr || !*head_ptr)
return;
head = *head_ptr;
node = head;
while (node)
{
next_node = node->next;
free(node->value);
free(node);
node = next_node;
}
*head_ptr = NULL;
}