-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_getenv.c
More file actions
49 lines (48 loc) · 1006 Bytes
/
_getenv.c
File metadata and controls
49 lines (48 loc) · 1006 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
#include "shell.h"
/**
* _getenv - get a envioromental variable.
* @name: variable to read
* Return: value of the variable.
*/
char *_getenv(char *name)
{
unsigned int j = 0, i = 0, brk = 0;
char **variable;
char *aux;
int file_from, l_read;
char content[1100];
if (content == NULL)
return (NULL);
name = str_concat(name, "=");
file_from = open("env_file", O_RDONLY);
while (brk == 0)
{ l_read = read(file_from, content, 1024);
for (j = l_read; j < 1100; j++)
content[j] = '\0';
if (l_read == -1)
return (NULL);
variable = _strtok(content, "\n");
for (i = 0; variable[i] && brk != 1; i++)
{
if (cmpstr(variable[i], name) == 1)
brk = 1;
if (brk != 1)
free(variable[i]);
}
if (brk != 1)
free(variable);
if (l_read != 1024 && brk != 1)
brk = 2;
}
free(name);
if (brk == 1)
{
for (j = i; variable[j]; j++)
free(variable[j]);
aux = _strdup(variable[i - 1]);
free(variable[i - 1]);
free(variable);
return (aux);
}
return ("-1");
}