- tchar *p;
- tchar *canonical_path;
-
- if (wim_path == NULL) {
- wim_path = T("");
- } else {
- while (is_any_path_separator(*wim_path))
- wim_path++;
- }
- canonical_path = TSTRDUP(wim_path);
- if (canonical_path) {
- for (p = canonical_path; *p; p++)
- if (is_any_path_separator(*p))
- *p = WIM_PATH_SEPARATOR;
- for (p = tstrchr(canonical_path, T('\0')) - 1;
- p >= canonical_path && *p == WIM_PATH_SEPARATOR;
- p--)
- {
- *p = T('\0');
- }
- }
- return canonical_path;
+ const tchar *in;
+ tchar *out;
+ tchar *result;
+
+ in = wim_path;
+ if (!in)
+ in = T("");
+
+ result = MALLOC((1 + tstrlen(in) + 1) * sizeof(result[0]));
+ if (!result)
+ return NULL;
+
+ out = result;
+
+ /* Add leading slash if missing */
+ if (!is_any_path_separator(*in))
+ *out++ = WIM_PATH_SEPARATOR;
+
+ do_canonicalize_path(in, out);
+
+ return result;