]> git.ipfire.org Git - pakfire.git/commitdiff
env: Allow string formatting when setting a value
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 3 Jan 2025 08:15:46 +0000 (08:15 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 3 Jan 2025 08:15:46 +0000 (08:15 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/env.c
src/pakfire/env.h
src/pakfire/jail.c
src/pakfire/parser.c

index 9d1dcfd12183f2b85c92e63d7b135002ecb68555..d519bc9c816d0d51d98cdfb4a6c9f21adcc431fd 100644 (file)
@@ -124,7 +124,9 @@ const char* pakfire_env_get(struct pakfire_env* env, const char* key) {
 }
 
 // Sets an environment variable
-int pakfire_env_set(struct pakfire_env* env, const char* key, const char* value) {
+int pakfire_env_set(struct pakfire_env* env, const char* key, const char* format, ...) {
+       char value[PATH_MAX];
+       va_list args;
        int r;
 
        // Find the index where to write this value to
@@ -136,6 +138,13 @@ int pakfire_env_set(struct pakfire_env* env, const char* key, const char* value)
        if (i >= ENVIRON_SIZE)
                return -ENOBUFS;
 
+       // Format the value
+       va_start(args, format);
+       r = pakfire_string_vformat(value, format, args);
+       va_end(args);
+       if (r < 0)
+               return r;
+
        // Free any previous value
        if (env->env[i])
                free(env->env[i]);
@@ -169,7 +178,7 @@ int pakfire_env_import(struct pakfire_env* env, const char** e) {
                        continue;
 
                // Set value
-               r = pakfire_env_set(env, key, val);
+               r = pakfire_env_set(env, key, "%s", val);
 
                if (key)
                        free(key);
index 2be310ea9befb63de938b4c273e1603c52e16f6c..5dc361f978431c2283335181d1725bc87c64bfe9 100644 (file)
@@ -35,7 +35,8 @@ struct pakfire_env* pakfire_env_unref(struct pakfire_env* env);
 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* value);
+int pakfire_env_set(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);
 
index ede54b88610429d6cc9658acbceb2919eb3413ac..1941c889a4797b60179813e92cf2da5649ff1af2 100644 (file)
@@ -215,7 +215,7 @@ int pakfire_jail_create(struct pakfire_jail** jail, struct pakfire* pakfire) {
 
        // Set default environment
        for (const struct environ* e = ENV; e->key; e++) {
-               r = pakfire_env_set(j->env, e->key, e->val);
+               r = pakfire_env_set(j->env, e->key, "%s", e->val);
                if (r < 0)
                        goto ERROR;
        }
@@ -228,7 +228,7 @@ int pakfire_jail_create(struct pakfire_jail** jail, struct pakfire* pakfire) {
        }
 
        // Set container UUID
-       r = pakfire_env_set(j->env, "container_uuid", pakfire_jail_uuid(j));
+       r = pakfire_env_set(j->env, "container_uuid", "%s", pakfire_jail_uuid(j));
        if (r < 0)
                goto ERROR;
 
@@ -1738,7 +1738,7 @@ int pakfire_jail_shell(struct pakfire_jail* jail, struct pakfire_env* e) {
        // Copy TERM
        char* TERM = secure_getenv("TERM");
        if (TERM) {
-               r = pakfire_env_set(env, "TERM", TERM);
+               r = pakfire_env_set(env, "TERM", "%s", TERM);
                if (r < 0)
                        goto ERROR;
        }
@@ -1746,7 +1746,7 @@ int pakfire_jail_shell(struct pakfire_jail* jail, struct pakfire_env* e) {
        // Copy LANG
        char* LANG = secure_getenv("LANG");
        if (LANG) {
-               r = pakfire_env_set(env, "LANG", LANG);
+               r = pakfire_env_set(env, "LANG", "%s", LANG);
                if (r < 0)
                        goto ERROR;
        }
index 1b101b7fb1d3ba76da703021ead7a7ec4fedceb0..17cc8f4072198b6c42fe285505c456de0434979d 100644 (file)
@@ -972,7 +972,7 @@ int pakfire_parser_set_env(struct pakfire_parser* parser, struct pakfire_env* en
                                continue;
 
                        // Store the value
-                       r = pakfire_env_set(env, d->name, value);
+                       r = pakfire_env_set(env, d->name, "%s", value);
                        free(value);
                        if (r < 0)
                                return r;