Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use case-insensitive matching for env variables #1710

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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