- char const *slash;
- size_t length; /* Length of result, not including NUL. */
-
- slash = strrchr (path, '/');
- if (BACKSLASH_IS_PATH_SEPARATOR)
- {
- char const *b = strrchr (path, '\\');
- if (b && slash < b)
- slash = b;
- }
-
- /* If the last byte of PATH is a slash, decrement SLASH until it's
- pointing at the leftmost in a sequence of trailing slashes. */
- if (slash && slash[1] == 0)
- {
- while (path < slash && ISSLASH (slash[-1]))
- {
- --slash;
- }
-
- if (path < slash)
- {
- size_t len = slash - path;
- slash = memrchr (path, '/', len);
- if (BACKSLASH_IS_PATH_SEPARATOR)
- {
- char const *b = memrchr (path, '\\', len);
- if (b && slash < b)
- slash = b;
- }
- }
- }
-
- if (slash == 0)
- {
- /* File is in the current directory. */
-
- length = FILESYSTEM_PREFIX_LEN (path);
-
- if (length == 0)
- {
- path = ".";
- length = 1;
- }
- }
- else
- {
- /* Remove any trailing slashes from the result. If we have a
- canonicalized "d:/path", leave alone the root case "d:/". */
- char const *lim = path + FILESYSTEM_PREFIX_LEN (path);
-
- while (slash > lim && ISSLASH (*slash))
- --slash;
-
- length = slash - path + 1;
- }
-
- *result = path;
+ size_t prefix_length = FILE_SYSTEM_PREFIX_LEN (file);
+ size_t length;
+
+ /* Advance prefix_length beyond important leading slashes. */
+ prefix_length += (prefix_length != 0
+ ? (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
+ && ISSLASH (file[prefix_length]))
+ : (ISSLASH (file[0])
+ ? ((DOUBLE_SLASH_IS_DISTINCT_ROOT
+ && ISSLASH (file[1]) && ! ISSLASH (file[2])
+ ? 2 : 1))
+ : 0));
+
+ /* Strip the basename and any redundant slashes before it. */
+ for (length = last_component (file) - file;
+ prefix_length < length; length--)
+ if (! ISSLASH (file[length - 1]))
+ break;