]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
env: add env_list_add_getenv() and env_list_add_getenvs()
authorKarel Zak <kzak@redhat.com>
Wed, 28 Aug 2024 09:23:31 +0000 (11:23 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 4 Sep 2024 11:39:03 +0000 (13:39 +0200)
* 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 <kzak@redhat.com>
include/env.h
lib/Makemodule.am
lib/env.c

index ba0f670d3a828ce2a05ab36f0f9e76e6bd0e6c6d..b75023efcc9bd0556f901af096709b691597a54c 100644 (file)
@@ -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
index d95721ee2236e33002f182bc48568280e68835d1..bf24b6bee86e0cf1ac5ef33a7b83ea84839b0542 100644 (file)
@@ -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
index 02c115867851398d0d517d2965314fcce0f1e09b..dfea1e5fcc697b306c08e0aa3b7ec58623830ed6 100644 (file)
--- a/lib/env.c
+++ b/lib/env.c
@@ -19,6 +19,7 @@
 #include <sys/types.h>
 
 #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.
  *