-
Notifications
You must be signed in to change notification settings - Fork 1
/
_getenv.c
215 lines (209 loc) · 3.76 KB
/
_getenv.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
208
209
210
211
212
213
214
215
#include "shell.h"
/**
* getallenv - get all environment
* Return: environment
*
*/
char **getallenv()
{
char **environ = *(getenviron());
char **envcopy;
size_t len = 0;
envcopy = environ;
while (envcopy[len] != NULL)
len++;
#ifdef DEBUGMODE
printf("Got length of env lines %d\nNow copying\n", len);
#endif
envcopy = malloc(sizeof(char **) * (len + 1));
if (envcopy == NULL)
return (NULL);
while (len > 0)
{
envcopy[len] = environ[len];
len--;
}
envcopy[len] = environ[len];
return (envcopy);
}
/**
* setallenv - set whole environment for new value
* @envin: environment
* @newval: new value to be added
* Return: 0 if success, -1 if failure
*/
/* add should be null for first init to not free passed array */
int setallenv(char **envin, char *newval)
{
char ***environ = getenviron();
size_t len = 0;
#ifdef DEBUGMODE
printf("In setallenv, newval:%s\n", newval);
#endif
while (envin[len] != NULL)
len++;
if (newval != NULL)
len++;
*environ = malloc(sizeof(char **) * (len + 1));
if (*environ == NULL)
return (-1);
for (len = 0; envin[len] != NULL; len++)
if (newval == NULL)
{
(*environ)[len] = _strdup(envin[len]);
}
else
(*environ)[len] = envin[len];
if (newval != NULL)
{
#ifdef DEBUGMODE
printf("Adding newval:%s\n", newval);
#endif
(*environ)[len] = newval;
len++;
}
(*environ)[len] = NULL;
#ifdef DEBUGMODE
printf("At end. Free old environ if adding a string\n");
#endif
if (newval != NULL)
free(envin);
return (0);
}
/**
* _getenv - get local environment
* @name: environment variable
* Return: string of local environment
*/
char *_getenv(char *name)
{
char **environ = *(getenviron());
int i, j;
char *s;
#ifdef DEBUGMODE
printf("In getenv, name:%s\n", name);
#endif
i = 0;
while (environ[i] != NULL)
{
s = environ[i];
j = 0;
#ifdef DEBUGSVARS
printf("Checking against:%s\n", environ[i]);
#endif
while (s[j] == name[j])
{
j++;
if (name[j] == 0 && s[j] == '=')
return (_strdup(s + j + 1));
}
i++;
}
return (name);
}
/**
* _setenv - set environment for new value
* @name: name of variable
* @val: value of variable
* Return: 0 or setallenv if success, -1 if fail
*/
int _setenv(char *name, char *val)
{
char ***environroot = getenviron();
char **environ = *environroot;
int i, j, namel, vall;
char *s, *ptr;
#ifdef DEBUGMODE
printf("In setenv, name:%s:val:%s\n", name, val);
#endif
if (name == NULL || val == NULL)
return (0);
namel = _strlen(name);
vall = _strlen(val);
ptr = malloc(sizeof(char) * (namel + vall + 2));
if (ptr == NULL)
return (-1);
s = ptr;
_strcpy(s, name);
s += namel;
_strcpy(s++, "=");
_strcpy(s, val);
s += vall;
*s = 0;
#ifdef DEBUGMODE
printf("Ptr mallocd:%s\n", ptr);
#endif
i = 0;
while (environ[i] != NULL)
{
s = environ[i];
j = 0;
while (s[j] == name[j])
{
j++;
if (name[j] == 0 && s[j] == '=')
{
free(environ[i]);
environ[i] = ptr;
return (0);
}
}
i++;
}
return (setallenv(*environroot, ptr));
}
/**
* _unsetenv - unset environment
* @name: name of variable to unset
* Return: 0 if sucess
*
* testing functionality copy environ, if hits skip over, realloc
*/
int _unsetenv(char *name)
{
char **environ = *getenviron();
int i, j;
int check = 0;
char *s;
char **env;
#ifdef DEBUGMODE
printf("In unsetenv, name:%s\n", name);
#endif
if (name == NULL)
return (0);
i = 0;
while (environ[i] != NULL)
{
s = environ[i];
j = 0;
while (s[j] == name[j])
{
j++;
if (s[j] == '=' && name[j] == '\0')
{
check = 1;
break;
}
}
if (check == 1)
break;
i++;
}
free(environ[i]);
while (environ[i] != NULL)
{
environ[i] = environ[i + 1];
i++;
}
environ[i] = NULL;
env = environ;
setallenv(env, NULL);
i = 0;
while (env[i])
{
free(env[i]);
i++;
}
free(env);
return (0);
}