-
Notifications
You must be signed in to change notification settings - Fork 0
/
environments.c
207 lines (176 loc) · 4.48 KB
/
environments.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "main.h"
char **_copyenv(void);
void free_env(void);
char **_getenv(const char *var);
int shellby_env(char **args, char __attribute__((__unused__)) **front);
int shellby_setenv(char **args, char __attribute__((__unused__)) **front);
int shellby_unsetenv(char **args, char __attribute__((__unused__)) **front);
/**
* _copyenv - Creates a copy of the environment.
*
* Return: If an error occurs - NULL.
* O/w - a double pointer to the new copy.
*/
char **_copyenv(void)
{
char **new_environ;
size_t size;
int index;
for (size = 0; environ[size]; size++)
;
new_environ = malloc(sizeof(char *) * (size + 1));
if (!new_environ)
return (NULL);
for (index = 0; environ[index]; index++)
{
new_environ[index] = malloc(_strlen(environ[index]) + 1);
if (!new_environ[index])
{
for (index--; index >= 0; index--)
free(new_environ[index]);
free(new_environ);
return (NULL);
}
_strcpy(new_environ[index], environ[index]);
}
new_environ[index] = NULL;
return (new_environ);
}
/**
* free_env - Frees the the environment copy.
*/
void free_env(void)
{
int index;
for (index = 0; environ[index]; index++)
free(environ[index]);
free(environ);
}
/**
* _getenv - Gets an environmental variable from the PATH.
* @var: The name of the environmental variable to get.
*
* Return: If the environmental variable does not exist - NULL.
* Otherwise - a pointer to the environmental variable.
*/
char **_getenv(const char *var)
{
int index, len;
len = _strlen(var);
for (index = 0; environ[index]; index++)
{
if (_strncmp(var, environ[index], len) == 0)
return (&environ[index]);
}
return (NULL);
}
/**
* shellby_env - Prints the current environment.
* @args: An array of arguments passed to the shell.
* @front: A double pointer to the beginning of args.
*
* Return: If an error occurs - -1.
* Otherwise - 0.
*
* Description: Prints one variable per line in the
* format 'variable'='value'.
*/
int shellby_env(char **args, char __attribute__((__unused__)) **front)
{
int index;
char nc = '\n';
if (!environ)
return (-1);
for (index = 0; environ[index]; index++)
{
write(STDOUT_FILENO, environ[index], _strlen(environ[index]));
write(STDOUT_FILENO, &nc, 1);
}
(void)args;
return (0);
}
/**
* shellby_setenv - Changes or adds an environmental variable to the PATH.
* @args: An array of arguments passed to the shell.
* @front: A double pointer to the beginning of args.
* Description: args[1] is the name of the new or existing PATH variable.
* args[2] is the value to set the new or changed variable to.
*
* Return: If an error occurs - -1.
* Otherwise - 0.
*/
int shellby_setenv(char **args, char __attribute__((__unused__)) **front)
{
char **env_var = NULL, **new_environ, *new_value;
size_t size;
int index;
if (!args[0] || !args[1])
return (create_error(args, -1));
new_value = malloc(_strlen(args[0]) + 1 + _strlen(args[1]) + 1);
if (!new_value)
return (create_error(args, -1));
_strcpy(new_value, args[0]);
_strcat(new_value, "=");
_strcat(new_value, args[1]);
env_var = _getenv(args[0]);
if (env_var)
{
free(*env_var);
*env_var = new_value;
return (0);
}
for (size = 0; environ[size]; size++)
;
new_environ = malloc(sizeof(char *) * (size + 2));
if (!new_environ)
{
free(new_value);
return (create_error(args, -1));
}
for (index = 0; environ[index]; index++)
new_environ[index] = environ[index];
free(environ);
environ = new_environ;
environ[index] = new_value;
environ[index + 1] = NULL;
return (0);
}
/**
* shellby_unsetenv - Deletes an environmental variable from the PATH.
* @args: An array of arguments passed to the shell.
* @front: A double pointer to the beginning of args.
* Description: args[1] is the PATH variable to remove.
*
* Return: If an error occurs - -1.
* Otherwise - 0.
*/
int shellby_unsetenv(char **args, char __attribute__((__unused__)) **front)
{
char **env_var, **new_environ;
size_t size;
int index, index2;
if (!args[0])
return (create_error(args, -1));
env_var = _getenv(args[0]);
if (!env_var)
return (0);
for (size = 0; environ[size]; size++)
;
new_environ = malloc(sizeof(char *) * size);
if (!new_environ)
return (create_error(args, -1));
for (index = 0, index2 = 0; environ[index]; index++)
{
if (*env_var == environ[index])
{
free(*env_var);
continue;
}
new_environ[index2] = environ[index];
index2++;
}
free(environ);
environ = new_environ;
environ[size - 1] = NULL;
return (0);
}