extern struct ul_env_list *env_list_add_variable(struct ul_env_list *ls,
const char *name, const char *value);
+
+extern struct ul_env_list *env_list_add_getenv(struct ul_env_list *ls,
+ const char *name, const char *dflt);
+extern struct ul_env_list *env_list_add_getenvs(struct ul_env_list *ls,
+ const char *str);
+
extern int env_list_setenv(struct ul_env_list *ls, int overwrite);
extern void env_list_free(struct ul_env_list *ls);
extern struct ul_env_list *env_list_from_fd(int pid);
extern char *safe_getenv(const char *arg);
-
#ifndef XSETENV_EXIT_CODE
# define XSETENV_EXIT_CODE EXIT_FAILURE
#endif
test_pwdutils_SOURCES = lib/pwdutils.c
test_pwdutils_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM
-test_remove_env_SOURCES = lib/env.c
+test_remove_env_SOURCES = lib/env.c lib/strv.c lib/strutils.c
test_remove_env_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM
test_buffer_SOURCES = lib/buffer.c lib/mbsalign.c
#include <sys/types.h>
#include "env.h"
+#include "strv.h"
#include "all-io.h"
#ifndef HAVE_ENVIRON_DECL
return ls;
}
-
/*
* Saves the @str (with the name=value string) to the @ls and returns a pointer
* to the new head of the list.
return env_list_add(ls, name, strlen(name), value, value ? strlen(value) : 0);
}
+/*
+ * Calls getenv() and adds the result to the list.
+ */
+struct ul_env_list *env_list_add_getenv(struct ul_env_list *ls,
+ const char *name, const char *dflt)
+{
+ const char *val;
+
+ if (!name)
+ return ls;
+
+ val = getenv(name);
+ if (!val)
+ val= dflt;
+ if (val)
+ ls = env_list_add_variable(ls, name, val);
+ return ls;
+}
+
+/*
+ * Calls getenv() for each name (comma-separated) in @str and adds the results
+ * to the list.
+ */
+struct ul_env_list *env_list_add_getenvs(struct ul_env_list *ls, const char *str)
+{
+ char **all, **name;
+
+ if (!str)
+ return ls;
+
+ all = strv_split(str, ",");
+ if (!all)
+ return ls;
+
+ STRV_FOREACH(name, all)
+ ls = env_list_add_getenv(ls, *name, NULL);
+
+ strv_free(all);
+ return ls;
+}
+
/*
* Use env_list_from_fd() to read environment from @fd.
*