]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
env: Add a function to append values
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Jan 2025 17:14:25 +0000 (17:14 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Jan 2025 17:14:25 +0000 (17:14 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/env.c
src/pakfire/env.h

index d519bc9c816d0d51d98cdfb4a6c9f21adcc431fd..9ea23019ca54feeae6cfb3a0013bc2d53e52098b 100644 (file)
@@ -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;
index ff720fe99059c74ccfdd04786ddb1cc5e8f66b67..3fb911639b992bbe217ce677da4be28deb6c5068 100644 (file)
@@ -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);