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 it.
27 envlist
= g_malloc(sizeof(*envlist
));
29 QLIST_INIT(&envlist
->el_entries
);
30 envlist
->el_count
= 0;
36 * Releases given envlist and its entries.
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
);
56 * Parses comma separated list of set/modify environment
57 * variable entries and updates given enlist accordingly.
60 * envlist_parse(el, "HOME=foo,SHELL=/bin/sh");
62 * inserts/sets environment variables HOME and SHELL.
64 * Returns 0 on success, errno otherwise.
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.
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.
91 envlist_parse(envlist_t
*envlist
, const char *env
,
92 int (*callback
)(envlist_t
*, const char *))
94 char *tmpenv
, *envvar
;
97 assert(callback
!= NULL
);
99 if ((envlist
== NULL
) || (env
== NULL
))
102 tmpenv
= g_strdup(env
);
106 envvar
= strchr(tmpenv
, ',');
107 if (envvar
!= NULL
) {
110 if ((*callback
)(envlist
, tmpenv
) != 0) {
115 } while (envvar
!= NULL
);
122 * Sets environment value to envlist in similar manner
125 * Returns 0 in success, errno otherwise.
128 envlist_setenv(envlist_t
*envlist
, const char *env
)
130 struct envlist_entry
*entry
= NULL
;
134 if ((envlist
== NULL
) || (env
== NULL
))
137 /* find out first equals sign in given env */
138 if ((eq_sign
= strchr(env
, '=')) == NULL
)
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
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)
154 QLIST_REMOVE(entry
, ev_link
);
155 g_free((char *)entry
->ev_var
);
161 entry
= g_malloc(sizeof(*entry
));
162 entry
->ev_var
= g_strdup(env
);
163 QLIST_INSERT_HEAD(&envlist
->el_entries
, entry
, ev_link
);
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
;
178 if ((envlist
== NULL
) || (env
== NULL
))
181 /* env is not allowed to contain '=' */
182 if (strchr(env
, '=') != NULL
)
186 * Find out the requested entry and remove
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)
196 QLIST_REMOVE(entry
, ev_link
);
197 g_free((char *)entry
->ev_var
);
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
212 * If caller provides count pointer, number of items in array is
216 envlist_to_environ(const envlist_t
*envlist
, size_t *count
)
218 struct envlist_entry
*entry
;
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 */
230 *count
= envlist
->el_count
;