]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix bug: (directory-file-name "///") returned "//"
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 8 Sep 2017 00:46:12 +0000 (17:46 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 8 Sep 2017 00:47:24 +0000 (17:47 -0700)
* src/fileio.c (directory_file_name): For "///" and longer,
return "/", not "//", as per POSIX.
* test/src/fileio-tests.el (fileio-tests--directory-file-name)
(fileio-tests--file-name-as-directory): New tests.

src/fileio.c
test/src/fileio-tests.el

index 0a52982291d1ccf1150a525621a4c5d33bb6f58a..9df3b1bedaeab9b7aab5628574800de0a71e1597 100644 (file)
@@ -566,15 +566,16 @@ is already present.  */)
 static ptrdiff_t
 directory_file_name (char *dst, char *src, ptrdiff_t srclen, bool multibyte)
 {
-  /* Process as Unix format: just remove any final slash.
-     But leave "/" and "//" unchanged.  */
-  while (srclen > 1
+  /* In Unix-like systems, just remove any final slashes.  However, if
+     they are all slashes, leave "/" and "//" alone, and treat "///"
+     and longer as if they were "/".  */
+  if (! (srclen == 2 && IS_DIRECTORY_SEP (src[0])))
+    while (srclen > 1
 #ifdef DOS_NT
-        && !IS_ANY_SEP (src[srclen - 2])
+          && !IS_ANY_SEP (src[srclen - 2])
 #endif
-        && IS_DIRECTORY_SEP (src[srclen - 1])
-        && ! (srclen == 2 && IS_DIRECTORY_SEP (src[0])))
-    srclen--;
+          && IS_DIRECTORY_SEP (src[srclen - 1]))
+      srclen--;
 
   memcpy (dst, src, srclen);
   dst[srclen] = 0;
index 5103d2f21e6102782a17a9f57bbe56bff5d4fed7..ac5d533e63b17654ae5c96e565873da665dfa6a9 100644 (file)
   "Check that any non-NULL ASCII character can appear in a symlink.
 Also check that an encoding error can appear in a symlink."
   (should (equal nil (fileio-tests--symlink-failure))))
+
+(ert-deftest fileio-tests--directory-file-name ()
+  (should (equal (directory-file-name "/") "/"))
+  (should (equal (directory-file-name "//") "//"))
+  (should (equal (directory-file-name "///") "/"))
+  (should (equal (directory-file-name "////") "/"))
+  (should (equal (directory-file-name "/abc") "/abc"))
+  (should (equal (directory-file-name "/abc/") "/abc"))
+  (should (equal (directory-file-name "/abc//") "/abc")))
+
+(ert-deftest fileio-tests--file-name-as-directory ()
+  (should (equal (file-name-as-directory "") "./"))
+  (should (equal (file-name-as-directory "/") "/"))
+  (should (equal (file-name-as-directory "//") "//"))
+  (should (equal (file-name-as-directory "///") "///"))
+  (should (equal (file-name-as-directory "////") "////"))
+  (should (equal (file-name-as-directory "/abc") "/abc/"))
+  (should (equal (file-name-as-directory "/abc/") "/abc/"))
+  (should (equal (file-name-as-directory "/abc//") "/abc//")))