Skip to content

Commit

Permalink
Use case-insensitive matching for env variables
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Oct 27, 2023
1 parent c367d44 commit ed0a4ee
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
34 changes: 33 additions & 1 deletion src/config/toml_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,38 @@ void readTOMLvalue(struct conf_item *conf_item, const char* key, toml_table_t *t
}
}

// Retrieve the value of an environment variable in a case-insensitive way
// The first matching environment variable is returned
const char * __attribute__((pure)) getenv_case_insensitive(const char *key)
{
// Check if this is a valid key
if(key == NULL || strlen(key) == 0)
return NULL;

// Get the length of the key
const size_t keylen = strlen(key);

// Loop over all environment variables
for(char **env = environ; *env != NULL; ++env)
{
// Check if this is a valid environment variable
if(*env == NULL || strlen(*env) == 0)
continue;

// Check if this environment variable starts with the given key
if(strncasecmp(*env, key, keylen) == 0)
{
// Check if this is the environment variable we are
// looking for, i.e. it has a '=' after the key
if((*env)[keylen] == '=')
return &((*env)[keylen+1]);
}
}

// Return NULL if we did not find anything
return NULL;
}

#define FTLCONF_PREFIX "FTLCONF_"
bool readEnvValue(struct conf_item *conf_item, struct config *newconf)
{
Expand All @@ -738,7 +770,7 @@ bool readEnvValue(struct conf_item *conf_item, struct config *newconf)
envkey[i] = '_';

// First check if a environmental variable with the given key exists
const char *envvar = getenv(envkey);
const char *envvar = getenv_case_insensitive(envkey);

// Return early if this environment variable does not exist
if(envvar == NULL)
Expand Down
1 change: 1 addition & 0 deletions src/config/toml_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void print_comment(FILE *fp, const char *str, const char *intro, const unsigned
void print_toml_allowed_values(cJSON *allowed_values, FILE *fp, const unsigned int width, const unsigned int indent);
void writeTOMLvalue(FILE * fp, const int indent, const enum conf_type t, union conf_value *v);
void readTOMLvalue(struct conf_item *conf_item, const char* key, toml_table_t *toml, struct config *newconf);
const char *getenv_case_insensitive(const char *key) __attribute__((pure));
bool readEnvValue(struct conf_item *conf_item, struct config *newconf);

#endif //CONFIG_WRITER_H
2 changes: 1 addition & 1 deletion src/config/toml_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bool readFTLtoml(struct config *oldconf, struct config *newconf,

// Check if we are in Adam mode
// (only read the env vars)
const char *envvar = getenv("FTLCONF_ENV_ONLY");
const char *envvar = getenv_case_insensitive("FTLCONF_ENV_ONLY");
const bool adam_mode = (envvar != NULL &&
(strcmp(envvar, "true") == 0 ||
strcmp(envvar, "yes") == 0));
Expand Down

0 comments on commit ed0a4ee

Please sign in to comment.