Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File::Basename basename() bug #8006

Closed
p5pRT opened this issue Jul 7, 2005 · 8 comments
Closed

File::Basename basename() bug #8006

p5pRT opened this issue Jul 7, 2005 · 8 comments

Comments

@p5pRT
Copy link

p5pRT commented Jul 7, 2005

Migrated from rt.perl.org#36477 (status was 'resolved')

Searchable as RT36477$

@p5pRT
Copy link
Author

p5pRT commented Jul 7, 2005

From jens.luedicke@gmail.com

There is a bug in basename() (Module File​::Basename)​:

According to the manpage​:

basename

  The basename() routine returns the first element of the list
produced by calling fileparse() with the same arguments, except that
it always quotes metacharacters in the given suffixes. It is provided
for programmer compatibility with the Unix shell command basename(1).

The bug​: basename() doesn't return '/' for parameter '/'

--
Jens Luedicke
web​: http​://perldude.de/

@p5pRT
Copy link
Author

p5pRT commented Jul 7, 2005

From @steve-m-hay

[jensl - Thu Jul 07 02​:54​:06 2005]​:

There is a bug in basename() (Module File​::Basename)​:

According to the manpage​:

basename

The basename\(\) routine returns the first element of the list

produced by calling fileparse() with the same arguments, except that
it always quotes metacharacters in the given suffixes. It is provided
for programmer compatibility with the Unix shell command basename(1).

The bug​: basename() doesn't return '/' for parameter '/'

I was about to reply that as of change 25090 this is no longer a bug
since basename() is no longer documented to return the same as the first
element of the list returned by fileparse() - it is now documented to
behave the same way as the Unix shell command basename(1), which is what
its intended purpose has always been.

However, using the Cygwin basename(1) on my Win32 system I find that​:

C​:\p5p\bleadperl>\cygwin\bin\basename /
/
C​:\p5p\bleadperl>\cygwin\bin\basename \
\

whereas (using the current bleadperl @​ change 25094)
File​::Basename​::basename() still doesn't return the same thing as the
shell command​:

C​:\p5p\bleadperl>.\perl -MFile​::Basename -e "print basename('/')"

C​:\p5p\bleadperl>.\perl -MFile​::Basename -e "print basename('\\')"

(blank output in both cases).

Is there genuinely still a bug in File​::Basename​::basename(), or is it
just that Cygwin's basename(1) isn't entirely representative of what
basename(1) typically does? I'll let someone with a more traditional
Unixy system available answer that one.

@p5pRT
Copy link
Author

p5pRT commented Jul 7, 2005

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Jul 7, 2005

From @schwern

On Thu, Jul 07, 2005 at 04​:17​:50PM -0000, Steve Hay via RT wrote​:

Is there genuinely still a bug in File​::Basename​::basename(), or is it
just that Cygwin's basename(1) isn't entirely representative of what
basename(1) typically does? I'll let someone with a more traditional
Unixy system available answer that one.

Its a bug. Patch attached.

And I found another bug, but I will keep you in suspense until my next
patch!

--
Michael G Schwern schwern@​pobox.com http​://www.pobox.com/~schwern
Just call me 'Moron Sugar'.
  http​://www.somethingpositive.net/sp05182002.shtml

@p5pRT
Copy link
Author

p5pRT commented Jul 7, 2005

From @schwern

fb.patch
--- lib/File/Basename.pm	2005/07/07 21:04:45	1.1
+++ lib/File/Basename.pm	2005/07/07 21:09:11
@@ -206,9 +206,13 @@
 
 
 sub basename {
-  my($name) = shift;
-  _strip_trailing_sep($name);
-  (fileparse($name, map("\Q$_\E",@_)))[0];
+  my($path) = shift;
+
+  _strip_trailing_sep($path);
+  my($basename, $dirname) = fileparse( $path, map("\Q$_\E",@_) );
+  $basename = $dirname unless length $basename;
+
+  return $basename;
 }
 
 
--- lib/File/Basename.t	2005/07/07 20:55:38	1.1
+++ lib/File/Basename.t	2005/07/07 21:14:20
@@ -5,7 +5,7 @@
     @INC = '../lib';
 }
 
-use Test::More tests => 57;
+use Test::More tests => 60;
 
 BEGIN { use_ok 'File::Basename' }
 
@@ -26,7 +26,6 @@
     is(basename('/arma/virumque.cano'), 'virumque.cano');
     is(dirname ('/arma/virumque.cano'), '/arma');
     is(dirname('arma/'), '.');
-    is(dirname('/'), '/');
 }
 
 
@@ -131,6 +130,18 @@
 }
 
 
+### rt.cpan.org 36477
+{
+    fileparse_set_fstype('Unix');
+    is(dirname('/'), '/');
+    is(basename('/'), '/');
+
+    fileparse_set_fstype('DOS');
+    is(dirname('\\'), '\\');
+    is(basename('\\'), '\\');
+}
+
+
 ### Test tainting
 {
     #   The empty tainted value, for tainting strings
@@ -150,6 +161,7 @@
         1;
     }
 
+    fileparse_set_fstype 'Unix';
     ok tainted(dirname($TAINT.'/perl/lib//'));
     ok all_tainted(fileparse($TAINT.'/dir/draft.book7','\.book\d+'));
 }

@p5pRT
Copy link
Author

p5pRT commented Jul 8, 2005

From @steve-m-hay

Michael G Schwern wrote​:

On Thu, Jul 07, 2005 at 04​:17​:50PM -0000, Steve Hay via RT wrote​:

Is there genuinely still a bug in File​::Basename​::basename(), or is it
just that Cygwin's basename(1) isn't entirely representative of what
basename(1) typically does? I'll let someone with a more traditional
Unixy system available answer that one.

Its a bug. Patch attached.

Thanks - applied as change 25096.


Radan Computational Ltd.

The information contained in this message and any files transmitted with it are confidential and intended for the addressee(s) only. If you have received this message in error or there are any problems, please notify the sender immediately. The unauthorized use, disclosure, copying or alteration of this message is strictly forbidden. Note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of Radan Computational Ltd. The recipient(s) of this message should check it and any attached files for viruses​: Radan Computational will accept no liability for any damage caused by any virus transmitted by this email.

@p5pRT
Copy link
Author

p5pRT commented Jul 8, 2005

From @steve-m-hay

Now fixed in bleadperl as noted above.

@p5pRT
Copy link
Author

p5pRT commented Jul 8, 2005

@steve-m-hay - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant