1 #include "qemu-common.h"
2 #include "qemu/queue.h"
3 #include "qemu/envlist.h"
6 const char *ev_var
; /* actual env value */
7 QLIST_ENTRY(envlist_entry
) ev_link
;
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.
27 if ((envlist
= malloc(sizeof (*envlist
))) == NULL
)
30 QLIST_INIT(&envlist
->el_entries
);
31 envlist
->el_count
= 0;
37 * Releases given envlist and its entries.
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
);
57 * Parses comma separated list of set/modify environment
58 * variable entries and updates given enlist accordingly.
61 * envlist_parse(el, "HOME=foo,SHELL=/bin/sh");
63 * inserts/sets environment variables HOME and SHELL.
65 * Returns 0 on success, errno otherwise.
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.
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.
92 envlist_parse(envlist_t
*envlist
, const char *env
,
93 int (*callback
)(envlist_t
*, const char *))
95 char *tmpenv
, *envvar
;
98 assert(callback
!= NULL
);
100 if ((envlist
== NULL
) || (env
== NULL
))
103 if ((tmpenv
= strdup(env
)) == NULL
)
108 envvar
= strchr(tmpenv
, ',');
109 if (envvar
!= NULL
) {
112 if ((*callback
)(envlist
, tmpenv
) != 0) {
117 } while (envvar
!= NULL
);
124 * Sets environment value to envlist in similar manner
127 * Returns 0 in success, errno otherwise.
130 envlist_setenv(envlist_t
*envlist
, const char *env
)
132 struct envlist_entry
*entry
= NULL
;
136 if ((envlist
== NULL
) || (env
== NULL
))
139 /* find out first equals sign in given env */
140 if ((eq_sign
= strchr(env
, '=')) == NULL
)
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
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)
156 QLIST_REMOVE(entry
, ev_link
);
157 free((char *)entry
->ev_var
);
163 if ((entry
= malloc(sizeof (*entry
))) == NULL
)
165 if ((entry
->ev_var
= strdup(env
)) == NULL
) {
169 QLIST_INSERT_HEAD(&envlist
->el_entries
, entry
, ev_link
);
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
;
184 if ((envlist
== NULL
) || (env
== NULL
))
187 /* env is not allowed to contain '=' */
188 if (strchr(env
, '=') != NULL
)
192 * Find out the requested entry and remove
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)
202 QLIST_REMOVE(entry
, ev_link
);
203 free((char *)entry
->ev_var
);
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
222 envlist_to_environ(const envlist_t
*envlist
, size_t *count
)
224 struct envlist_entry
*entry
;
227 penv
= env
= malloc((envlist
->el_count
+ 1) * sizeof (char *));
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 */
238 *count
= envlist
->el_count
;