7 #include "qemu-queue.h"
10 struct envlist_entry {
11 const char *ev_var; /* actual env value */
12 QLIST_ENTRY(envlist_entry) ev_link;
16 QLIST_HEAD(, envlist_entry) el_entries; /* actual entries */
17 size_t el_count; /* number of entries */
20 static int envlist_parse(envlist_t *envlist,
21 const char *env, int (*)(envlist_t *, const char *));
24 * Allocates new envlist and returns pointer to that or
25 * NULL in case of error.
32 if ((envlist = malloc(sizeof (*envlist))) == NULL)
35 QLIST_INIT(&envlist->el_entries);
36 envlist->el_count = 0;
42 * Releases given envlist and its entries.
45 envlist_free(envlist_t *envlist)
47 struct envlist_entry *entry;
49 assert(envlist != NULL);
51 while (envlist->el_entries.lh_first != NULL) {
52 entry = envlist->el_entries.lh_first;
53 QLIST_REMOVE(entry, ev_link);
55 free((char *)entry->ev_var);
62 * Parses comma separated list of set/modify environment
63 * variable entries and updates given enlist accordingly.
66 * envlist_parse(el, "HOME=foo,SHELL=/bin/sh");
68 * inserts/sets environment variables HOME and SHELL.
70 * Returns 0 on success, errno otherwise.
73 envlist_parse_set(envlist_t *envlist, const char *env)
75 return (envlist_parse(envlist, env, &envlist_setenv));
79 * Parses comma separated list of unset environment variable
80 * entries and removes given variables from given envlist.
82 * Returns 0 on success, errno otherwise.
85 envlist_parse_unset(envlist_t *envlist, const char *env)
87 return (envlist_parse(envlist, env, &envlist_unsetenv));
91 * Parses comma separated list of set, modify or unset entries
92 * and calls given callback for each entry.
94 * Returns 0 in case of success, errno otherwise.
97 envlist_parse(envlist_t *envlist, const char *env,
98 int (*callback)(envlist_t *, const char *))
100 char *tmpenv, *envvar;
101 char *envsave = NULL;
103 assert(callback != NULL);
105 if ((envlist == NULL) || (env == NULL))
109 * We need to make temporary copy of the env string
110 * as strtok_r(3) modifies it while it tokenizes.
112 if ((tmpenv = strdup(env)) == NULL)
115 envvar = strtok_r(tmpenv, ",", &envsave);
116 while (envvar != NULL) {
117 if ((*callback)(envlist, envvar) != 0) {
121 envvar = strtok_r(NULL, ",", &envsave);
129 * Sets environment value to envlist in similar manner
132 * Returns 0 in success, errno otherwise.
135 envlist_setenv(envlist_t *envlist, const char *env)
137 struct envlist_entry *entry = NULL;
141 if ((envlist == NULL) || (env == NULL))
144 /* find out first equals sign in given env */
145 if ((eq_sign = strchr(env, '=')) == NULL)
147 envname_len = eq_sign - env + 1;
150 * If there already exists variable with given name
151 * we remove and release it before allocating a whole
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)
161 QLIST_REMOVE(entry, ev_link);
162 free((char *)entry->ev_var);
168 if ((entry = malloc(sizeof (*entry))) == NULL)
170 if ((entry->ev_var = strdup(env)) == NULL) {
174 QLIST_INSERT_HEAD(&envlist->el_entries, entry, ev_link);
180 * Removes given env value from envlist in similar manner
181 * than unsetenv(3). Returns 0 in success, errno otherwise.
184 envlist_unsetenv(envlist_t *envlist, const char *env)
186 struct envlist_entry *entry;
189 if ((envlist == NULL) || (env == NULL))
192 /* env is not allowed to contain '=' */
193 if (strchr(env, '=') != NULL)
197 * Find out the requested entry and remove
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)
207 QLIST_REMOVE(entry, ev_link);
208 free((char *)entry->ev_var);
217 * Returns given envlist as array of strings (in same form that
218 * global variable environ is). Caller must free returned memory
219 * by calling free(3) for each element and for the array. Returned
220 * array and given envlist are not related (no common references).
222 * If caller provides count pointer, number of items in array is
223 * stored there. In case of error, NULL is returned and no memory
227 envlist_to_environ(const envlist_t *envlist, size_t *count)
229 struct envlist_entry *entry;
232 penv = env = malloc((envlist->el_count + 1) * sizeof (char *));
236 for (entry = envlist->el_entries.lh_first; entry != NULL;
237 entry = entry->ev_link.le_next) {
238 *(penv++) = strdup(entry->ev_var);
240 *penv = NULL; /* NULL terminate the list */
243 *count = envlist->el_count;