From 676fc13cb24a6a983828595853e6de893c5f04f1 Mon Sep 17 00:00:00 2001 From: Christophe CURIS Date: Sat, 7 Jun 2014 21:21:48 +0200 Subject: [PATCH] wmix: be more verbose about the configuration file in use If the config file was set by user with '-f', it would be a good idea to report him if we're not able to load it. Took opportunity to report to user that the config file is loaded if he asked for verbose operations, so he can see if the default file loaded is the one he expected. Signed-off-by: Christophe CURIS --- wmix/config.c | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/wmix/config.c b/wmix/config.c index 9252657..42f9e6c 100644 --- a/wmix/config.c +++ b/wmix/config.c @@ -50,17 +50,8 @@ struct _Config config; */ void config_init(void) { - char *home; - memset(&config, 0, sizeof(config)); - /* we can theoretically live without a config file */ - home = getenv("HOME"); - if (home) { - config.file = calloc(1, strlen(home) + 9); - sprintf(config.file, "%s/.wmixrc", home); - } - config.mousewheel = 1; config.scrolltext = 1; config.wheel_button_up = 4; @@ -133,16 +124,38 @@ void parse_cli_options(int argc, char **argv) */ void config_read(void) { + const char *filename; + char buffer_fname[512]; FILE *fp; char buf[512]; char *ptr; - if (config.file == NULL) - return; + if (config.file != NULL) { + filename = config.file; + } else { + const char *home; - fp = fopen(config.file, "r"); - if (!fp) + home = getenv("HOME"); + if (home == NULL) { + fprintf(stderr, "wmix: warning, could not get $HOME, can't load configuration file\n"); + return; + } + snprintf(buffer_fname, sizeof(buffer_fname), "%s/.wmixrc", home); + filename = buffer_fname; + } + + fp = fopen(filename, "r"); + if (fp == NULL) { + if (config.file != NULL) { + /* The config file was explicitely specified by user, tell him there's a problem */ + fprintf(stderr, "wmix: error, could not load configuration file \"%s\"\n", filename); + exit(EXIT_FAILURE); + } + /* Otherwise, it is acceptable if the file does not exist */ return; + } + if (config.verbose) + printf("Using configuration file: %s\n", filename); while (fgets(buf, 512, fp)) { if ((ptr = strstr(buf, "mousewheel="))) {