Skip to content

Commit

Permalink
Fixing data dir not getting created for tl_save_display_conf
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbor committed Mar 10, 2022
1 parent a005438 commit 82d112e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
41 changes: 34 additions & 7 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

const char *name = "pademelon";
const char *sysconf = "/etc/%s/%s";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/*
Expand Down
2 changes: 2 additions & 0 deletions src/tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, " > ");
Expand Down

0 comments on commit 82d112e

Please sign in to comment.