-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv_func.c
163 lines (141 loc) · 2.91 KB
/
env_func.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "shell.h"
#define l(x) _strlen((x))
#define cch const char
/**
* _putenv - write to env variables
* @es: env var value arg pair
* Return: 0 on success and -1 on faliure
*/
int _putenv(char *es)
{
char **_environ = environ, **newenviron, **_newenviron;
int len = 0;
while (*_environ)
len++, _environ++;
newenviron = _malloc(sizeof(char *) * (len + 4));
_newenviron = newenviron;
_environ = environ;
while (len)
{
*_newenviron = _malloc(sizeof(char) * _strlen(*_environ) + 4);
_strcpy(*_newenviron, *_environ);
_newenviron++, _environ++, len--;
}
*_newenviron = _malloc(sizeof(char) * _strlen(es) + 4);
_strcpy(*_newenviron, es);
free(es);
_newenviron++;
*_newenviron = NULL;
free_pp(environ);
environ = newenviron;
return (0);
}
/**
* _setenv - set env variables
* @name: variable name
* @value: value
* @overwrite: overwrite status
*
* Return: 0 on success and -1 on faliure
*/
int _setenv(cch *name, cch *value, __attribute__((unused))int overwrite)
{
char *es, **ep, *var;
int i = 0;
if (name == NULL || name[0] == '\0' || value == NULL)
return (-1);
if (environ)
{
ep = _arrdup(environ);
while (ep[i])
{
var = _strtok(ep[i], "=", 0);
if (!_strcmp(var, (char *)name))
{
free(environ[i]);
environ[i] = _malloc(_strlen(name) + _strlen(value) + 4);
_strcpy(environ[i], (char *)name);
_strcat(environ[i], "=");
_strcat(environ[i], (char *)value);
free_pp(ep);
return (0);
}
i++;
}
free_pp(ep);
}
es = _malloc(_strlen(name) + _strlen(value) + 2);
if (es == NULL)
return (-1);
_strcpy(es, (char *)name), _strcat(es, "="), _strcat(es, (char *)value);
return ((_putenv(es) != 0) ? -1 : 0);
free(es);
}
/**
* _unsetenv - delete env variables
* @name: name of varaible
* Return: 0 on success and -1 on faliure
*/
int _unsetenv(const char *name)
{
char **ep, **sp, *var, *value;
if (name == NULL || name[0] == '\0')
return (-1);
ep = _arrdup(environ);
free_pp(environ);
environ = _malloc(sizeof(char *));
for (sp = ep; *sp != NULL; )
{
var = _strtok(*sp, "=", 0);
if (_strcmp(var, (char *)name))
{
value = _strtok(NULL, "=", 0);
_setenv(var, value, 1);
}
sp++;
}
free_pp(ep);
return (0);
}
/**
* _printenv - print env variables
*/
void _printenv(void)
{
char **_env = environ;
if (!_env)
return;
_write(-1, NULL, 0);
while (*_env)
{
write(1, *_env, _strlen(*_env));
write(1, "\n", 1);
_env++;
}
write(1, NULL, 0);
}
/**
* _getenv - get env variables
* @name: name of variable
* Return: pointer to env variables
*/
char *_getenv(const char *name)
{
int len, i;
const char *np;
char **p, *cp;
if (name == NULL || environ == NULL)
return (NULL);
for (np = name; *np && *np != '='; ++np)
;
len = np - name;
for (p = environ; (cp = *p) != NULL; ++p)
{
for (np = name, i = len; i && *cp; i--)
if (*cp++ != *np++)
break;
if (i == 0 && *cp++ == '=')
return (cp);
}
return (NULL);
}