diff --git a/src/common.c b/src/common.c index 1a6cd2d..63542e5 100644 --- a/src/common.c +++ b/src/common.c @@ -5,6 +5,7 @@ #include #include #include +#include const char *name = "pademelon"; const char *sysconf = "/etc/%s/%s"; @@ -54,6 +55,32 @@ int execute(const char *command) { return 1; } +void init_user_data_path(void) { + /* does not guarantee exitence - only best effort */ + char *xdg_data, *home; + + /* get data dir */ + if (!getenv("HOME")) + die("Unable to read $HOME variable"); + if ((xdg_data = getenv("XDG_DATA_HOME"))) { + char path[strlen(xdg_data) + strlen(name) + 2]; + if(sprintf(path, "%s/%s", xdg_data, name) < 0) + die("Unable to configure user data dir"); + fprintf(stderr, "Creating data dir: %s\n", path); + mkdir(path, S_IRWXU); + } else { + if (!(home = getenv("HOME"))) + die("Unable to read $HOME variable"); + char path[strlen(def_userdata) + strlen(home) + strlen(name) + 2]; + if(sprintf(path, def_userdata, home) < 0) + die("Unable to configure user data dir"); + strcat(path, "/"); + strcat(path, name); + fprintf(stderr, "Creating data dir: %s\n", path); + mkdir(path, S_IRWXU); + } +} + int str_to_int(const char *str, int *integer) { char *endptr; long ltemp; @@ -133,23 +160,23 @@ char *user_config_path(char *file) { } char *user_data_path(char *file) { - char *path, *file_cpy, *xdg_config; + char *path, *file_cpy, *xdg_data; file_cpy = file ? file : ""; - /* get config dir */ + /* get data dir */ if (!getenv("HOME")) die("Unable to read $HOME variable"); - xdg_config = getenv("XDG_DATA_HOME"); - char home_config[strlen(def_userdata) + strlen(getenv("HOME")) + 1]; - if(sprintf(home_config, def_userdata, getenv("HOME")) < 0) + xdg_data = getenv("XDG_DATA_HOME"); + char home_data[strlen(def_userdata) + strlen(getenv("HOME")) + 1]; + if(sprintf(home_data, def_userdata, getenv("HOME")) < 0) die("Unable to configure fallback user data dir"); /* allocate space for the string; must be freed by user */ - path = malloc(strlen(userdata) + strlen(xdg_config ? xdg_config : home_config) + strlen(name) + strlen(file_cpy) + 1); + path = malloc(strlen(userdata) + strlen(xdg_data ? xdg_data : home_data) + strlen(name) + strlen(file_cpy) + 1); if (!path) die("Unable to allocate memory for the user data dir"); /* configure data dir according to userdata variable, program name and file_cpy */ - if (sprintf(path, userdata, xdg_config ? xdg_config : home_config, name, file_cpy) < 0) + if (sprintf(path, userdata, xdg_data ? xdg_data : home_data, name, file_cpy) < 0) die("Unable to configure the user data dir"); return path; } diff --git a/src/common.h b/src/common.h index 2013f11..352080a 100644 --- a/src/common.h +++ b/src/common.h @@ -26,6 +26,8 @@ void die(const char *msg); /* returns -1 on error, return code else */ int execute(const char *command); +void init_user_data_path(void); + int str_to_int(const char *str, int *integer); /* diff --git a/src/tools.c b/src/tools.c index 718371c..d37922e 100644 --- a/src/tools.c +++ b/src/tools.c @@ -220,6 +220,8 @@ int tl_save_display_conf(void) { if (!path) return EXIT_FAILURE; + init_user_data_path(); + char temp[strlen(UNXRANDR_CMD) + strlen(" > ") + strlen(path) + 1]; strcpy(temp, UNXRANDR_CMD); strcat(temp, " > ");