From 7892d8cc51c84b6a0330bc27517e5d3a9efbb67e Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Mon, 1 Apr 2019 01:52:34 -0400 Subject: [PATCH] Allow spaces in image file path This allows one or more spaces to be in the image file path --- main.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index 7048276..7af227c 100644 --- a/main.c +++ b/main.c @@ -374,6 +374,23 @@ static cairo_surface_t *select_image(struct swaylock_state *state, return default_image; } +static char *join_args(char **argv, int argc) { + assert(argc > 0); + int len = 0, i; + for (i = 0; i < argc; ++i) { + len += strlen(argv[i]) + 1; + } + char *res = malloc(len); + len = 0; + for (i = 0; i < argc; ++i) { + strcpy(res + len, argv[i]); + len += strlen(argv[i]); + res[len++] = ' '; + } + res[len - 1] = '\0'; + return res; +} + static void load_image(char *arg, struct swaylock_state *state) { // [[]:] struct swaylock_image *image = calloc(1, sizeof(struct swaylock_image)); @@ -407,11 +424,19 @@ static void load_image(char *arg, struct swaylock_state *state) { } } - // Bash doesn't replace the ~ with $HOME if the output name is supplied + // The shell will not expand ~ to the value of $HOME when an output name is + // given. Also, any image paths given in the config file need to have shell + // expansions performed wordexp_t p; + while (strstr(image->path, " ")) { + image->path = realloc(image->path, strlen(image->path) + 2); + char *ptr = strstr(image->path, " ") + 1; + memmove(ptr + 1, ptr, strlen(ptr) + 1); + *ptr = '\\'; + } if (wordexp(image->path, &p, 0) == 0) { free(image->path); - image->path = strdup(p.we_wordv[0]); + image->path = join_args(p.we_wordv, p.we_wordc); wordfree(&p); }