1 #include "qemu/osdep.h"
2 #include "qemu-common.h"
3 #include "qemu/queue.h"
4 #include "qemu/envlist.h"
7 const char *ev_var; /* actual env value */
8 QLIST_ENTRY(envlist_entry) ev_link;
12 QLIST_HEAD(, envlist_entry) el_entries; /* actual entries */
13 size_t el_count; /* number of entries */
16 static int envlist_parse(envlist_t *envlist,
17 const char *env, int (*)(envlist_t *, const char *));
20 * Allocates new envlist and returns pointer to that or
21 * NULL in case of error.
28 if ((envlist = malloc(sizeof (*envlist))) == NULL)
31 QLIST_INIT(&envlist->el_entries);
32 envlist->el_count = 0;
38 * Releases given envlist and its entries.
41 envlist_free(envlist_t *envlist)
43 struct envlist_entry *entry;
45 assert(envlist != NULL);
47 while (envlist->el_entries.lh_first != NULL) {
48 entry = envlist->el_entries.lh_first;
49 QLIST_REMOVE(entry, ev_link);
51 free((char *)entry->ev_var);
58 * Parses comma separated list of set/modify environment
59 * variable entries and updates given enlist accordingly.
62 * envlist_parse(el, "HOME=foo,SHELL=/bin/sh");
64 * inserts/sets environment variables HOME and SHELL.
66 * Returns 0 on success, errno otherwise.
69 envlist_parse_set(envlist_t *envlist, const char *env)
71 return (envlist_parse(envlist, env, &envlist_setenv));
75 * Parses comma separated list of unset environment variable
76 * entries and removes given variables from given envlist.
78 * Returns 0 on success, errno otherwise.
81 envlist_parse_unset(envlist_t *envlist, const char *env)
83 return (envlist_parse(envlist, env, &envlist_unsetenv));
87 * Parses comma separated list of set, modify or unset entries
88 * and calls given callback for each entry.
90 * Returns 0 in case of success, errno otherwise.
93 envlist_parse(envlist_t *envlist, const char *env,
94 int (*callback)(envlist_t *, const char *))
96 char *tmpenv, *envvar;
99 assert(callback != NULL);
101 if ((envlist == NULL) || (env == NULL))
104 if ((tmpenv = strdup(env)) == NULL)
109 envvar = strchr(tmpenv, ',');
110 if (envvar != NULL) {
113 if ((*callback)(envlist, tmpenv) != 0) {
118 } while (envvar != NULL);
125 * Sets environment value to envlist in similar manner
128 * Returns 0 in success, errno otherwise.
131 envlist_setenv(envlist_t *envlist, const char *env)
133 struct envlist_entry *entry = NULL;
137 if ((envlist == NULL) || (env == NULL))
140 /* find out first equals sign in given env */
141 if ((eq_sign = strchr(env, '=')) == NULL)
143 envname_len = eq_sign - env + 1;
146 * If there already exists variable with given name
147 * we remove and release it before allocating a whole
150 for (entry = envlist->el_entries.lh_first; entry != NULL;
151 entry = entry->ev_link.le_next) {
152 if (strncmp(entry->ev_var, env, envname_len) == 0)
157 QLIST_REMOVE(entry, ev_link);
158 free((char *)entry->ev_var);
164 if ((entry = malloc(sizeof (*entry))) == NULL)
166 if ((entry->ev_var = strdup(env)) == NULL) {
170 QLIST_INSERT_HEAD(&envlist->el_entries, entry, ev_link);
176 * Removes given env value from envlist in similar manner
177 * than unsetenv(3). Returns 0 in success, errno otherwise.
180 envlist_unsetenv(envlist_t *envlist, const char *env)
182 struct envlist_entry *entry;
185 if ((envlist == NULL) || (env == NULL))
188 /* env is not allowed to contain '=' */
189 if (strchr(env, '=') != NULL)
193 * Find out the requested entry and remove
196 envname_len = strlen(env);
197 for (entry = envlist->el_entries.lh_first; entry != NULL;
198 entry = entry->ev_link.le_next) {
199 if (strncmp(entry->ev_var, env, envname_len) == 0)
203 QLIST_REMOVE(entry, ev_link);
204 free((char *)entry->ev_var);
213 * Returns given envlist as array of strings (in same form that
214 * global variable environ is). Caller must free returned memory
215 * by calling free(3) for each element and for the array. Returned
216 * array and given envlist are not related (no common references).
218 * If caller provides count pointer, number of items in array is
219 * stored there. In case of error, NULL is returned and no memory
223 envlist_to_environ(const envlist_t *envlist, size_t *count)
225 struct envlist_entry *entry;
228 penv = env = malloc((envlist->el_count + 1) * sizeof (char *));
232 for (entry = envlist->el_entries.lh_first; entry != NULL;
233 entry = entry->ev_link.le_next) {
234 *(penv++) = strdup(entry->ev_var);
236 *penv = NULL; /* NULL terminate the list */
239 *count = envlist->el_count;