qemu-log: Rename the public-facing cpu_set_log function to qemu_set_log
[qemu/pbrook.git] / util / envlist.c
blobebc06cf0f3f1ad4a26e68d61064ebb581b39dc4d
1 #include "qemu-common.h"
2 #include "qemu/queue.h"
3 #include "qemu/envlist.h"
5 struct envlist_entry {
6 const char *ev_var; /* actual env value */
7 QLIST_ENTRY(envlist_entry) ev_link;
8 };
10 struct envlist {
11 QLIST_HEAD(, envlist_entry) el_entries; /* actual entries */
12 size_t el_count; /* number of entries */
15 static int envlist_parse(envlist_t *envlist,
16 const char *env, int (*)(envlist_t *, const char *));
19 * Allocates new envlist and returns pointer to that or
20 * NULL in case of error.
22 envlist_t *
23 envlist_create(void)
25 envlist_t *envlist;
27 if ((envlist = malloc(sizeof (*envlist))) == NULL)
28 return (NULL);
30 QLIST_INIT(&envlist->el_entries);
31 envlist->el_count = 0;
33 return (envlist);
37 * Releases given envlist and its entries.
39 void
40 envlist_free(envlist_t *envlist)
42 struct envlist_entry *entry;
44 assert(envlist != NULL);
46 while (envlist->el_entries.lh_first != NULL) {
47 entry = envlist->el_entries.lh_first;
48 QLIST_REMOVE(entry, ev_link);
50 free((char *)entry->ev_var);
51 free(entry);
53 free(envlist);
57 * Parses comma separated list of set/modify environment
58 * variable entries and updates given enlist accordingly.
60 * For example:
61 * envlist_parse(el, "HOME=foo,SHELL=/bin/sh");
63 * inserts/sets environment variables HOME and SHELL.
65 * Returns 0 on success, errno otherwise.
67 int
68 envlist_parse_set(envlist_t *envlist, const char *env)
70 return (envlist_parse(envlist, env, &envlist_setenv));
74 * Parses comma separated list of unset environment variable
75 * entries and removes given variables from given envlist.
77 * Returns 0 on success, errno otherwise.
79 int
80 envlist_parse_unset(envlist_t *envlist, const char *env)
82 return (envlist_parse(envlist, env, &envlist_unsetenv));
86 * Parses comma separated list of set, modify or unset entries
87 * and calls given callback for each entry.
89 * Returns 0 in case of success, errno otherwise.
91 static int
92 envlist_parse(envlist_t *envlist, const char *env,
93 int (*callback)(envlist_t *, const char *))
95 char *tmpenv, *envvar;
96 char *envsave = NULL;
98 assert(callback != NULL);
100 if ((envlist == NULL) || (env == NULL))
101 return (EINVAL);
104 * We need to make temporary copy of the env string
105 * as strtok_r(3) modifies it while it tokenizes.
107 if ((tmpenv = strdup(env)) == NULL)
108 return (errno);
110 envvar = strtok_r(tmpenv, ",", &envsave);
111 while (envvar != NULL) {
112 if ((*callback)(envlist, envvar) != 0) {
113 free(tmpenv);
114 return (errno);
116 envvar = strtok_r(NULL, ",", &envsave);
119 free(tmpenv);
120 return (0);
124 * Sets environment value to envlist in similar manner
125 * than putenv(3).
127 * Returns 0 in success, errno otherwise.
130 envlist_setenv(envlist_t *envlist, const char *env)
132 struct envlist_entry *entry = NULL;
133 const char *eq_sign;
134 size_t envname_len;
136 if ((envlist == NULL) || (env == NULL))
137 return (EINVAL);
139 /* find out first equals sign in given env */
140 if ((eq_sign = strchr(env, '=')) == NULL)
141 return (EINVAL);
142 envname_len = eq_sign - env + 1;
145 * If there already exists variable with given name
146 * we remove and release it before allocating a whole
147 * new entry.
149 for (entry = envlist->el_entries.lh_first; entry != NULL;
150 entry = entry->ev_link.le_next) {
151 if (strncmp(entry->ev_var, env, envname_len) == 0)
152 break;
155 if (entry != NULL) {
156 QLIST_REMOVE(entry, ev_link);
157 free((char *)entry->ev_var);
158 free(entry);
159 } else {
160 envlist->el_count++;
163 if ((entry = malloc(sizeof (*entry))) == NULL)
164 return (errno);
165 if ((entry->ev_var = strdup(env)) == NULL) {
166 free(entry);
167 return (errno);
169 QLIST_INSERT_HEAD(&envlist->el_entries, entry, ev_link);
171 return (0);
175 * Removes given env value from envlist in similar manner
176 * than unsetenv(3). Returns 0 in success, errno otherwise.
179 envlist_unsetenv(envlist_t *envlist, const char *env)
181 struct envlist_entry *entry;
182 size_t envname_len;
184 if ((envlist == NULL) || (env == NULL))
185 return (EINVAL);
187 /* env is not allowed to contain '=' */
188 if (strchr(env, '=') != NULL)
189 return (EINVAL);
192 * Find out the requested entry and remove
193 * it from the list.
195 envname_len = strlen(env);
196 for (entry = envlist->el_entries.lh_first; entry != NULL;
197 entry = entry->ev_link.le_next) {
198 if (strncmp(entry->ev_var, env, envname_len) == 0)
199 break;
201 if (entry != NULL) {
202 QLIST_REMOVE(entry, ev_link);
203 free((char *)entry->ev_var);
204 free(entry);
206 envlist->el_count--;
208 return (0);
212 * Returns given envlist as array of strings (in same form that
213 * global variable environ is). Caller must free returned memory
214 * by calling free(3) for each element and for the array. Returned
215 * array and given envlist are not related (no common references).
217 * If caller provides count pointer, number of items in array is
218 * stored there. In case of error, NULL is returned and no memory
219 * is allocated.
221 char **
222 envlist_to_environ(const envlist_t *envlist, size_t *count)
224 struct envlist_entry *entry;
225 char **env, **penv;
227 penv = env = malloc((envlist->el_count + 1) * sizeof (char *));
228 if (env == NULL)
229 return (NULL);
231 for (entry = envlist->el_entries.lh_first; entry != NULL;
232 entry = entry->ev_link.le_next) {
233 *(penv++) = strdup(entry->ev_var);
235 *penv = NULL; /* NULL terminate the list */
237 if (count != NULL)
238 *count = envlist->el_count;
240 return (env);