X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Fpaths.c;h=db0b68275a8e35d8de137204721ad4b05d343fde;hb=72a2079aa02bcb86051dc527cc1c21fc77bc3695;hp=675b9f2752a4e21746febd9a848821bcc37f875b;hpb=927b1829e714d177de9d38c2da7fcdc13be44ed0;p=wimlib diff --git a/src/paths.c b/src/paths.c index 675b9f27..db0b6827 100644 --- a/src/paths.c +++ b/src/paths.c @@ -74,13 +74,34 @@ path_stream_name(const tchar *path) return stream_name + 1; } -/* Duplicate a path; return empty string for NULL input. */ -tchar * -canonicalize_fs_path(const tchar *fs_path) +/* Collapse and translate path separators, and strip trailing slashes. Doesn't + * add or delete a leading slash. + * + * @in may alias @out. + */ +void +do_canonicalize_path(const tchar *in, tchar *out) { - if (fs_path == NULL) - fs_path = T(""); - return TSTRDUP(fs_path); + tchar *orig_out = out; + + while (*in) { + if (is_any_path_separator(*in)) { + /* Collapse multiple path separators into one */ + *out++ = WIM_PATH_SEPARATOR; + do { + in++; + } while (is_any_path_separator(*in)); + } else { + /* Copy non-path-separator character */ + *out++ = *in++; + } + } + + /* Remove trailing slash if existent */ + if (out - orig_out > 1 && *(out - 1) == WIM_PATH_SEPARATOR) + --out; + + *out = T('\0'); } /* @@ -126,24 +147,7 @@ canonicalize_wim_path(const tchar *wim_path) if (!is_any_path_separator(*in)) *out++ = WIM_PATH_SEPARATOR; - while (*in) { - if (is_any_path_separator(*in)) { - /* Collapse multiple path separators into one */ - *out++ = WIM_PATH_SEPARATOR; - do { - in++; - } while (is_any_path_separator(*in)); - } else { - /* Copy non-path-separator character */ - *out++ = *in++; - } - } - - /* Remove trailing slash if existent */ - if (*(out - 1) == WIM_PATH_SEPARATOR && (out - 1) != result) - --out; - - *out = T('\0'); + do_canonicalize_path(in, out); return result; }