mirror of
https://github.com/simon987/sist2.git
synced 2025-04-19 10:16:42 +00:00
Handle special characters in file paths
This commit is contained in:
parent
a011b7e97b
commit
046edea0e2
22
src/util.c
22
src/util.c
@ -42,16 +42,34 @@ char *abspath(const char *path) {
|
|||||||
return abs;
|
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 *expandpath(const char *path) {
|
||||||
char tmp[PATH_MAX * 2] = {0,};
|
char tmp[PATH_MAX * 2];
|
||||||
|
|
||||||
|
shell_escape(tmp, path);
|
||||||
|
|
||||||
wordexp_t w;
|
wordexp_t w;
|
||||||
wordexp(path, &w, 0);
|
wordexp(tmp, &w, 0);
|
||||||
|
|
||||||
if (w.we_wordv == NULL) {
|
if (w.we_wordv == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*tmp = '\0';
|
||||||
for (int i = 0; i < w.we_wordc; i++) {
|
for (int i = 0; i < w.we_wordc; i++) {
|
||||||
strcat(tmp, w.we_wordv[i]);
|
strcat(tmp, w.we_wordv[i]);
|
||||||
if (i != w.we_wordc - 1) {
|
if (i != w.we_wordc - 1) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user