Skip to content
This repository has been archived by the owner on Aug 14, 2022. It is now read-only.

Commit

Permalink
Stop using wordexp to expand image paths
Browse files Browse the repository at this point in the history
Using wordexp behaves too much like a shell in that it breaks filenames
with spaces. Since there's no standard way of escaping them safely, just
drop it. This loses support for environment variables. I may implement
that again later, but it's a non-trivial project.

Fixes #37.
  • Loading branch information
vilhalmer committed Dec 19, 2020
1 parent b822c12 commit 6937fee
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@
#include "config.h"
#include "oguri.h"

static char * expand_tilde(const char * path) {
if (strchr(path, '~') != path) {
return strdup(path);
}

char * home = getenv("HOME");
if (home == NULL || strcmp(home, "") == 0) {
fprintf(stderr, "$HOME is not set, cannot expand ~\n");
return NULL;
}

int length = snprintf(NULL, 0, "%s/%s", home, path + 1);
char * expanded = calloc(length, sizeof(char));
sprintf(expanded, "%s/%s", home, path + 1);
return expanded;
}

static char * expand_config_path(const char * path) {
// TODO: Return perror messages to caller for nicer display
if (!getenv("XDG_CONFIG_HOME")) {
Expand All @@ -26,9 +43,8 @@ static char * expand_config_path(const char * path) {
}

wordexp_t p = {0};

errno = 0;
if (wordexp(path, &p, WRDE_UNDEF) != 0 || errno != 0) {
if (wordexp(path, &p, WRDE_UNDEF|WRDE_NOCMD) != 0 || errno != 0) {
perror("Shell expansion error");
wordfree(&p);
return NULL;
Expand Down Expand Up @@ -116,7 +132,7 @@ bool configure_output(
}

if (strcmp(property, "image") == 0) {
char * expanded = expand_config_path(value);
char * expanded = expand_tilde(value);
if (access(expanded, R_OK)) {
fprintf(stderr, "Unable to access image '%s'\n", value);
free(expanded);
Expand Down

0 comments on commit 6937fee

Please sign in to comment.