-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_add_helper.c
161 lines (141 loc) · 3.53 KB
/
list_add_helper.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "shell.h"
/**
* add_variable_node - adds a variable to the end of a variable_list_t list.
*
* @head: pointer to the head of the linked list.
* @var_length: length of the variable name.
* @value: pointer to the value of the variable.
* @value_length: length of the value of the variable.
*
* Return: pointer to the head of the linked list.
*/
variable_list_t *add_variable_node(variable_list_t **head, int var_length,
char *value, int value_length)
{
variable_list_t *new_node, *temp_node;
new_node = malloc(sizeof(variable_list_t));
if (new_node == NULL)
{
perror("malloc error");
return (NULL);
}
new_node->var_len = var_length;
new_node->value = value;
new_node->val_len = value_length;
new_node->next = NULL;
if (*head == NULL)
{
*head = new_node;
}
else
{
temp_node = *head;
while (temp_node->next != NULL)
{
temp_node = temp_node->next;
}
temp_node->next = new_node;
}
return (*head);
}
/**
* add_command_node - adds a command line node to the end of a linked list.
* @list_head: pointer to the head of the linked list.
* @command_line: command line string to be added to the list.
*
* Return: Pointer to the head of the modified list.
*/
commands_list_node_t *add_command_node(commands_list_node_t **list_head,
char *command_line)
{
commands_list_node_t *new_node, *current_node;
new_node = malloc(sizeof(*new_node));
if (new_node == NULL)
return (NULL);
new_node->command_line = command_line;
new_node->next = NULL;
if (*list_head == NULL)
{
/* List is empty, add the new node as the head */
*list_head = new_node;
}
else
{
/* Traverse the list to find the last node */
current_node = *list_head;
while (current_node->next != NULL)
current_node = current_node->next;
/* Add the new node to the end of the list */
current_node->next = new_node;
}
return (*list_head);
}
/**
* add_separator_node - adds a separator node to the end of a list.
* @list_head: pointer to the head of the list.
* @separator: separator character to be added to the list.
*
* Return: Pointer to the head of the modified list.
*/
separator_list_node_t *add_separator_node(separator_list_node_t **list_head,
char separator)
{
separator_list_node_t *new_node, *current_node;
new_node = malloc(sizeof(*new_node));
if (new_node == NULL)
return (NULL);
new_node->separator_char = separator;
new_node->next = NULL;
if (*list_head == NULL)
{
/* List is empty, add the new node as the head */
*list_head = new_node;
}
else
{
/* Traverse the list to find the last node */
current_node = *list_head;
while (current_node->next != NULL)
current_node = current_node->next;
/* Add the new node to the end of the list */
current_node->next = new_node;
}
return (*list_head);
}
/**
* add_sep_nodes - adds a node to the separator list
*
* @head_s: head of separator
* @input: the command input
*/
void add_sep_nodes(separator_list_node_t **head_s, char *input)
{
int i;
convert_special_chars(input, 0);
for (i = 0; input[i]; i++)
{
if (input[i] == ';')
add_separator_node(head_s, input[i]);
if (input[i] == '|' || input[i] == '&')
{
add_separator_node(head_s, input[i]);
i++;
}
}
}
/**
* add_command_nodes - adds a node to the command list
*
* @head_c: head of commands
* @input: the command input
*/
void add_command_nodes(commands_list_node_t **head_c, char *input)
{
char *line;
line = strtok(input, ";|&");
do {
convert_special_chars(line, 1);
add_command_node(head_c, line);
line = strtok(NULL, ";|&");
} while (line != NULL);
}