From 15ba3bc645c7620cdbaf261f8ba4634cd2a932a0 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Tue, 15 Feb 2022 10:26:25 -0500 Subject: Strip trailing separators --- lib/cleanpath.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/cleanpath.c b/lib/cleanpath.c index 138251f..5f7db0e 100644 --- a/lib/cleanpath.c +++ b/lib/cleanpath.c @@ -109,22 +109,24 @@ char *cleanpath_read(struct CleanPath *path) { goto cleanpath_read__failure; } - for (part_count = 0; part_count < CLEANPATH_PART_MAX && path->part[part_count] != NULL; part_count++); + for (part_count = 0; path->part[part_count] != NULL; part_count++) { + if (!strlen(path->part[part_count])) + continue; + } - for (size_t i = 0; i < CLEANPATH_PART_MAX && path->part[i] != NULL; i++) { + for (size_t i = 0; i < part_count; i++) { // Ignore filtered paths - if (*path->part[i] == '\0') { + if (!strlen(path->part[i])) { continue; } strcat(result, path->part[i]); - if (part_count > 1 && i == 0) { + if (i == part_count - 1) continue; - } strcat(result, path->sep); } result_len = strlen(result); - if (result_len) { + if (result_len && !strcmp(&result[result_len - 1], path->sep)) { result[result_len - 1] = '\0'; } -- cgit