-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_linked_list.c
124 lines (94 loc) · 1.98 KB
/
path_linked_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
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
#include "main.h"
#include <unistd.h>
#include <stdlib.h>
path_t *add_path_node(path_t **head, char *path);
int count_chars_to_delimiter(char *str);
/**
* generate_path_list - generates a linked list from the path environment var
* @head: pointer to pointer to list head
*
* Return: pointer to list head on success or NULL on error
*/
path_t *generate_path_list(path_t **head)
{
int offset = 0;
char *path, *tmp, *path_env = _getenv("PATH");
path = NULL;
if (!path_env)
return (NULL);
tmp = path_env;
while (*path_env)
{
if (*path_env == ':')
path_env++;
offset = count_chars_to_delimiter(path_env);
if (offset == -1)
return (NULL);
path = malloc(sizeof(char) * (offset + 2));
_strncpy(path, path_env, offset);
_strcat(path, "/");
if (add_path_node(head, path) == NULL)
{
free(path);
free(tmp);
free_path_list(head);
return (NULL);
}
path_env += offset;
}
free(tmp);
return (*head);
}
/**
* count_chars_to_delimiter - counts the number of characters
* before the delimeter :
* @str: string pointer with values seperated by :
*
* Return: number of characters before the delimeter
*/
int count_chars_to_delimiter(char *str)
{
int count = 0;
if (!str)
return (-1);
while (*str && *str != ':')
{
count++;
str++;
}
return (count);
}
/**
* add_path_node - adds a new node to a path_t linked list
* @head: pointer to pointer to head
* @path: string pointer for path
*
* Return: pointer to new node or NULL on error
*/
path_t *add_path_node(path_t **head, char *path)
{
path_t *node, *current;
current = node = NULL;
if (!head)
return (NULL);
if (*head == NULL)
{
node = malloc(sizeof(path_t));
if (!node)
return (NULL);
node->path = path;
node->next = NULL;
*head = node;
return (node);
}
current = *head;
while (current->next)
current = current->next;
node = malloc(sizeof(path_t));
if (!node)
return (NULL);
node->path = path;
node->next = NULL;
current->next = node;
return (node);
}