From 7b3eb51ec3ea3f96dc3d794e799f0d61c3e1d7be Mon Sep 17 00:00:00 2001 From: gramanas Date: Wed, 14 Nov 2018 16:42:40 +0200 Subject: XDG_CONFIG_HOME support --- src/confparser.c | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'src/confparser.c') diff --git a/src/confparser.c b/src/confparser.c index 019cf14..046bee7 100644 --- a/src/confparser.c +++ b/src/confparser.c @@ -14,7 +14,7 @@ ERRLOG(configfile); -const char * const CONFIG_NAME = "/ckrc"; +static const char * const CONFIG_NAME = "/ckrc"; void initialize_conf(Conf *c) { #define X(var, str, name) \ @@ -53,6 +53,46 @@ void make_config_name(char * ret, const char *confPath) { strcpy(ret, tmp); } +int find_config(UserOpt *opt) { + /* If env CK_CONFIG is set */ + char *config_home = getenv("CK_CONFIG"); + if (config_home) { + if (util_is_dir(config_home)) { + opt->confDir = strdup(config_home); + LOG("Using $CK_CONFIG: %s", config_home); + return 0; + } + } + LOG("$CK_CONFIG not avaliable.") + + /* If XDG_CONFIG_HOME exists*/ + config_home = getenv("XDG_CONFIG_HOME"); + if (config_home) { + if (util_is_dir(config_home)) { + char defaultConf[STR_S] = "ck"; + opt->confDir = malloc(strlen(config_home) + 1 /* '/' */ + strlen(defaultConf) + 1); + str_join_dirname_with_basename(opt->confDir, config_home, defaultConf); + LOG("Using $XDG_CONFIG_HOME: %s", config_home); + return 0; + } + } + LOG("$XDG_CONFIG_HOME not avaliable.") + + /* fallback to HOME/.ck */ + config_home = getenv("HOME"); + if (config_home) { + if (util_is_dir(config_home)) { + char defaultConf[STR_S] = ".ck"; + opt->confDir = malloc(strlen(config_home) + 1 /* '/' */ + strlen(defaultConf) + 1); + str_join_dirname_with_basename(opt->confDir, config_home, defaultConf); + LOG("Using $HOME: %s", config_home); + return 0; + } + } + LOG("$XDG_CONFIG_HOME not avaliable."); + return -1; +} + int config_file_parse(Conf *conf, UserOpt *opt) { LOG("Using '%s' for ck configuration directory", opt->confDir); FILE *confPtr; -- cgit v1.2.3