2 * paths.c - Path manipulation routines
6 * Copyright (C) 2012, 2013 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib/paths.h"
29 #include "wimlib/util.h"
33 /* Like the basename() function, but does not modify @path; it just returns a
34 * pointer to it. This assumes the path separator is the
35 * OS_PREFERRED_PATH_SEPARATOR. */
37 path_basename(const tchar *path)
39 return path_basename_with_len(path, tstrlen(path));
42 /* Like path_basename(), but take an explicit string length. */
44 path_basename_with_len(const tchar *path, size_t len)
46 const tchar *p = &path[len] - 1;
48 /* Trailing slashes. */
52 if (*p != OS_PREFERRED_PATH_SEPARATOR)
57 while ((p != path - 1) && *p != OS_PREFERRED_PATH_SEPARATOR)
65 * Returns a pointer to the part of @path following the first colon in the last
66 * path component, or NULL if the last path component does not contain a colon.
69 path_stream_name(const tchar *path)
71 const tchar *base = path_basename(path);
72 const tchar *stream_name = tstrchr(base, T(':'));
76 return stream_name + 1;
80 /* Duplicate a path; return empty string for NULL input. */
82 canonicalize_fs_path(const tchar *fs_path)
86 return TSTRDUP(fs_path);
90 * canonicalize_wim_path - Given a user-provided path to a file within a WIM
91 * image, translate it into a "canonical" path.
93 * To do this, we translate all supported path separators
94 * (is_any_path_separator()) into the WIM_PATH_SEPARATOR, and strip any leading
95 * and trailing slashes. The returned string is allocated. Note that there
96 * still may be consecutive path separators within the string. Furthermore, the
97 * string may be empty, which indicates the root dentry of the WIM image.
100 canonicalize_wim_path(const tchar *wim_path)
103 tchar *canonical_path;
105 if (wim_path == NULL) {
108 while (is_any_path_separator(*wim_path))
111 canonical_path = TSTRDUP(wim_path);
112 if (canonical_path) {
113 for (p = canonical_path; *p; p++)
114 if (is_any_path_separator(*p))
115 *p = WIM_PATH_SEPARATOR;
116 for (p = tstrchr(canonical_path, T('\0')) - 1;
117 p >= canonical_path && *p == WIM_PATH_SEPARATOR;
123 return canonical_path;