-
Notifications
You must be signed in to change notification settings - Fork 1
/
extended_memory_handlers.c
85 lines (75 loc) · 1.54 KB
/
extended_memory_handlers.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
#include "shell.h"
/**
* free_list - frees a path_t list
* @head: the head node
*/
void free_list(path_t **head)
{
path_t *current_node;
if (head == NULL || *head == NULL)
return;
while (*head != NULL)
{
current_node = (*head);
(*head) = (*head)->next; /* move to the next node */
safe_free(
current_node->pathname); /* free memory allocated for the string */
safe_free(current_node); /* free memory for the current node */
}
}
/**
* free_aliases - frees an alias_t list
* @head: a pointer to the list containing the aliases
*/
void free_aliases(alias_t **head)
{
alias_t *current;
if (*head == NULL || head == NULL)
return;
while (*head != NULL)
{
current = (*head);
(*head) = (*head)->next;
/* free memory */
safe_free(current->name);
safe_free(current->value);
safe_free(current);
}
}
/**
* multi_free - frees dynamically allocated memory
* @format: the format of how dynamically allocated variables are given
*
* Description: 's' is for a normal string (char *)
* 't' is for an array of strings (char **)
* 'p' is for the path_t list
* 'a' is for the alias_t list
*/
void multi_free(const char *format, ...)
{
va_list ap;
char *line;
va_start(ap, format);
while (*format != '\0')
{
switch (*format)
{
case 's':
line = va_arg(ap, char *);
safe_free(line);
break;
case 't':
free_str(va_arg(ap, char ***));
break;
case 'p':
free_list(va_arg(ap, path_t **));
break;
case 'a':
free_aliases(va_arg(ap, alias_t **));
break;
default:
break;
}
format++;
}
}