basename(1) sez:
The suffix is not stripped if it is identical to the remaining characters 
in string.

Which our basename() does not do.  This patch makes it do.


-- 
Michael G Schwern     [EMAIL PROTECTED]     http://www.pobox.com/~schwern
Just call me 'Moron Sugar'.
        http://www.somethingpositive.net/sp05182002.shtml
--- lib/File/Basename.pm        2005/07/07 21:10:58     1.2
+++ lib/File/Basename.pm        2005/07/07 22:34:26
@@ -157,9 +157,9 @@
   }
       
 
-  my($tail, $suffix);
+  my $tail   = '';
+  my $suffix = '';
   if (@suffices) {
-    $tail = '';
     foreach $suffix (@suffices) {
       my $pat = ($igncase ? '(?i)' : '') . "($suffix)\$";
       if ($basename =~ s/$pat//s) {
@@ -170,7 +170,7 @@
   }
 
   # Ensure taint is propgated from the path to its pieces.
-  $tail .= $taint if defined $tail; # avoid warning if $tail == undef
+  $tail .= $taint;
   wantarray ? ($basename .= $taint, $dirpath .= $taint, $tail)
             : ($basename .= $taint);
 }
@@ -208,9 +208,24 @@
 sub basename {
   my($path) = shift;
 
+  # From BSD basename(1)
+  # The basename utility deletes any prefix ending with the last slash `/'
+  # character present in string (after first stripping trailing slashes)
   _strip_trailing_sep($path);
-  my($basename, $dirname) = fileparse( $path, map("\Q$_\E",@_) );
-  $basename = $dirname unless length $basename;
+
+  my($basename, $dirname, $suffix) = fileparse( $path, map("\Q$_\E",@_) );
+
+  # From BSD basename(1)
+  # The suffix is not stripped if it is identical to the remaining 
+  # characters in string.
+  if( length $suffix and !length $basename ) {
+      $basename = $suffix;
+  }
+  
+  # Ensure that basename '/' == '/'
+  if( !length $basename ) {
+      $basename = $dirname;
+  }
 
   return $basename;
 }
--- lib/File/Basename.t 2005/07/07 21:10:39     1.2
+++ lib/File/Basename.t 2005/07/07 22:37:03
@@ -5,7 +5,7 @@
     @INC = '../lib';
 }
 
-use Test::More tests => 60;
+use Test::More tests => 64;
 
 BEGIN { use_ok 'File::Basename' }
 
@@ -139,6 +139,17 @@
     fileparse_set_fstype('DOS');
     is(dirname('\\'), '\\');
     is(basename('\\'), '\\');
+}
+
+
+### basename(1) sez: "The suffix is not stripped if it is identical to the
+### remaining characters in string"
+{
+    fileparse_set_fstype('Unix');
+    is(basename('.foo'), '.foo');
+    is(basename('.foo', '.foo'),     '.foo');
+    is(basename('.foo.bar', '.foo'), '.foo.bar');
+    is(basename('.foo.bar', '.bar'), '.foo');
 }
 
 

Reply via email to