-
Notifications
You must be signed in to change notification settings - Fork 2
/
dynamic_structs.c
83 lines (74 loc) · 1.48 KB
/
dynamic_structs.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
#include "shell.h"
#include <stdlib.h>
/**
* create_command - creates space for command structure containing data passed
* @command: array of pointers to tokens used in command including command
* itself.
* @separator: the character separator, either ';', '|', '&', or '\0'(none)
*
* Return: pointer to the newly allocated command
*/
command_t *create_command(char separator, char **command)
{
command_t *ret;
ret = malloc(sizeof(command_t));
if (!ret)
return (NULL);
ret->separator = separator;
ret->command = command;
ret->prev_valid = 1; /* default validity of previous command is true */
ret->next = NULL;
return (ret);
}
/**
* free_command_queue - frees a queue_t of command_t's
* @q: pointer to our queue containing commands
*
* Return: always void.
*/
void free_command_queue(queue_t *q)
{
command_t *temp = NULL;
if (!q)
return;
temp = q->front;
while (temp)
{
free_command(temp);
temp = temp->next;
}
q->front = q->rear = NULL;
free(q);
}
/**
* free_token_list - frees an array of tokens previously malloced
* @tokens: 2D array of tokens
*
* Return: always void.
*/
void free_token_list(char **tokens)
{
int i = 0;
if (!tokens)
return;
while (tokens[i])
{
free(tokens[i]);
i++;
}
free(tokens);
}
/**
* free_command - frees a command pointer of its token list and itself
* @command: command to free
*
* Return: always void.
*/
void free_command(command_t *command)
{
if (command)
{
free_token_list(command->command);
free(command);
}
}