11 struct envlist_entry
{
12 const char *ev_var
; /* actual env value */
13 LIST_ENTRY(envlist_entry
) ev_link
;
17 LIST_HEAD(, envlist_entry
) el_entries
; /* actual entries */
18 size_t el_count
; /* number of entries */
21 static int envlist_parse(envlist_t
*envlist
,
22 const char *env
, int (*)(envlist_t
*, const char *));
25 * Allocates new envlist and returns pointer to that or
26 * NULL in case of error.
33 if ((envlist
= malloc(sizeof (*envlist
))) == NULL
)
36 LIST_INIT(&envlist
->el_entries
);
37 envlist
->el_count
= 0;
43 * Releases given envlist and its entries.
46 envlist_free(envlist_t
*envlist
)
48 struct envlist_entry
*entry
;
50 assert(envlist
!= NULL
);
52 while (envlist
->el_entries
.lh_first
!= NULL
) {
53 entry
= envlist
->el_entries
.lh_first
;
54 LIST_REMOVE(entry
, ev_link
);
56 free((char *)entry
->ev_var
);
63 * Parses comma separated list of set/modify environment
64 * variable entries and updates given enlist accordingly.
67 * envlist_parse(el, "HOME=foo,SHELL=/bin/sh");
69 * inserts/sets environment variables HOME and SHELL.
71 * Returns 0 on success, errno otherwise.
74 envlist_parse_set(envlist_t
*envlist
, const char *env
)
76 return (envlist_parse(envlist
, env
, &envlist_setenv
));
80 * Parses comma separated list of unset environment variable
81 * entries and removes given variables from given envlist.
83 * Returns 0 on success, errno otherwise.
86 envlist_parse_unset(envlist_t
*envlist
, const char *env
)
88 return (envlist_parse(envlist
, env
, &envlist_unsetenv
));
92 * Parses comma separated list of set, modify or unset entries
93 * and calls given callback for each entry.
95 * Returns 0 in case of success, errno otherwise.
98 envlist_parse(envlist_t
*envlist
, const char *env
,
99 int (*callback
)(envlist_t
*, const char *))
101 char *tmpenv
, *envvar
;
102 char *envsave
= NULL
;
104 assert(callback
!= NULL
);
106 if ((envlist
== NULL
) || (env
== NULL
))
110 * We need to make temporary copy of the env string
111 * as strtok_r(3) modifies it while it tokenizes.
113 if ((tmpenv
= strdup(env
)) == NULL
)
116 envvar
= strtok_r(tmpenv
, ",", &envsave
);
117 while (envvar
!= NULL
) {
118 if ((*callback
)(envlist
, envvar
) != 0) {
122 envvar
= strtok_r(NULL
, ",", &envsave
);
130 * Sets environment value to envlist in similar manner
133 * Returns 0 in success, errno otherwise.
136 envlist_setenv(envlist_t
*envlist
, const char *env
)
138 struct envlist_entry
*entry
= NULL
;
142 if ((envlist
== NULL
) || (env
== NULL
))
145 /* find out first equals sign in given env */
146 if ((eq_sign
= strchr(env
, '=')) == NULL
)
148 envname_len
= eq_sign
- env
+ 1;
151 * If there already exists variable with given name
152 * we remove and release it before allocating a whole
155 for (entry
= envlist
->el_entries
.lh_first
; entry
!= NULL
;
156 entry
= entry
->ev_link
.le_next
) {
157 if (strncmp(entry
->ev_var
, env
, envname_len
) == 0)
162 LIST_REMOVE(entry
, ev_link
);
163 free((char *)entry
->ev_var
);
169 if ((entry
= malloc(sizeof (*entry
))) == NULL
)
171 if ((entry
->ev_var
= strdup(env
)) == NULL
) {
175 LIST_INSERT_HEAD(&envlist
->el_entries
, entry
, ev_link
);
181 * Removes given env value from envlist in similar manner
182 * than unsetenv(3). Returns 0 in success, errno otherwise.
185 envlist_unsetenv(envlist_t
*envlist
, const char *env
)
187 struct envlist_entry
*entry
;
190 if ((envlist
== NULL
) || (env
== NULL
))
193 /* env is not allowed to contain '=' */
194 if (strchr(env
, '=') != NULL
)
198 * Find out the requested entry and remove
201 envname_len
= strlen(env
);
202 for (entry
= envlist
->el_entries
.lh_first
; entry
!= NULL
;
203 entry
= entry
->ev_link
.le_next
) {
204 if (strncmp(entry
->ev_var
, env
, envname_len
) == 0)
208 LIST_REMOVE(entry
, ev_link
);
209 free((char *)entry
->ev_var
);
218 * Returns given envlist as array of strings (in same form that
219 * global variable environ is). Caller must free returned memory
220 * by calling free(3) for each element and for the array. Returned
221 * array and given envlist are not related (no common references).
223 * If caller provides count pointer, number of items in array is
224 * stored there. In case of error, NULL is returned and no memory
228 envlist_to_environ(const envlist_t
*envlist
, size_t *count
)
230 struct envlist_entry
*entry
;
233 penv
= env
= malloc((envlist
->el_count
+ 1) * sizeof (char *));
237 for (entry
= envlist
->el_entries
.lh_first
; entry
!= NULL
;
238 entry
= entry
->ev_link
.le_next
) {
239 *(penv
++) = strdup(entry
->ev_var
);
241 *penv
= NULL
; /* NULL terminate the list */
244 *count
= envlist
->el_count
;