From 0c9c5dc1f8b2d89bff689cb2134de3082828e24b Mon Sep 17 00:00:00 2001 From: "william.valenduc" Date: Sat, 17 Jan 2026 23:58:15 +0000 Subject: [PATCH] feat(vars): get_var_or_env --- src/utils/vars/vars.c | 10 +++++++++- src/utils/vars/vars.h | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/utils/vars/vars.c b/src/utils/vars/vars.c index 70ab328..985e085 100644 --- a/src/utils/vars/vars.c +++ b/src/utils/vars/vars.c @@ -2,7 +2,7 @@ #include "vars.h" #include -#include +#include #include #include "../hash_map/hash_map.h" @@ -19,6 +19,14 @@ char *get_var(const struct hash_map *vars, const char *key) return (char *)hash_map_get(vars, key); } +char *get_var_or_env(const struct hash_map *vars, const char *key) +{ + char *value = (char *)hash_map_get(vars, key); + if (value == NULL) + value = getenv(key); + return value; +} + bool set_var(struct hash_map *vars, const char *key, const char *value) { if (key == NULL || value == NULL) diff --git a/src/utils/vars/vars.h b/src/utils/vars/vars.h index 97db704..de1c4c6 100644 --- a/src/utils/vars/vars.h +++ b/src/utils/vars/vars.h @@ -15,6 +15,12 @@ struct hash_map *vars_init(void); */ char *get_var(const struct hash_map *vars, const char *key); +/** + * Get the value of a variable, from the environment if not found in vars, + * NULL if not found in either. + */ +char *get_var_or_env(const struct hash_map *vars, const char *key); + /** * Set the value of a variable. Key and value ownership are transferred to * the hash_map and need to be on the heap. Returns true on success, false on