-
Notifications
You must be signed in to change notification settings - Fork 29
/
env1.c
80 lines (69 loc) · 1.38 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
75
76
77
78
79
80
#include "holberton.h"
/**
* cmp_env_name - compares env variables names
* with the name passed.
* @nenv: name of the environment variable
* @name: name passed
*
* Return: 0 if are not equal. Another value if they 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 environment variable
* @_environ: environment variable
*
* Return: value of the environment variable if is found.
* In other case, returns NULL.
*/
char *_getenv(const char *name, char **_environ)
{
char *ptr_env;
int i, mov;
/* Initialize ptr_env value */
ptr_env = NULL;
mov = 0;
/* Compare all environment variables */
/* environ is declared in the header file */
for (i = 0; _environ[i]; i++)
{
/* If name and env are equal */
mov = cmp_env_name(_environ[i], name);
if (mov)
{
ptr_env = _environ[i];
break;
}
}
return (ptr_env + mov);
}
/**
* _env - prints the evironment 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);
}