From f52cce59febb9c4f5f6a7f3f6f645801ca5abf15 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 28 Aug 2024 11:23:31 +0200 Subject: [PATCH] env: add env_list_add_getenv() and env_list_add_getenvs() * Add env_list_add_getenv() to import a single variable from the environ[] array. The function may accept a default value if getenv() does not return anything. * Add env_list_add_getenvs() to import multiple variables from the environ[] array. Signed-off-by: Karel Zak --- include/env.h | 7 ++++++- lib/Makemodule.am | 2 +- lib/env.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/include/env.h b/include/env.h index ba0f670d3a..b75023efcc 100644 --- a/include/env.h +++ b/include/env.h @@ -15,13 +15,18 @@ extern void __sanitize_env(struct ul_env_list **org); 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 diff --git a/lib/Makemodule.am b/lib/Makemodule.am index d95721ee22..bf24b6bee8 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -211,7 +211,7 @@ test_timeutils_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_TIMEUTILS 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 diff --git a/lib/env.c b/lib/env.c index 02c1158678..dfea1e5fcc 100644 --- a/lib/env.c +++ b/lib/env.c @@ -19,6 +19,7 @@ #include #include "env.h" +#include "strv.h" #include "all-io.h" #ifndef HAVE_ENVIRON_DECL @@ -81,7 +82,6 @@ static struct ul_env_list *env_list_add( struct ul_env_list *ls0, return ls; } - /* * Saves the @str (with the name=value string) to the @ls and returns a pointer * to the new head of the list. @@ -120,6 +120,47 @@ struct ul_env_list *env_list_add_variable( 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. * -- 2.47.3