-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfun_env.c
101 lines (95 loc) · 1.47 KB
/
fun_env.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "holberton.h"
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>
#define MAX 500
/**
* _env - show all environment variables.
* @h: node tha has the builtin command
*
* Return: no return
*/
int _env(command_t *h)
{
char **env = NULL, **args = NULL;
int i = 0, l = 0;
char sl = '\n';
(void)h;
args = h->args;
if (args[1] != NULL)
{
search_file_env(h);
return (0);
}
env = _setenv(NULL, NULL);
while (env[i])
{
for (l = 0; env[i][l]; l++)
;
write(1, env[i], l);
write(1, &sl, 1);
i++;
}
return (0);
}
/**
* _setenviron - set environ.
* @h: node tha has the builtin command
*
* Return: o if succes or -1
*/
int _setenviron(command_t *h)
{
char **args = NULL;
int i = 0, st = 0;
args = h->args;
for (; args[i]; i++)
;
if (i != 3)
{
setstatus(&st);
return (0);
}
_setenv(args[1], args[2]);
setstatus(&st);
return (0);
}
/**
* _unsetenv - unset variable.
* @h: node tha has the builtin command
*
* Return: no return
*/
int _unsetenv(command_t *h)
{
char **args = NULL;
char *var = NULL;
char *err = "Error\n";
int i = 0;
args = h->args;
for (; args[i]; i++)
;
if (i != 2)
{
write(STDERR_FILENO, err, _strlen(err));
return (-1);
}
var = _getenvvar(args[1]);
if (var)
{
_setenv(args[1], NULL);
free(var);
return (0);
}
else
{
write(STDERR_FILENO, err, _strlen(err));
return (-1);
}
return (-1);
}