-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_to_list.c
58 lines (45 loc) · 1.13 KB
/
cmd_to_list.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
#include "command.h"
/**
* cmd_to_list - construct a linked list of tokenized commands
* @cmd: the command to parse
*
* Return: If memory allocation fails, return NULL. Otherwise, return a
* pointer to the head of the new list.
*/
cmdlist_t *cmd_to_list(const char *cmd)
{
cmdlist_t *head = NULL;
size_t count;
char *split = _strdup(cmd);
if (!split)
return (NULL);
count = split_cmd(split);
if (!_cmd_to_list(&head, split, count))
{
free_cmdlist(&head);
return (NULL);
}
free(split);
return (head);
}
/**
* _cmd_to_list - construct a linked list of tokenized commands (helper)
* @tailptr: pointer to the tail of the command list
* @split: a line split with null bytes on separators
* @count: the number of commands contained in split
*
* Return: If memory allocation fails, return NULL. Otherwise, return a
* pointer to the tail of the new list.
*/
cmdlist_t *_cmd_to_list(cmdlist_t **tailptr, char *split, size_t count)
{
cmdlist_t *tail;
if (!count)
return (*tailptr);
tail = add_cmd_end(tailptr, split);
if (!tail)
return (NULL);
while (*split++)
;
return (_cmd_to_list(&tail, split, count - 1));
}