From 4285e8859353c31b39fa9539a89e58dc3a020264 Mon Sep 17 00:00:00 2001 From: Lars Henriksen Date: Fri, 17 Aug 2018 21:49:39 +0200 Subject: Rewrite of io_init() The introduction of the "-C " option is an opportunity to review the initialization of data paths. It lead to a rewrite. Two "root" directories are used (data and configuration files); by default they are identical. The statically allocated path buffers are turned into dynamically allocated buffers. Missing files/directories now include hooks. Signed-off-by: Lars Henriksen Signed-off-by: Lukas Fleischer --- src/io.c | 95 ++++++++++++++++++++++------------------------------------------ 1 file changed, 33 insertions(+), 62 deletions(-) (limited to 'src/io.c') diff --git a/src/io.c b/src/io.c index 1adf382..0e7baea 100644 --- a/src/io.c +++ b/src/io.c @@ -143,64 +143,36 @@ unsigned io_fprintln(const char *fname, const char *fmt, ...) */ void io_init(const char *cfile, const char *datadir, const char *confdir) { - const char *home; - const char *conf_home; - - if (datadir != NULL) { - home = datadir; - if (confdir == NULL) - conf_home = home; - else - conf_home = confdir; - - snprintf(path_dir, BUFSIZ, "%s", home); - - snprintf(path_conf_dir, BUFSIZ, "%s", conf_home); - snprintf(path_conf, BUFSIZ, "%s/" CONF_PATH_NAME, conf_home); - snprintf(path_keys, BUFSIZ, "%s/" KEYS_PATH_NAME, conf_home); - snprintf(path_hooks, BUFSIZ, "%s/" HOOKS_DIR_NAME, conf_home); - - snprintf(path_todo, BUFSIZ, "%s/" TODO_PATH_NAME, home); - snprintf(path_cpid, BUFSIZ, "%s/" CPID_PATH_NAME, home); - snprintf(path_dpid, BUFSIZ, "%s/" DPID_PATH_NAME, home); - snprintf(path_notes, BUFSIZ, "%s/" NOTES_DIR_NAME, home); - snprintf(path_dmon_log, BUFSIZ, "%s/" DLOG_PATH_NAME, home); - } else { - home = getenv("HOME"); - if (home == NULL) { - home = "."; - } - if (confdir == NULL) - conf_home = home; - else - conf_home = confdir; - - snprintf(path_dir, BUFSIZ, "%s/" DIR_NAME, home); - - snprintf(path_conf_dir, BUFSIZ, "%s", conf_home); - snprintf(path_conf, BUFSIZ, "%s/" CONF_PATH, conf_home); - snprintf(path_keys, BUFSIZ, "%s/" KEYS_PATH, conf_home); - snprintf(path_hooks, BUFSIZ, "%s/" HOOKS_DIR, conf_home); + if (!datadir) { + if (!(datadir = getenv("HOME"))) + datadir = "."; + asprintf(&path_ddir, "%s%s", datadir, "/" DIR_NAME); + } else + asprintf(&path_ddir, "%s%s", datadir, "/"); - snprintf(path_todo, BUFSIZ, "%s/" TODO_PATH, home); - snprintf(path_cpid, BUFSIZ, "%s/" CPID_PATH, home); - snprintf(path_dpid, BUFSIZ, "%s/" DPID_PATH, home); - snprintf(path_notes, BUFSIZ, "%s/" NOTES_DIR, home); - snprintf(path_dmon_log, BUFSIZ, "%s/" DLOG_PATH, home); - } + if (!confdir) + asprintf(&path_cdir, "%s%s", path_ddir, "/"); + else + asprintf(&path_cdir, "%s%s", confdir, "/"); - if (cfile == NULL) { - if (datadir != NULL) { - snprintf(path_apts, BUFSIZ, "%s/" APTS_PATH_NAME, - home); - } else { - snprintf(path_apts, BUFSIZ, "%s/" APTS_PATH, home); - } - } else { - snprintf(path_apts, BUFSIZ, "%s", cfile); + /* Data files */ + if (cfile) { + path_apts = mem_strdup(cfile); EXIT_IF(!io_file_exists(path_apts), _("%s does not exist"), path_apts); + } else { + asprintf(&path_apts, "%s%s", path_ddir, APTS_PATH_NAME); } + asprintf(&path_todo, "%s%s", path_ddir, TODO_PATH_NAME); + asprintf(&path_cpid, "%s%s", path_ddir, CPID_PATH_NAME); + asprintf(&path_dpid, "%s%s", path_ddir, DPID_PATH_NAME); + asprintf(&path_notes, "%s%s", path_ddir, NOTES_DIR_NAME); + asprintf(&path_dmon_log, "%s%s", path_ddir, DLOG_PATH_NAME); + + /* Configuration files */ + asprintf(&path_conf, "%s%s", path_cdir, CONF_PATH_NAME); + asprintf(&path_keys, "%s%s", path_cdir, KEYS_PATH_NAME); + asprintf(&path_hooks, "%s%s", path_cdir, HOOKS_DIR_NAME); } void io_extract_data(char *dst_data, const char *org, int len) @@ -1146,24 +1118,23 @@ int io_check_file(const char *file) * Checks if data files exist. If not, create them. * The following structure has to be created: * - * $HOME/.calcurse/ - * | - * +--- notes/ - * |___ conf - * |___ keys - * |___ apts - * |___ todo + * (default for both: $HOME/.calcurse/) + * | | + * |__ apts |___ conf + * |__ todo |___ keys + * |__ notes/ |___ hooks/ */ int io_check_data_files(void) { int missing = 0; - missing += io_check_dir(path_dir) ? 0 : 1; - missing += io_check_dir(path_conf_dir) ? 0 : 1; + missing += io_check_dir(path_ddir) ? 0 : 1; missing += io_check_dir(path_notes) ? 0 : 1; missing += io_check_file(path_todo) ? 0 : 1; missing += io_check_file(path_apts) ? 0 : 1; + missing += io_check_dir(path_cdir) ? 0 : 1; missing += io_check_file(path_conf) ? 0 : 1; + missing += io_check_dir(path_hooks) ? 0 : 1; if (!io_check_file(path_keys)) { missing++; -- cgit v1.2.3