diff --git a/src/config/toml_helper.c b/src/config/toml_helper.c index fe0e39d69..8f90df54c 100644 --- a/src/config/toml_helper.c +++ b/src/config/toml_helper.c @@ -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) { @@ -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) diff --git a/src/config/toml_helper.h b/src/config/toml_helper.h index 4a972f447..c627195a2 100644 --- a/src/config/toml_helper.h +++ b/src/config/toml_helper.h @@ -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 diff --git a/src/config/toml_reader.c b/src/config/toml_reader.c index 73961d2cf..7649d63ee 100644 --- a/src/config/toml_reader.c +++ b/src/config/toml_reader.c @@ -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));