feat(vars): get_var_or_env
This commit is contained in:
parent
cf5da6f231
commit
0c9c5dc1f8
2 changed files with 15 additions and 1 deletions
|
|
@ -2,7 +2,7 @@
|
|||
#include "vars.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue