- char const *end_p = name += FILESYSTEM_PREFIX_LEN (name);
- char const *first, *p;
- char *base;
- int length;
-
- /* Make END_P point to the byte after the last non-slash character
- in NAME if one exists. */
- for (p = name; *p; p++)
- if (!ISSLASH (*p))
- end_p = p + 1;
-
- if (end_p == name)
- {
- first = end_p;
- }
- else
+ char const *base = last_component (name);
+ size_t length;
+
+ /* If there is no last component, then name is a file system root or the
+ empty string. */
+ if (! *base)
+ return xstrndup (name, base_len (name));
+
+ /* Collapse a sequence of trailing slashes into one. */
+ length = base_len (base);
+ if (ISSLASH (base[length]))
+ length++;
+
+ /* On systems with drive letters, "a/b:c" must return "./b:c" rather
+ than "b:c" to avoid confusion with a drive letter. On systems
+ with pure POSIX semantics, this is not an issue. */
+ if (FILE_SYSTEM_PREFIX_LEN (base))