cpu-plug-test: Don't pass integers as strings to device_add
[qemu.git] / util / envlist.c
blob1eeb7fca87c13806c0bfdf96fb41930686a93847
1 #include "qemu/osdep.h"
2 #include "qemu-common.h"
3 #include "qemu/queue.h"
4 #include "qemu/envlist.h"
6 struct envlist_entry {
7 const char *ev_var; /* actual env value */
8 QLIST_ENTRY(envlist_entry) ev_link;
9 };
11 struct envlist {
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 it.
22 envlist_t *
23 envlist_create(void)
25 envlist_t *envlist;
27 envlist = g_malloc(sizeof(*envlist));
29 QLIST_INIT(&envlist->el_entries);
30 envlist->el_count = 0;
32 return (envlist);
36 * Releases given envlist and its entries.
38 void
39 envlist_free(envlist_t *envlist)
41 struct envlist_entry *entry;
43 assert(envlist != NULL);
45 while (envlist->el_entries.lh_first != NULL) {
46 entry = envlist->el_entries.lh_first;
47 QLIST_REMOVE(entry, ev_link);
49 g_free((char *)entry->ev_var);
50 g_free(entry);
52 g_free(envlist);
56 * Parses comma separated list of set/modify environment
57 * variable entries and updates given enlist accordingly.
59 * For example:
60 * envlist_parse(el, "HOME=foo,SHELL=/bin/sh");
62 * inserts/sets environment variables HOME and SHELL.
64 * Returns 0 on success, errno otherwise.
66 int
67 envlist_parse_set(envlist_t *envlist, const char *env)
69 return (envlist_parse(envlist, env, &envlist_setenv));
73 * Parses comma separated list of unset environment variable
74 * entries and removes given variables from given envlist.
76 * Returns 0 on success, errno otherwise.
78 int
79 envlist_parse_unset(envlist_t *envlist, const char *env)
81 return (envlist_parse(envlist, env, &envlist_unsetenv));
85 * Parses comma separated list of set, modify or unset entries
86 * and calls given callback for each entry.
88 * Returns 0 in case of success, errno otherwise.
90 static int
91 envlist_parse(envlist_t *envlist, const char *env,
92 int (*callback)(envlist_t *, const char *))
94 char *tmpenv, *envvar;
95 char *envsave = NULL;
96 int ret = 0;
97 assert(callback != NULL);
99 if ((envlist == NULL) || (env == NULL))
100 return (EINVAL);
102 tmpenv = g_strdup(env);
103 envsave = tmpenv;
105 do {
106 envvar = strchr(tmpenv, ',');
107 if (envvar != NULL) {
108 *envvar = '\0';
110 if ((*callback)(envlist, tmpenv) != 0) {
111 ret = errno;
112 break;
114 tmpenv = envvar + 1;
115 } while (envvar != NULL);
117 g_free(envsave);
118 return ret;
122 * Sets environment value to envlist in similar manner
123 * than putenv(3).
125 * Returns 0 in success, errno otherwise.
128 envlist_setenv(envlist_t *envlist, const char *env)
130 struct envlist_entry *entry = NULL;
131 const char *eq_sign;
132 size_t envname_len;
134 if ((envlist == NULL) || (env == NULL))
135 return (EINVAL);
137 /* find out first equals sign in given env */
138 if ((eq_sign = strchr(env, '=')) == NULL)
139 return (EINVAL);
140 envname_len = eq_sign - env + 1;
143 * If there already exists variable with given name
144 * we remove and release it before allocating a whole
145 * new entry.
147 for (entry = envlist->el_entries.lh_first; entry != NULL;
148 entry = entry->ev_link.le_next) {
149 if (strncmp(entry->ev_var, env, envname_len) == 0)
150 break;
153 if (entry != NULL) {
154 QLIST_REMOVE(entry, ev_link);
155 g_free((char *)entry->ev_var);
156 g_free(entry);
157 } else {
158 envlist->el_count++;
161 entry = g_malloc(sizeof(*entry));
162 entry->ev_var = g_strdup(env);
163 QLIST_INSERT_HEAD(&envlist->el_entries, entry, ev_link);
165 return (0);
169 * Removes given env value from envlist in similar manner
170 * than unsetenv(3). Returns 0 in success, errno otherwise.
173 envlist_unsetenv(envlist_t *envlist, const char *env)
175 struct envlist_entry *entry;
176 size_t envname_len;
178 if ((envlist == NULL) || (env == NULL))
179 return (EINVAL);
181 /* env is not allowed to contain '=' */
182 if (strchr(env, '=') != NULL)
183 return (EINVAL);
186 * Find out the requested entry and remove
187 * it from the list.
189 envname_len = strlen(env);
190 for (entry = envlist->el_entries.lh_first; entry != NULL;
191 entry = entry->ev_link.le_next) {
192 if (strncmp(entry->ev_var, env, envname_len) == 0)
193 break;
195 if (entry != NULL) {
196 QLIST_REMOVE(entry, ev_link);
197 g_free((char *)entry->ev_var);
198 g_free(entry);
200 envlist->el_count--;
202 return (0);
206 * Returns given envlist as array of strings (in same form that
207 * global variable environ is). Caller must free returned memory
208 * by calling g_free for each element and the array.
209 * Returned array and given envlist are not related (no common
210 * references).
212 * If caller provides count pointer, number of items in array is
213 * stored there.
215 char **
216 envlist_to_environ(const envlist_t *envlist, size_t *count)
218 struct envlist_entry *entry;
219 char **env, **penv;
221 penv = env = g_malloc((envlist->el_count + 1) * sizeof(char *));
223 for (entry = envlist->el_entries.lh_first; entry != NULL;
224 entry = entry->ev_link.le_next) {
225 *(penv++) = g_strdup(entry->ev_var);
227 *penv = NULL; /* NULL terminate the list */
229 if (count != NULL)
230 *count = envlist->el_count;
232 return (env);