-
Notifications
You must be signed in to change notification settings - Fork 0
/
environ.c
70 lines (59 loc) · 1.16 KB
/
environ.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
#include "shell.h"
/**
* env - a function that handles the environment env
* @data: data containing envrionment
*/
void env(data_t *data)
{
list_t *list = data->envp;
while (list)
{
p_str(list->str, "\n");
list = list->next;
}
p_ch(-1);
}
/**
* initialize_path - a funtion that initializes the path variable
* @data: a list of arguments
* @envp: the path environment
*/
void initialize_path(data_t *data, char **envp)
{
list_t *list = NULL;
int i = 0;
while (envp[i])
add_node_end(&list, envp[i++]);
data->envp = list;
}
/**
* add_node_end - a function that adds a node at the
* end of a singly linked list
* @head: the head of the sinly linked list
* @str: the string to be added
* Return: 0
*/
void add_node_end(list_t **head, char *str)
{
list_t *ptr = NULL;
list_t *temp = NULL;
ptr = malloc(sizeof(list_t));
if (ptr == NULL)
exit(129); /* Cannot allocate memory */
ptr->str = _strdup(str);
if (ptr->str == NULL)
{
free(ptr);
exit(129); /* Cannot allocate memory */
}
ptr->next = NULL;
if (*head == NULL)
{
*head = ptr;
return;
}
temp = *head;
while (temp->next)
temp = temp->next;
temp->next = ptr;
}