-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_set_unset.c
196 lines (184 loc) · 4.83 KB
/
env_set_unset.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
#include "main.h"
/**
* _env - prints the current environment.
*
*/
void _env(void)
{
int i = 0, rem, loop = 0;
char to_print[1024];
to_print[0] = 0;
while (environ && environ[i])
{
if (strlen(to_print) + strlen(environ[i]) + 2 > 1024)
{
fflush(stdout);
write(STDOUT_FILENO, to_print, strlen(to_print));
to_print[0] = 0;
}
if (strlen(to_print) == 0 && strlen(environ[i]) + 2 > 1024)
{
rem = strlen(environ[i]) + 2 - 1024;
while (rem > 0)
{
fflush(stdout);
write(STDOUT_FILENO, (environ[i] + (1024 * loop)), 1024);
rem -= 1024;
loop++;
}
sprintf(to_print + strlen(to_print), "%s\n", (environ[i] + (1024 * loop)));
i++;
continue;
}
sprintf(to_print + strlen(to_print), "%s\n", environ[i]);
i++;
}
fflush(stdout);
if (strlen(to_print))
write(STDOUT_FILENO, to_print, strlen(to_print));
}
/**
* set_unset_error - prints an error message.
* @error: integer indicating error type.
*
* Return: always -1 successful or 1 for failure.
*/
int set_unset_error(int error)
{
if (error == 0)
{
write(STDERR_FILENO, "error: name can't be a NULL pointer ", 36);
write(STDERR_FILENO, "or zero length string\n", 22);
return (-1);
}
if (error == 1)
{
write(STDERR_FILENO, "error: the first character of name ", 35);
write(STDERR_FILENO, "should be a letter or '_'\n", 26);
return (-1);
}
if (error == 2)
{
write(STDERR_FILENO, "error: name can't contain '='\n", 30);
return (-1);
}
if (error == 3)
{
write(STDERR_FILENO, "error: proper usage: setenv VARIABLE VALUE\n", 43);
return (-1);
}
if (error == 4)
{
write(STDERR_FILENO, "error: proper usage: unsetenv VARIABLE\n", 39);
return (-1);
}
return (1);
}
/**
* set_unset_env - a helper function that calls setenv or unsetenv.
* @tokens: pointer to an array of command arguments.
* @o_env_elms: pointer to an array of original environ elements address.
*
* Return: 0 if successful, or -1 if the number of arguments in tokens is
* incorrect ,if tokens[0] is null, points to a string of length zero or
* contains '='.
*/
int set_unset_env(char **tokens, char **o_env_elms)
{
int i, len_name;
char *e = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
if (!(tokens[1]) || !(*(tokens[1])))
return (set_unset_error(0));
if (strcspn(tokens[1], e))
return (set_unset_error(1));
len_name = strlen(tokens[1]);
for (i = 0; i < len_name; i++)
{
if (tokens[1][i] == '=')
return (set_unset_error(2));
}
if (!strcmp(tokens[0], "setenv"))
{
if (!tokens[2] || tokens[3])
return (set_unset_error(3));
return (_setenv(tokens[1], tokens[2], 1, o_env_elms));
}
if (!strcmp(tokens[0], "unsetenv"))
{
if (!tokens[1] || tokens[2])
return (set_unset_error(4));
return (_unsetenv(tokens[1], o_env_elms));
}
return (-1);
}
/**
* _setenv - if an environment variable `name` does not exist adds a variable
* named `name` with value of `value`. If `name` exists and `overwrite` is
* non-zero changes value of name to `value`.
* @name: name of environment variable
* @value: value of environment variable
* @overwrite: overwrite flag incase 'name' already exists.
* @o_en_el: pointer to an array of original environ elements address.
*
* Return: 0 if successful, or -1 there is insufficient memory to add
* a new variable.
*/
int _setenv(const char *name, const char *value, int overwrite, char **o_en_el)
{
char *new_var, **new_environ;
int len_name, len_value, null_index, index;
static int environ_alloced = -1;
len_name = strlen(name);
index = _getindex(name, environ);
null_index = (_get_null_index());
len_value = strlen(value);
if (index != -1 && !overwrite)
return (0);
new_var = malloc(sizeof(char) * (len_name + len_value + 2));
strncpy(new_var, name, len_name);
new_var[len_name] = '=';
strcpy((new_var + len_name + 1), value);
if (index != -1)
{
free_environ(index, NULL, o_en_el);
environ[index] = new_var;
}
else
{
if (environ_alloced == -1)
{
new_environ = malloc((null_index + 2) * sizeof(char *));
memcpy(new_environ, environ, (sizeof(char *) * null_index));
new_environ[null_index] = new_var;
new_environ[null_index + 1] = NULL;
environ = new_environ;
environ_alloced = 1;
}
else
{
environ = realloc(environ, ((null_index + 2) * sizeof(char *)));
environ[null_index] = new_var;
environ[null_index + 1] = NULL;
}
}
return (0);
}
/**
* _unsetenv - deletes variable named `name` from the environment
* @name: name of environment variable.
* @o_env_elms: pointer to an array of original environ elements address.
*
* Return: 0 if successful, or -1 for failure.
*/
int _unsetenv(const char *name, char **o_env_elms)
{
int i, index, null_index;
index = _getindex(name, environ);
if (index == -1)
return (0);
null_index = _get_null_index();
free_environ(index, NULL, o_env_elms);
for (i = index; i < null_index; i++)
environ[i] = environ[i + 1];
return (0);
}