-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_getenv.c
More file actions
43 lines (33 loc) · 674 Bytes
/
_getenv.c
File metadata and controls
43 lines (33 loc) · 674 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
#include "main.h"
/**
* _getenv - Get env variable by key
* @env: env from main args
* @env_variable: key of needed value
* Return: value of key if found. Otherwise return NULL
*/
char *_getenv(char **env, char *env_variable)
{
char *cpy;
char *key;
char *value, *cvalue;
int i = 0;
while (env[i])
{
cpy = (char *) malloc(_strlen(env[i]) + 1);
if (!cpy)
return (NULL);
_strcpy(cpy, env[i]);
key = strtok(cpy, "=");
value = strtok(NULL, "=");
if (_strcmp(key, env_variable) == 0)
{
cvalue = (char *) malloc(_strlen(value) + 1);
_strcpy(cvalue, value);
free(cpy);
return (cvalue);
}
i++;
free(cpy);
}
return (NULL);
}