forked from svilladaniel/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_functions.c
More file actions
66 lines (62 loc) · 1.02 KB
/
list_functions.c
File metadata and controls
66 lines (62 loc) · 1.02 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
#include "holberton.h"
/**
* _link - built linked list with directories
* @a: PATH
* Return: head
**/
link_t *_link(char *a)
{
link_t *head = NULL;
char *token;
token = _strtok(a, ":");
while (token != NULL)
{
head = _add_nodeint_end(&head, token);
token = _strtok(NULL, ":");
}
return (head);
}
/**
* _add_nodeint_end - add a new string to a node at the end
* @head: head
* @n: directories
* Return: head
**/
link_t *_add_nodeint_end(link_t **head, char *n)
{
link_t *new_node;
link_t *last_node = *head;
new_node = malloc(sizeof(link_t));
if (new_node == NULL)
return (NULL);
new_node->dir = n;
new_node->next = NULL;
if (*head == NULL)
{
*head = new_node;
return (*head);
}
else
{
while (last_node->next != NULL)
{
last_node = last_node->next;
}
last_node->next = new_node;
}
return (*head);
}
/**
* free_list - frees a list_t list
* @head: head of a node
*/
void free_list(link_t *head)
{
link_t *temp;
while (head != NULL)
{
temp = head;
head = head->next;
free(temp);
}
}