-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_env.c
More file actions
50 lines (47 loc) · 893 Bytes
/
get_env.c
File metadata and controls
50 lines (47 loc) · 893 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
39
40
41
42
43
44
45
46
47
48
49
50
#include "holberton.h"
/**
* _getenv - function to get the eviroment variable
* @str: string PATH
* Return: The value of variable
* @env: enviroment
*/
char *_getenv(char *str, char **env)
{
int i;
char *path = NULL;
for (i = 0; env[i] != NULL; i++)
{
if ((env[i][0] == str[0]) && (env[i][1] == str[1])
&& (env[i][2] == str[2]) && (env[i][3] == str[3]))
{
if (_strlen(env[i]) > 5 && (env[i][5] != ':'))
{
path = malloc(_strlen(env[i]) - _strlen(str));
if (path == NULL)
return (NULL);
_strcpy(path, _strchr(env[i], '/'));
}
else
{
path = NULL;
}
}
}
return (path);
}
/**
* print_env - function print the enviroment
* @env: enviroment
* Return: None
*/
int print_env(char **env)
{
unsigned int i = 0;
while (env[i])
{
write(STDOUT_FILENO, env[i], _strlen(env[i]));
write(STDOUT_FILENO, "\n", 1);
i++;
}
return (0);
}