guile-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Guile-commits] 01/02: 'basename' correctly handles "/" and "//".


From: Ludovic Courtès
Subject: [Guile-commits] 01/02: 'basename' correctly handles "/" and "//".
Date: Tue, 4 Jun 2019 15:25:03 -0400 (EDT)

civodul pushed a commit to branch stable-2.2
in repository guile.

commit 36ad1d24b3d2c174a64c445502a36f19605dbd65
Author: Ludovic Courtès <address@hidden>
Date:   Tue Jun 4 21:20:15 2019 +0200

    'basename' correctly handles "/" and "//".
    
    * libguile/filesys.c (scm_basename): Special-case "/" and "//".
    * test-suite/tests/filesys.test ("basename"): New test prefix.
---
 libguile/filesys.c            | 17 +++++++++++++----
 test-suite/tests/filesys.test |  8 +++++++-
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/libguile/filesys.c b/libguile/filesys.c
index 1a8dfa0..3cf474c 100644
--- a/libguile/filesys.c
+++ b/libguile/filesys.c
@@ -1602,11 +1602,20 @@ SCM_DEFINE (scm_basename, "basename", 1, 1, 0,
   c_filename = scm_to_utf8_string (filename);
   scm_dynwind_free (c_filename);
 
-  c_last_component = last_component (c_filename);
-  if (!c_last_component)
-    res = filename;
+  if (strcmp (c_filename, "/") == 0
+      || strcmp (c_filename, "//") == 0)
+    /* As per
+       
<http://pubs.opengroup.org/onlinepubs/9699919799/functions/basename.html>,
+       "/" and "//" are treated specially.  */
+    res = scm_from_utf8_string ("/");
   else
-    res = scm_from_utf8_string (c_last_component);
+    {
+      c_last_component = last_component (c_filename);
+      if (!c_last_component)
+        res = filename;
+      else
+        res = scm_from_utf8_string (c_last_component);
+    }
   scm_dynwind_end ();
 
   if (!SCM_UNBNDP (suffix) &&
diff --git a/test-suite/tests/filesys.test b/test-suite/tests/filesys.test
index fceb182..9ec9f61 100644
--- a/test-suite/tests/filesys.test
+++ b/test-suite/tests/filesys.test
@@ -1,6 +1,6 @@
 ;;;; filesys.test --- test file system functions -*- scheme -*-
 ;;;; 
-;;;; Copyright (C) 2004, 2006, 2013 Free Software Foundation, Inc.
+;;;; Copyright (C) 2004, 2006, 2013, 2019 Free Software Foundation, Inc.
 ;;;; 
 ;;;; This library is free software; you can redistribute it and/or
 ;;;; modify it under the terms of the GNU Lesser General Public
@@ -222,6 +222,12 @@
               (cons (join-thread child) out)))
           (throw 'unresolved)))))
 
+(with-test-prefix "basename"
+
+  (pass-if-equal "/" "/" (basename "/"))
+  (pass-if-equal "//" "/" (basename "//"))
+  (pass-if-equal "a/b/c" "c" (basename "a/b/c")))
+
 (delete-file (test-file))
 (when (file-exists? (test-symlink))
   (delete-file (test-symlink)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]