1#include <assert.h>
2#include <errno.h>
3#include <stdlib.h>
4#include <string.h>
5#include <unistd.h>
6
7#include "qemu-queue.h"
8#include "envlist.h"
9
10struct envlist_entry {
11 const char *ev_var;
12 QLIST_ENTRY(envlist_entry) ev_link;
13};
14
15struct envlist {
16 QLIST_HEAD(, envlist_entry) el_entries;
17 size_t el_count;
18};
19
20static int envlist_parse(envlist_t *envlist,
21 const char *env, int (*)(envlist_t *, const char *));
22
23
24
25
26
27envlist_t *
28envlist_create(void)
29{
30 envlist_t *envlist;
31
32 if ((envlist = malloc(sizeof (*envlist))) == NULL)
33 return (NULL);
34
35 QLIST_INIT(&envlist->el_entries);
36 envlist->el_count = 0;
37
38 return (envlist);
39}
40
41
42
43
44void
45envlist_free(envlist_t *envlist)
46{
47 struct envlist_entry *entry;
48
49 assert(envlist != NULL);
50
51 while (envlist->el_entries.lh_first != NULL) {
52 entry = envlist->el_entries.lh_first;
53 QLIST_REMOVE(entry, ev_link);
54
55 free((char *)entry->ev_var);
56 free(entry);
57 }
58 free(envlist);
59}
60
61
62
63
64
65
66
67
68
69
70
71
72int
73envlist_parse_set(envlist_t *envlist, const char *env)
74{
75 return (envlist_parse(envlist, env, &envlist_setenv));
76}
77
78
79
80
81
82
83
84int
85envlist_parse_unset(envlist_t *envlist, const char *env)
86{
87 return (envlist_parse(envlist, env, &envlist_unsetenv));
88}
89
90
91
92
93
94
95
96static int
97envlist_parse(envlist_t *envlist, const char *env,
98 int (*callback)(envlist_t *, const char *))
99{
100 char *tmpenv, *envvar;
101 char *envsave = NULL;
102
103 assert(callback != NULL);
104
105 if ((envlist == NULL) || (env == NULL))
106 return (EINVAL);
107
108
109
110
111
112 if ((tmpenv = strdup(env)) == NULL)
113 return (errno);
114
115 envvar = strtok_r(tmpenv, ",", &envsave);
116 while (envvar != NULL) {
117 if ((*callback)(envlist, envvar) != 0) {
118 free(tmpenv);
119 return (errno);
120 }
121 envvar = strtok_r(NULL, ",", &envsave);
122 }
123
124 free(tmpenv);
125 return (0);
126}
127
128
129
130
131
132
133
134int
135envlist_setenv(envlist_t *envlist, const char *env)
136{
137 struct envlist_entry *entry = NULL;
138 const char *eq_sign;
139 size_t envname_len;
140
141 if ((envlist == NULL) || (env == NULL))
142 return (EINVAL);
143
144
145 if ((eq_sign = strchr(env, '=')) == NULL)
146 return (EINVAL);
147 envname_len = eq_sign - env + 1;
148
149
150
151
152
153
154 for (entry = envlist->el_entries.lh_first; entry != NULL;
155 entry = entry->ev_link.le_next) {
156 if (strncmp(entry->ev_var, env, envname_len) == 0)
157 break;
158 }
159
160 if (entry != NULL) {
161 QLIST_REMOVE(entry, ev_link);
162 free((char *)entry->ev_var);
163 free(entry);
164 } else {
165 envlist->el_count++;
166 }
167
168 if ((entry = malloc(sizeof (*entry))) == NULL)
169 return (errno);
170 if ((entry->ev_var = strdup(env)) == NULL) {
171 free(entry);
172 return (errno);
173 }
174 QLIST_INSERT_HEAD(&envlist->el_entries, entry, ev_link);
175
176 return (0);
177}
178
179
180
181
182
183int
184envlist_unsetenv(envlist_t *envlist, const char *env)
185{
186 struct envlist_entry *entry;
187 size_t envname_len;
188
189 if ((envlist == NULL) || (env == NULL))
190 return (EINVAL);
191
192
193 if (strchr(env, '=') != NULL)
194 return (EINVAL);
195
196
197
198
199
200 envname_len = strlen(env);
201 for (entry = envlist->el_entries.lh_first; entry != NULL;
202 entry = entry->ev_link.le_next) {
203 if (strncmp(entry->ev_var, env, envname_len) == 0)
204 break;
205 }
206 if (entry != NULL) {
207 QLIST_REMOVE(entry, ev_link);
208 free((char *)entry->ev_var);
209 free(entry);
210
211 envlist->el_count--;
212 }
213 return (0);
214}
215
216
217
218
219
220
221
222
223
224
225
226char **
227envlist_to_environ(const envlist_t *envlist, size_t *count)
228{
229 struct envlist_entry *entry;
230 char **env, **penv;
231
232 penv = env = malloc((envlist->el_count + 1) * sizeof (char *));
233 if (env == NULL)
234 return (NULL);
235
236 for (entry = envlist->el_entries.lh_first; entry != NULL;
237 entry = entry->ev_link.le_next) {
238 *(penv++) = strdup(entry->ev_var);
239 }
240 *penv = NULL;
241
242 if (count != NULL)
243 *count = envlist->el_count;
244
245 return (env);
246}
247