The lib/Cwd.pm diet part of
[p5sagit/p5-mst-13.2.git] / lib / Cwd.pm
index 88afca2..db13aab 100644 (file)
@@ -1,5 +1,5 @@
 package Cwd;
-require 5.6.0;
+use 5.006;
 
 =head1 NAME
 
@@ -46,8 +46,6 @@ The cwd() is the most natural form for the current architecture. For
 most systems it is identical to `pwd` (but without the trailing line
 terminator).
 
-Unfortunately, cwd() tends to break if called under taint mode.
-
 =item fastcwd
 
     my $cwd = fastcwd();
@@ -75,7 +73,8 @@ The fastgetcwd() function is provided as a synonym for cwd().
 =head2 abs_path and friends
 
 These functions are exported only on request.  They each take a single
-argument and return the absolute pathname for it.
+argument and return the absolute pathname for it.  If no argument is
+given they'll use the current working directory.
 
 =over 4
 
@@ -95,7 +94,7 @@ A synonym for abs_path().
 
 =item fast_abs_path
 
-  my $abs_path = abs_path($file);
+  my $abs_path = fast_abs_path($file);
 
 A more dangerous, but potentially faster version of abs_path.
 
@@ -139,9 +138,7 @@ L<File::chdir>
 
 use strict;
 
-use Carp;
-
-our $VERSION = '2.06';
+our $VERSION = '2.08';
 
 use base qw/ Exporter /;
 our @EXPORT = qw(cwd getcwd fastcwd fastgetcwd);
@@ -168,6 +165,7 @@ if ($^O eq 'os2' && defined &sys_cwd && defined &sys_abspath) {
 
 eval {
     require XSLoader;
+    local $^W = 0;
     XSLoader::load('Cwd');
 };
 
@@ -182,11 +180,23 @@ foreach my $try (qw(/bin/pwd /usr/bin/pwd)) {
         last;
     }
 }
-$pwd_cmd ||= 'pwd';
+unless ($pwd_cmd) {
+    if (-x '/QOpenSys/bin/pwd') { # OS/400 PASE.
+        $pwd_cmd = '/QOpenSys/bin/pwd' ;
+    } else {
+        # Isn't this wrong?  _backtick_pwd() will fail if somenone has
+        # pwd in their path but it is not /bin/pwd or /usr/bin/pwd?
+        # See [perl #16774]. --jhi
+        $pwd_cmd = 'pwd';
+    }
+}
 
 # The 'natural and safe form' for UNIX (pwd may be setuid root)
 sub _backtick_pwd {
+    local @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)};
     my $cwd = `$pwd_cmd`;
+    # Belt-and-suspenders in case someone said "undef $/".
+    local $/ = "\n";
     # `pwd` may fail e.g. if the disk is full
     chomp($cwd) if defined $cwd;
     $cwd;
@@ -197,7 +207,9 @@ sub _backtick_pwd {
 
 unless(defined &cwd) {
     # The pwd command is not available in some chroot(2)'ed environments
-    if($^O eq 'MacOS' || grep { -x "$_/pwd" } split(':', $ENV{PATH})) {
+    if( $^O eq 'MacOS' || (defined $ENV{PATH} && 
+                           grep { -x "$_/pwd" } split(':', $ENV{PATH})) ) 
+    {
        *cwd = \&_backtick_pwd;
     }
     else {
@@ -256,9 +268,9 @@ sub fastcwd {
     $path = '/' . join('/', @path);
     if ($^O eq 'apollo') { $path = "/".$path; }
     # At this point $path may be tainted (if tainting) and chdir would fail.
-    # To be more useful we untaint it then check that we landed where we started.
-    $path = $1 if $path =~ /^(.*)\z/s; # untaint
-    CORE::chdir($path) || return undef;
+    # Untaint it then check that we landed where we started.
+    $path =~ /^(.*)\z/s                # untaint
+       && CORE::chdir($1) or return undef;
     ($cdev, $cino) = stat('.');
     die "Unstable directory path, current directory changed unexpectedly"
        if $cdev != $orig_cdev || $cino != $orig_cino;
@@ -346,7 +358,8 @@ sub _perl_abs_path
 
     unless (@cst = stat( $start ))
     {
-       carp "stat($start): $!";
+       require Carp;
+       Carp::carp ("stat($start): $!");
        return '';
     }
     $cwd = '';
@@ -355,14 +368,17 @@ sub _perl_abs_path
     {
        $dotdots .= '/..';
        @pst = @cst;
+       local *PARENT;
        unless (opendir(PARENT, $dotdots))
        {
-           carp "opendir($dotdots): $!";
+           require Carp;
+           Carp::carp ("opendir($dotdots): $!");
            return '';
        }
        unless (@cst = stat($dotdots))
        {
-           carp "stat($dotdots): $!";
+           require Carp;
+           Carp::carp ("stat($dotdots): $!");
            closedir(PARENT);
            return '';
        }
@@ -376,7 +392,8 @@ sub _perl_abs_path
            {
                unless (defined ($dir = readdir(PARENT)))
                {
-                   carp "readdir($dotdots): $!";
+                   require Carp;
+                   Carp::carp ("readdir($dotdots): $!");
                    closedir(PARENT);
                    return '';
                }
@@ -397,13 +414,26 @@ sub _perl_abs_path
 # used to the libc function.  --tchrist 27-Jan-00
 *realpath = \&abs_path;
 
+my $Curdir;
 sub fast_abs_path {
     my $cwd = getcwd();
     require File::Spec;
-    my $path = @_ ? shift : File::Spec->curdir;
-    CORE::chdir($path) || croak "Cannot chdir to $path:$!";
+    my $path = @_ ? shift : ($Curdir ||= File::Spec->curdir);
+
+    # Detaint else we'll explode in taint mode.  This is safe because
+    # we're not doing anything dangerous with it.
+    ($path) = $path =~ /(.*)/;
+    ($cwd)  = $cwd  =~ /(.*)/;
+
+    if (!CORE::chdir($path)) {
+       require Carp;
+       Carp::croak ("Cannot chdir to $path: $!");
+    }
     my $realpath = getcwd();
-    CORE::chdir($cwd)  || croak "Cannot chdir back to $cwd:$!";
+    if (! ((-d $cwd) && (CORE::chdir($cwd)))) {
+       require Carp;
+       Carp::croak ("Cannot chdir back to $cwd: $!");
+    }
     $realpath;
 }
 
@@ -429,7 +459,11 @@ sub _vms_cwd {
 sub _vms_abs_path {
     return $ENV{'DEFAULT'} unless @_;
     my $path = VMS::Filespec::pathify($_[0]);
-    croak("Invalid path name $_[0]") unless defined $path;
+    if (! defined $path)
+       {
+       require Carp;
+       Carp::croak("Invalid path name $_[0]")
+       }
     return VMS::Filespec::rmsexpand($path);
 }
 
@@ -463,12 +497,18 @@ sub _dos_cwd {
 }
 
 sub _qnx_cwd {
+       local $ENV{PATH} = '';
+       local $ENV{CDPATH} = '';
+       local $ENV{ENV} = '';
     $ENV{'PWD'} = `/usr/bin/fullpath -t`;
     chop $ENV{'PWD'};
     return $ENV{'PWD'};
 }
 
 sub _qnx_abs_path {
+       local $ENV{PATH} = '';
+       local $ENV{CDPATH} = '';
+       local $ENV{ENV} = '';
     my $path = @_ ? shift : '.';
     my $realpath=`/usr/bin/fullpath -t $path`;
     chop $realpath;
@@ -498,6 +538,7 @@ sub _epoc_cwd {
         *fastcwd       = \&_NT_cwd;
         *fastgetcwd    = \&_NT_cwd;
         *abs_path      = \&fast_abs_path;
+        *realpath   = \&fast_abs_path;
     }
     elsif ($^O eq 'os2') {
         # sys_cwd may keep the builtin command