-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_getenv.c
51 lines (45 loc) · 832 Bytes
/
_getenv.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
#include "shell.h"
/**
* _getenv - function searches the environment list
* @env: variable to find
* @env_s: environment linked list
* Return: the buffer
*/
char *_getenv(char *env, node_t **env_s)
{
node_t *temp = *env_s;
char *buff;
int i, j, k = 0;
while (temp)
{
if (temp->s[0] == env[0])
{
for (i = 0; temp->s[i] != '='; i++)
{
if (temp->s[i] != env[i])
break;
if (temp->s[i + 1] == '=')
k = 1;
}
}
i++;
if (k == 1)
break;
else if (temp->next == NULL)
return (NULL);
temp = temp->next;
}
if (k != 1)
return (NULL);
for (j = i; temp->s[i]; i++)
;
buff = malloc(sizeof(char) * (i + 1));
if (buff == NULL)
{
write(STDERR_FILENO, "Cannot allocate memory\n", 24);
return (NULL);
}
for (k = 0; j <= i; j++, k++)
buff[k] = temp->s[j];
return (buff);
}