-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.c
More file actions
68 lines (62 loc) · 1.06 KB
/
command.c
File metadata and controls
68 lines (62 loc) · 1.06 KB
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
#include "hsh.h"
/**
* free_commands - free command list
*
* @head: start of the list
*/
void free_commands(command_t **head)
{
command_t *node = *head;
if (node)
{
free_commands(&node->next);
free_array(node->args);
free(node);
}
*head = NULL;
}
/**
* add_command - add a command node to the end of list
*
* @head: start of the list
* @args: arguments of the command
* @op: operator
*/
void add_command(command_t **head, const char *args, char op)
{
command_t *cmd, *tmp;
char *line;
cmd = malloc(sizeof(command_t));
if (cmd == NULL)
return;
line = _strdup(args);
cmd->args = split(line, " ");
cmd->op = op;
cmd->next = NULL;
cmd->name = cmd->args[0];
tmp = *head;
if (tmp == NULL)
*head = cmd;
else
{
while (tmp->next)
tmp = tmp->next;
tmp->next = cmd;
}
free(line);
}
/**
* next_command - move to the next command
*
* @head: start of the list
*
* Return: the poped command
*/
command_t *next_command(command_t **head)
{
command_t *next, *node = *head;
next = node->next;
node->next = NULL;
*head = next;
return (node);
}