-
Notifications
You must be signed in to change notification settings - Fork 0
/
_llops.c
97 lines (91 loc) · 1.56 KB
/
_llops.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
#include "holberton.h"
/**
*create_ll - creates an empty linked list the size of the path variable
*@str: the PATH variable
*
*Return: a pointer to the empty array
*/
path_t *create_ll(char *str)
{
int i = 0;
int nodes = 1;
path_t *node, *head, *tmp, *end;
tmp = malloc(sizeof(path_t));
if (tmp == NULL)
return (NULL);
head = tmp;
end = malloc(sizeof(path_t));
if (end == NULL)
{
free(tmp);
return (NULL);
}
end->next = NULL;
while (str[i] != '\0')
{
if (str[i] == ':')
nodes++;
i++;
}
while ((nodes - 2) > 0)
{
node = malloc(sizeof(path_t));
if (node == NULL)
{
free(tmp);
free(end);
return (NULL);
}
tmp->next = node;
tmp = tmp->next;
nodes--;
}
tmp->next = end;
return (head);
}
/**
*fill_list - fills an empty linked list with PATH variable contents
*@str: the PATH variable
*@list: pointer to the empty linked list
*
*Return: pointer to the filled linked list
*/
path_t *fill_list(char *str, path_t *list)
{
path_t *ptr, *head;
char *dir;
int i = 0, j = 0, stcnt = 0, dirlen = 0;
if (str == NULL || list == NULL)
return (NULL);
head = list;
ptr = head;
while (ptr != NULL)
{
if (str[i] == ':' || str[i] == '\0')
{
if (str[i] != '\0')
i++;
dir = malloc(sizeof(char) * dirlen + 2);
if (dir == NULL)
return (NULL);
while (str[stcnt] != ':' && str[stcnt] != '\0')
{
dir[j] = str[stcnt];
stcnt++;
j++;
}
dir[j++] = '/';
dir[j] = '\0';
stcnt = i;
j = 0;
ptr->directory = dir;
ptr = ptr->next;
}
else
{
dirlen++;
i++;
}
}
return (head);
}