-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_handlers.c
177 lines (161 loc) · 4.38 KB
/
env_handlers.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
#include "shell.h"
char *get_env_value(char *beginning, int len);
char **_getenv(const char *var);
int sh_env(char **args, char __attribute__((__unused__)) **front);
int sh_setenv(char **args, char __attribute__((__unused__)) **front);
int sh_unsetenv(char **args, char __attribute__((__unused__)) **front);
/**
* get_env_value - Gets the value corresponding to an environmental variable.
* @beginning: The environmental variable to search for.
* @len: The length of the environmental variable to search for.
* Return: If the variable is not found - an empty string.
* Otherwise - the value of the environmental variable.
* Description: Variables are stored in the format VARIABLE=VALUE.
*/
char *get_env_value(char *beginning, int len)
{
char **var_addr;
char *replacement = NULL, *temp, *var;
var = malloc(len + 1);
if (!var)
return (NULL);
var[0] = '\0';
_strncat(var, beginning, len);
var_addr = _getenv(var);
free(var);
if (var_addr)
{
temp = *var_addr;
while (*temp != '=')
temp++;
temp++;
replacement = malloc(_strlen(temp) + 1);
if (replacement)
_strcpy(replacement, temp);
}
return (replacement);
}
/**
* sh_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 sh_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);
}
/**
* sh_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 sh_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);
}
/**
* sh_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 sh_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);
}
/**
* _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);
}