From: Michael Tremer Date: Sun, 5 Jan 2025 17:14:25 +0000 (+0000) Subject: env: Add a function to append values X-Git-Tag: 0.9.30~518 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=257cac6234ca334cd05749d3708aee801c807c63;p=people%2Fms%2Fpakfire.git env: Add a function to append values Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/env.c b/src/pakfire/env.c index d519bc9c8..9ea23019c 100644 --- a/src/pakfire/env.c +++ b/src/pakfire/env.c @@ -161,6 +161,31 @@ int pakfire_env_set(struct pakfire_env* env, const char* key, const char* format return 0; } +// Appends something to an environment variable, separated by : +int pakfire_env_append(struct pakfire_env* env, const char* key, const char* format, ...) { + const char* old_value = NULL; + char value[PATH_MAX]; + va_list args; + int r; + + // Format the value + va_start(args, format); + r = pakfire_string_vformat(value, format, args); + va_end(args); + if (r < 0) + return r; + + // Fetch the old value + old_value = pakfire_env_get(env, key); + + // If there was no previous value, we will just set the new value + if (!old_value) + return pakfire_env_set(env, key, "%s", value); + + // Otherwise we will append it separated by : + return pakfire_env_set(env, key, "%s:%s", old_value, value); +} + // Imports an environment int pakfire_env_import(struct pakfire_env* env, const char** e) { char* key = NULL; diff --git a/src/pakfire/env.h b/src/pakfire/env.h index ff720fe99..3fb911639 100644 --- a/src/pakfire/env.h +++ b/src/pakfire/env.h @@ -35,6 +35,8 @@ char** pakfire_env_get_envp(struct pakfire_env* env); const char* pakfire_env_get(struct pakfire_env* env, const char* key); int pakfire_env_set(struct pakfire_env* env, const char* key, const char* format, ...) __attribute__((format(printf, 3, 4))); +int pakfire_env_append(struct pakfire_env* env, const char* key, const char* format, ...) + __attribute__((format(printf, 3, 4))); int pakfire_env_import(struct pakfire_env* env, const char** e);