forked from kei-en/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environ.c
95 lines (82 loc) · 1.72 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
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
#include "shell.h"
/**
* _getenv - gets the value of an environment variable
* @name: name of the environment variable
*
* Return: pointer to the value of the environment variable
*/
char *_getenv(const char *name)
{
int i, j, status;
for (i = 0; environ[i] != NULL; i++)
{
status = 1;
for (j = 0; environ[i][j] != '='; j++)
{
if (name[j] != environ[i][j])
{
status = 0;
break;
}
}
if (status)
{
return (&environ[i][j + 1]);
}
}
return (NULL);
}
/**
* _getpath - gets the value of the PATH environment variable
* @path: name of the environment variable
* @path_cpy: copy of the path variable
*
* Return: pointer to the value of the PATH environment variable
*/
list_t *_getpath(char *path, char **path_cpy)
{
char *token = NULL, *delim = ":\0";
list_t *head, *path_node;
if (path == NULL)
return (NULL);
*path_cpy = _strdup(path); /* Free on shellLoop() */
head = NULL;
path_node = malloc(sizeof(list_t));
if (path_node == NULL)
return (NULL);
token = strtok(*path_cpy, delim);
path_node->str = token;
path_node->next = head;
head = path_node;
while (token != NULL)
{
token = strtok(NULL, delim);
if (token == NULL) /* Don't save token NULL in list */
break;
path_node = malloc(sizeof(list_t));
if (path_node == NULL)
return (NULL);
path_node->str = token;
path_node->next = head;
head = path_node;
}
return (head);
}
/**
* listpath - Return a linked list of all directories of path
* @path_cpy: a
*
* Return: linked list (list_t)
*/
list_t *list_path(char **path_cpy)
{
char *path;
list_t *head = NULL;
path = _getenv("PATH");
if (*path == '\0')
return (NULL);
head = _getpath(path, path_cpy);
if (head == NULL)
return (NULL);
return (head);
}