-
Notifications
You must be signed in to change notification settings - Fork 0
/
free.c
67 lines (61 loc) · 923 Bytes
/
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
#include "hsh.h"
#include <stdlib.h>
/**
* free_list - frees a node_t list.
*
* @head: pointer to head node in the list to free
*/
void free_list(node_t **head)
{
node_t *tmp;
if (head != NULL)
{
while (*head != NULL)
{
tmp = *head;
*head = (*head)->next;
free(tmp->key);
free(tmp->value);
free(tmp);
}
*head = NULL;
}
}
/**
* free_array - free a malloced array
*
* @arr: NULL terminated array
*/
void free_array(char **arr)
{
int i = 0;
if (arr != NULL)
{
while (arr[i] != NULL)
free(arr[i++]);
free(arr);
}
}
/**
* free_buf - free a buffer
*
* @buf: buffer to free
*/
void free_buf(buf_t *buf)
{
free(buf->ptr);
buf->ptr = NULL;
buf->len = 0;
}
/**
* free_ctx - free the context variable
*
* @ctx: context varible to free
*/
void free_ctx(context_t *ctx)
{
free_buf(&ctx->buf);
free_list(&ctx->env);
free_list(&ctx->aliases);
free_commands(&ctx->cmd);
}