-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_env_path.c
96 lines (82 loc) · 1.63 KB
/
get_env_path.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 "nice_header.h"
/**
* _getenv - getenv recreation
* @name: char, used for path
* Return: null or string with the value of
* the requested environment variable
*/
char *_getenv(const char *name)
{
int i, j;
int 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);
}
/**
* _getpathdir - put tokens in list
* @path: string with the value of
* the requested environment variable
* @pathCopy: a
* Return: a
*/
Node *_getpathdir(char *path, char **pathCopy)
{
char *token = NULL;
Node *head;
Node *pathNode;
char *delim = ":\0"; /* null char*/
if (path == NULL)
return (NULL);
*pathCopy = _strdup(path); /* free on shellLoop() */
head = NULL;
pathNode = malloc(sizeof(Node));
if (pathNode == NULL)
return (NULL);
token = strtok(*pathCopy, delim);
pathNode->str = token;
pathNode->next = head;
head = pathNode;
while (token != NULL)
{
token = strtok(NULL, delim);
if (token == NULL) /* don't save token NULL in list */
break;
pathNode = malloc(sizeof(Node));
if (pathNode == NULL)
return (NULL);
pathNode->str = token;
pathNode->next = head;
head = pathNode;
}
return (head);
}
/**
* listpath - Return a linked list of all directories of path
* @pathCopy: path
* Return: linked list
*/
Node *listpath(char **pathCopy)
{
char *getEnv;
Node *pathDirs = NULL;
getEnv = _getenv("PATH");
if (*getEnv == '\0')
return (NULL);
pathDirs = _getpathdir(getEnv, pathCopy);
return (pathDirs);
}