diff options
Diffstat (limited to 'src/config-ini.c')
-rw-r--r-- | src/config-ini.c | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/src/config-ini.c b/src/config-ini.c index 89d55d5..24027d4 100644 --- a/src/config-ini.c +++ b/src/config-ini.c @@ -107,11 +107,13 @@ open_config_file(const char *filepath) if (f == NULL && (env = getenv("XDG_CONFIG_DIRS")) != NULL && env[0] != '\0') { char *begin = env; - while (1) { - char *end = strchr(begin, ':'); + char *end; + int len; + for (;;) { + end = strchr(begin, ':'); if (end == NULL) end = strchr(begin, '\0'); - int len = end - begin; + len = (int)(end - begin); if (len > 0) { snprintf(cp, sizeof(cp), "%.*s/redshift/redshift.conf", len, begin); @@ -153,19 +155,20 @@ int config_ini_init(struct config_ini_state *state, const char *filepath) { struct config_ini_section *section = NULL; + char line[MAX_LINE_LENGTH]; + char *s; + FILE *f; + state->sections = NULL; - FILE *f = open_config_file(filepath); + f = open_config_file(filepath); if (f == NULL) { /* Only a serious error if a file was explicitly requested. */ if (filepath != NULL) return -1; return 0; } - char line[MAX_LINE_LENGTH]; - char *s; - - while (1) { + for (;;) { /* Handle the file input linewise. */ char *r = fgets(line, sizeof(line), f); if (r == NULL) break; @@ -215,8 +218,12 @@ config_ini_init(struct config_ini_state *state, const char *filepath) memcpy(section->name, name, end - name + 1); } else { + char *value, *end; + size_t value_len; + struct config_ini_setting *setting; + /* Split assignment at equals character. */ - char *end = strchr(s, '='); + end = strchr(s, '='); if (end == NULL || end == s) { fputs(_("Malformed assignment in config" " file.\n"), stderr); @@ -225,8 +232,8 @@ config_ini_init(struct config_ini_state *state, const char *filepath) return -1; } - *end = '\0'; - char *value = end + 1; + *end++ = '\0'; + value = end; if (section == NULL) { fputs(_("Assignment outside section in config" @@ -237,8 +244,7 @@ config_ini_init(struct config_ini_state *state, const char *filepath) } /* Create section. */ - struct config_ini_setting *setting = - malloc(sizeof(struct config_ini_setting)); + setting = malloc(sizeof(struct config_ini_setting)); if (setting == NULL) { fclose(f); config_ini_free(state); @@ -252,17 +258,17 @@ config_ini_init(struct config_ini_state *state, const char *filepath) section->settings = setting; /* Copy name of setting. */ - setting->name = malloc(end - s + 1); + setting->name = malloc(end - s); if (setting->name == NULL) { fclose(f); config_ini_free(state); return -1; } - memcpy(setting->name, s, end - s + 1); + memcpy(setting->name, s, end - s); /* Copy setting value. */ - size_t value_len = strlen(value) + 1; + value_len = strlen(value) + 1; setting->value = malloc(value_len); if (setting->value == NULL) { fclose(f); |