forked from Ana-Morales/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetenv.c
More file actions
38 lines (36 loc) · 704 Bytes
/
getenv.c
File metadata and controls
38 lines (36 loc) · 704 Bytes
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
#include "holberton.h"
/**
* _getenv - function that gets an enviromental variable
*@name: the name of the variable to get its value
*Return: pointer to a string with the value of the enviroment variable.
*/
char *_getenv(const char *name)
{
char **env = NULL, *env1 = NULL, *tok = NULL, *path = NULL;
int i = 0;
if (name == NULL || *name == '\0')
name = "PATH";
env = environ;
while (env[i] != NULL)
{
env1 = _strdup(env[i]);
tok = strtok(env1, "=");
while (tok != NULL)
{
if (_strcmp(name, tok) == 0)
{
tok = strtok(NULL, "=");
path = _strdup(tok);
free(env1);
return (path);
}
else
{
free(env1);
break;
}
}
i++;
}
return (NULL);
}