-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv1.c
executable file
·74 lines (62 loc) · 1.2 KB
/
env1.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "main.h"
/**
* cmp_env_name - compares env variables names
* with the name passed
* @nenv: name of the enviroment variable
* @name: name passed
* Return: 0 if are not equal. another value if the are
*/
int cmp_env_name(const char *nenv, const char *name)
{
int i;
for (i = 0; nenv[i] != '='; i++)
{
if (nenv[i] != name[i])
{
return (0);
}
}
return (i + 1);
}
/**
* _getenv - get an environment variable
* @name: name of the enviroment variable
* @_environ: environment variable
* Return: value of the environment variable if found
* NULL in other case
*/
char *_getenv(const char *name, char **_environ)
{
char *ptr_env;
int i, mov;
ptr_env = NULL;
mov = 0;
for (i = 0; _environ[i]; i++)
{
mov = cmp_env_name(_environ[i], name);
if (mov)
{
ptr_env = _environ[i];
break;
}
}
return (ptr_env + mov);
}
/**
* _env - prints the environment variables
* @datash: data relevant.
* Return: 1 on success.
*/
int _env(data_shell *datash)
{
int i, j;
for (i = 0; datash->_environ[i]; i++)
{
for (j = 0; datash->_environ[i][j]; j++)
;
write(STDOUT_FILENO, datash->_environ[i], j);
write(STDOUT_FILENO, "\n", 1);
}
datash->status = 0;
return (1);
}