From 1f980897f6947dd17eaa0190b2c1a23fb700636b Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 17 Jul 2019 10:53:22 +0200 Subject: fstree: add support for spaces in filenames This commit adds a mechanism to fstree_from_file to support filenames with spaces in them by quoting the entire string. Quote marks can still be used inside file names by escaping them with a backslash. Back slashes (if that is your thing) can also be escaped. Signed-off-by: David Oberhollenzer --- lib/fstree/fstree_from_file.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/fstree/fstree_from_file.c b/lib/fstree/fstree_from_file.c index 13328d5..901e431 100644 --- a/lib/fstree/fstree_from_file.c +++ b/lib/fstree/fstree_from_file.c @@ -113,7 +113,7 @@ static int handle_line(fstree_t *fs, const char *filename, size_t line_num, char *line) { const char *extra = NULL, *msg = NULL; - char keyword[16], *path; + char keyword[16], *path, *ptr; unsigned int x; struct stat sb; size_t i; @@ -136,17 +136,35 @@ static int handle_line(fstree_t *fs, const char *filename, /* isolate path */ path = line + i; - for (; line[i] != '\0'; ++i) { - /* TODO: escape sequences to support spaces in path */ + if (*path == '"') { + ptr = path; + ++i; - if (isspace(line[i])) - break; - } + while (line[i] != '\0' && line[i] != '"') { + if (line[i] == '\\' && + (line[i + 1] == '"' || line[i + 1] == '\\')) { + *(ptr++) = line[i + 1]; + i += 2; + } else { + *(ptr++) = line[i++]; + } + } - if (!isspace(line[i])) - goto fail_ent; + if (line[i] != '"' || !isspace(line[i + 1])) + goto fail_ent; + + *ptr = '\0'; + ++i; + } else { + while (line[i] != '\0' && !isspace(line[i])) + ++i; + + if (!isspace(line[i])) + goto fail_ent; + + line[i++] = '\0'; + } - line[i++] = '\0'; while (isspace(line[i])) ++i; -- cgit v1.2.3