From 046edea0e26ff280ddaabb707a0913ded3cdac33 Mon Sep 17 00:00:00 2001 From: simon987 Date: Wed, 10 Jun 2020 19:45:36 -0400 Subject: [PATCH] Handle special characters in file paths --- src/util.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/util.c b/src/util.c index c402a41..f418164 100644 --- a/src/util.c +++ b/src/util.c @@ -42,16 +42,34 @@ char *abspath(const char *path) { return abs; } +void shell_escape(char *dst, const char *src) { + const char *ptr = src; + char *out = dst; + while ((*ptr)) { + char c = *ptr++; + + if (c == '&' || c == '\n' || c == '|' || c == ';' || c == '<' || + c == '>' || c == '(' || c == ')' || c == '{' || c == '}') { + *out++ = '\\'; + } + *out++ = c; + } + *out = 0; +} + char *expandpath(const char *path) { - char tmp[PATH_MAX * 2] = {0,}; + char tmp[PATH_MAX * 2]; + + shell_escape(tmp, path); wordexp_t w; - wordexp(path, &w, 0); + wordexp(tmp, &w, 0); if (w.we_wordv == NULL) { return NULL; } + *tmp = '\0'; for (int i = 0; i < w.we_wordc; i++) { strcat(tmp, w.we_wordv[i]); if (i != w.we_wordc - 1) {