This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
remove dead code
[perl5.git] / lib / Cwd.pm
index 6952411..df40649 100644 (file)
@@ -20,11 +20,21 @@ getcwd - get pathname of current working directory
     chdir "/tmp";
     print $ENV{'PWD'};
 
+    use Cwd 'abs_path';
+    print abs_path($ENV{'PWD'});
+
+    use Cwd 'fast_abs_path';
+    print fast_abs_path($ENV{'PWD'});
+
 =head1 DESCRIPTION
 
 The getcwd() function re-implements the getcwd(3) (or getwd(3)) functions
 in Perl.
 
+The abs_path() function takes a single argument and returns the
+absolute pathname for that argument. It uses the same algorithm as
+getcwd(). (actually getcwd() is abs_path("."))
+
 The fastcwd() function looks the same as getcwd(), but runs faster.
 It's also more dangerous because it might conceivably chdir() you out
 of a directory that it can't chdir() you back into.  If fastcwd
@@ -35,6 +45,9 @@ that it leaves you in the same directory that it started in. If it has
 changed it will C<die> with the message "Unstable directory path,
 current directory changed unexpectedly". That should never happen.
 
+The fast_abs_path() function looks the same as abs_path(), but runs faster.
+And like fastcwd() is more dangerous.
+
 The cwd() function looks the same as getcwd and fastgetcwd but is
 implemented using the most natural and safe form for the current
 architecture. For most systems it is identical to `pwd` (but without
@@ -54,7 +67,7 @@ kept up to date if all packages which use chdir import it from Cwd.
 
 use Carp;
 
-$VERSION = '2.00';
+$VERSION = '2.01';
 
 require Exporter;
 @ISA = qw(Exporter);
@@ -82,66 +95,9 @@ sub _backtick_pwd {
 
 sub getcwd
 {
-    my($dotdots, $cwd, @pst, @cst, $dir, @tst);
-
-    unless (@cst = stat('.'))
-    {
-       warn "stat(.): $!";
-       return '';
-    }
-    $cwd = '';
-    $dotdots = '';
-    do
-    {
-       $dotdots .= '/' if $dotdots;
-       $dotdots .= '..';
-       @pst = @cst;
-       unless (opendir(PARENT, $dotdots))
-       {
-           warn "opendir($dotdots): $!";
-           return '';
-       }
-       unless (@cst = stat($dotdots))
-       {
-           warn "stat($dotdots): $!";
-           closedir(PARENT);
-           return '';
-       }
-       if ($pst[0] == $cst[0] && $pst[1] == $cst[1])
-       {
-           $dir = undef;
-       }
-       else
-       {
-           do
-           {
-               unless (defined ($dir = readdir(PARENT)))
-               {
-                   warn "readdir($dotdots): $!";
-                   closedir(PARENT);
-                   return '';
-               }
-               unless (@tst = lstat("$dotdots/$dir"))
-               {
-                   # warn "lstat($dotdots/$dir): $!";
-                   # Just because you can't lstat this directory
-                   # doesn't mean you'll never find the right one.
-                   # closedir(PARENT);
-                   # return '';
-               }
-           }
-           while ($dir eq '.' || $dir eq '..' || $tst[0] != $pst[0] ||
-                  $tst[1] != $pst[1]);
-       }
-       $cwd = (defined $dir ? "$dir" : "" ) . "/$cwd" ;
-       closedir(PARENT);
-    } while (defined $dir);
-    chop($cwd) unless $cwd eq '/'; # drop the trailing /
-    $cwd;
+    abs_path('.');
 }
 
-
-
 # By John Bazik
 #
 # Usage: $cwd = &fastcwd;
@@ -162,7 +118,7 @@ sub fastcwd {
     for (;;) {
        my $direntry;
        ($odev, $oino) = ($cdev, $cino);
-       chdir('..') || return undef;
+       CORE::chdir('..') || return undef;
        ($cdev, $cino) = stat('.');
        last if $odev == $cdev && $oino == $cino;
        opendir(DIR, '.') || return undef;
@@ -180,10 +136,11 @@ sub fastcwd {
        unshift(@path, $direntry);
     }
     $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 =~ /^(.*)$/;   # untaint
-    chdir($path) || return undef;
+    CORE::chdir($path) || return undef;
     ($cdev, $cino) = stat('.');
     die "Unstable directory path, current directory changed unexpectedly"
        if $cdev != $orig_cdev || $cino != $orig_cino;
@@ -249,9 +206,11 @@ sub chdir {
 
 sub abs_path
 {
-    my $start = shift || '.';
+    my $start = @_ ? shift : '.';
     my($dotdots, $cwd, @pst, @cst, $dir, @tst);
 
+    return cwd() if ( $^O =~ /cygwin/ );
+
     unless (@cst = stat( $start ))
     {
        carp "stat($start): $!";
@@ -276,7 +235,7 @@ sub abs_path
        }
        if ($pst[0] == $cst[0] && $pst[1] == $cst[1])
        {
-           $dir = '';
+           $dir = undef;
        }
        else
        {
@@ -293,19 +252,19 @@ sub abs_path
            while ($dir eq '.' || $dir eq '..' || $tst[0] != $pst[0] ||
                   $tst[1] != $pst[1]);
        }
-       $cwd = "$dir/$cwd";
+       $cwd = (defined $dir ? "$dir" : "" ) . "/$cwd" ;
        closedir(PARENT);
-    } while ($dir);
-    chop($cwd); # drop the trailing /
+    } while (defined $dir);
+    chop($cwd) unless $cwd eq '/'; # drop the trailing /
     $cwd;
 }
 
 sub fast_abs_path {
     my $cwd = getcwd();
     my $path = shift || '.';
-    chdir($path) || croak "Cannot chdir to $path:$!";
+    CORE::chdir($path) || croak "Cannot chdir to $path:$!";
     my $realpath = getcwd();
-    chdir($cwd)  || croak "Cannot chdir back to $cwd:$!";
+    CORE::chdir($cwd)  || croak "Cannot chdir back to $cwd:$!";
     $realpath;
 }
 
@@ -313,7 +272,7 @@ sub fast_abs_path {
 # --- PORTING SECTION ---
 
 # VMS: $ENV{'DEFAULT'} points to default directory at all times
-# 06-Mar-1996  Charles Bailey  bailey@genetics.upenn.edu
+# 06-Mar-1996  Charles Bailey  bailey@newman.upenn.edu
 # Note: Use of Cwd::chdir() causes the logical name PWD to be defined
 #   in the process logical name table as the default device and directory
 #   seen by Perl. This may not be the same as the default device
@@ -339,13 +298,13 @@ sub _os2_cwd {
 }
 
 sub _win32_cwd {
-    $ENV{'PWD'} = Win32::GetCurrentDirectory();
+    $ENV{'PWD'} = Win32::GetCwd();
     $ENV{'PWD'} =~ s:\\:/:g ;
     return $ENV{'PWD'};
 }
 
 *_NT_cwd = \&_win32_cwd if (!defined &_NT_cwd && 
-                            defined &Win32::GetCurrentDirectory);
+                            defined &Win32::GetCwd);
 
 *_NT_cwd = \&_os2_cwd unless defined &_NT_cwd;
 
@@ -360,6 +319,19 @@ sub _dos_cwd {
     return $ENV{'PWD'};
 }
 
+sub _qnx_cwd {
+    $ENV{'PWD'} = `/usr/bin/fullpath -t`;
+    chop $ENV{'PWD'};
+    return $ENV{'PWD'};
+}
+
+sub _qnx_abs_path {
+    my $path = shift || '.';
+    my $realpath=`/usr/bin/fullpath -t $path`;
+    chop $realpath;
+    return $realpath;
+}
+
 {
     local $^W = 0;     # assignments trigger 'subroutine redefined' warning
 
@@ -394,6 +366,14 @@ sub _dos_cwd {
         *fastcwd       = \&_dos_cwd;
         *abs_path      = \&fast_abs_path;
     }
+    elsif ($^O eq 'qnx') {
+        *cwd           = \&_qnx_cwd;
+        *getcwd                = \&_qnx_cwd;
+        *fastgetcwd    = \&_qnx_cwd;
+        *fastcwd       = \&_qnx_cwd;
+        *abs_path      = \&_qnx_abs_path;
+        *fast_abs_path = \&_qnx_abs_path;
+    }
 }
 
 # package main; eval join('',<DATA>) || die $@;        # quick test