Re: stability of sort()?
[p5sagit/p5-mst-13.2.git] / lib / Cwd.pm
CommitLineData
a0d0e21e 1package Cwd;
2require 5.000;
a0d0e21e 3
f06db76b 4=head1 NAME
5
902bacac 6Cwd - get pathname of current working directory
f06db76b 7
8=head1 SYNOPSIS
9
4633a7c4 10 use Cwd;
11 $dir = cwd;
12
13 use Cwd;
14 $dir = getcwd;
f06db76b 15
16 use Cwd;
902bacac 17 $dir = fastcwd;
18
19 use Cwd;
4633a7c4 20 $dir = fastgetcwd;
f06db76b 21
22 use Cwd 'chdir';
23 chdir "/tmp";
24 print $ENV{'PWD'};
25
e4c51978 26 use Cwd 'abs_path'; # aka realpath()
20408e3c 27 print abs_path($ENV{'PWD'});
28
29 use Cwd 'fast_abs_path';
30 print fast_abs_path($ENV{'PWD'});
31
f06db76b 32=head1 DESCRIPTION
33
902bacac 34This module provides functions for determining the pathname of the
35current working directory. By default, it exports the functions
36cwd(), getcwd(), fastcwd(), and fastgetcwd() into the caller's
37namespace. Each of these functions are called without arguments and
38return the absolute path of the current working directory. It is
39recommended that cwd (or another *cwd() function) be used in I<all>
40code to ensure portability.
41
42The cwd() is the most natural and safe form for the current
43architecture. For most systems it is identical to `pwd` (but without
44the trailing line terminator).
45
f06db76b 46The getcwd() function re-implements the getcwd(3) (or getwd(3)) functions
4633a7c4 47in Perl.
f06db76b 48
cb1a09d0 49The fastcwd() function looks the same as getcwd(), but runs faster.
fb73857a 50It's also more dangerous because it might conceivably chdir() you out
51of a directory that it can't chdir() you back into. If fastcwd
52encounters a problem it will return undef but will probably leave you
53in a different directory. For a measure of extra security, if
54everything appears to have worked, the fastcwd() function will check
55that it leaves you in the same directory that it started in. If it has
56changed it will C<die> with the message "Unstable directory path,
57current directory changed unexpectedly". That should never happen.
f06db76b 58
902bacac 59The fastgetcwd() function is provided as a synonym for cwd().
fb73857a 60
902bacac 61The abs_path() function takes a single argument and returns the
62absolute pathname for that argument. It uses the same algorithm as
63getcwd(). (Actually, getcwd() is abs_path(".")) Symbolic links and
64relative-path components ("." and "..") are resolved to return the
65canonical pathname, just like realpath(3). This function is also
66callable as realpath().
67
68The fast_abs_path() function looks the same as abs_path() but runs
69faster and, like fastcwd(), is more dangerous.
4633a7c4 70
71If you ask to override your chdir() built-in function, then your PWD
72environment variable will be kept up to date. (See
55497cff 73L<perlsub/Overriding Builtin Functions>.) Note that it will only be
1fef88e7 74kept up to date if all packages which use chdir import it from Cwd.
4633a7c4 75
f06db76b 76=cut
77
b060a406 78use strict;
96e4d5b1 79
80use Carp;
81
1f4f94f5 82our $VERSION = '2.04';
96e4d5b1 83
b060a406 84use base qw/ Exporter /;
85our @EXPORT = qw(cwd getcwd fastcwd fastgetcwd);
86our @EXPORT_OK = qw(chdir abs_path fast_abs_path realpath fast_realpath);
a0d0e21e 87
2ae52c40 88# Indicates if the XS portion has been loaded or not
89my $Booted = 0;
4633a7c4 90
8b88ae92 91# The 'natural and safe form' for UNIX (pwd may be setuid root)
96e4d5b1 92
8b88ae92 93sub _backtick_pwd {
7e03f963 94 my $cwd = `pwd`;
95 # `pwd` may fail e.g. if the disk is full
96 chomp($cwd) if defined $cwd;
4633a7c4 97 $cwd;
8b88ae92 98}
4633a7c4 99
100# Since some ports may predefine cwd internally (e.g., NT)
101# we take care not to override an existing definition for cwd().
102
ea54c8bd 103unless(defined &cwd) {
104 # The pwd command is not available in some chroot(2)'ed environments
4aecb5b5 105 if($^O eq 'MacOS' || grep { -x "$_/pwd" } split(':', $ENV{PATH})) {
ea54c8bd 106 *cwd = \&_backtick_pwd;
107 }
108 else {
109 *cwd = \&getcwd;
110 }
111}
a0d0e21e 112
1f4f94f5 113# set a reasonable (and very safe) default for fastgetcwd, in case it
114# isn't redefined later (20001212 rspier)
115*fastgetcwd = \&cwd;
748a9306 116
a0d0e21e 117# By Brandon S. Allbery
118#
119# Usage: $cwd = getcwd();
120
121sub getcwd
122{
07569ed3 123 abs_path('.');
a0d0e21e 124}
125
0d2079fa 126# Now a callout to an XSUB. We have to delay booting of the XSUB
127# until the first time fastcwd is called since Cwd::cwd is needed in the
128# building of perl when dynamic loading may be unavailable
a0d0e21e 129sub fastcwd {
2ae52c40 130 unless ($Booted) {
0d2079fa 131 require XSLoader;
132 XSLoader::load("Cwd");
2ae52c40 133 ++$Booted;
a0d0e21e 134 }
0d2079fa 135 return &Cwd::_fastcwd;
a0d0e21e 136}
137
138
4633a7c4 139# Keeps track of current working directory in PWD environment var
a0d0e21e 140# Usage:
141# use Cwd 'chdir';
142# chdir $newdir;
143
4633a7c4 144my $chdir_init = 0;
a0d0e21e 145
4633a7c4 146sub chdir_init {
3b8e3443 147 if ($ENV{'PWD'} and $^O ne 'os2' and $^O ne 'dos' and $^O ne 'MSWin32') {
a0d0e21e 148 my($dd,$di) = stat('.');
149 my($pd,$pi) = stat($ENV{'PWD'});
150 if (!defined $dd or !defined $pd or $di != $pi or $dd != $pd) {
4633a7c4 151 $ENV{'PWD'} = cwd();
a0d0e21e 152 }
153 }
154 else {
3b8e3443 155 my $wd = cwd();
156 $wd = Win32::GetFullPathName($wd) if $^O eq 'MSWin32';
157 $ENV{'PWD'} = $wd;
a0d0e21e 158 }
4633a7c4 159 # Strip an automounter prefix (where /tmp_mnt/foo/bar == /foo/bar)
3b8e3443 160 if ($^O ne 'MSWin32' and $ENV{'PWD'} =~ m|(/[^/]+(/[^/]+/[^/]+))(.*)|s) {
a0d0e21e 161 my($pd,$pi) = stat($2);
162 my($dd,$di) = stat($1);
163 if (defined $pd and defined $dd and $di == $pi and $dd == $pd) {
164 $ENV{'PWD'}="$2$3";
165 }
166 }
167 $chdir_init = 1;
168}
169
170sub chdir {
22978713 171 my $newdir = @_ ? shift : ''; # allow for no arg (chdir to HOME dir)
3b8e3443 172 $newdir =~ s|///*|/|g unless $^O eq 'MSWin32';
a0d0e21e 173 chdir_init() unless $chdir_init;
4ffa1610 174 my $newpwd;
175 if ($^O eq 'MSWin32') {
176 # get the full path name *before* the chdir()
177 $newpwd = Win32::GetFullPathName($newdir);
178 }
179
4633a7c4 180 return 0 unless CORE::chdir $newdir;
4ffa1610 181
3b8e3443 182 if ($^O eq 'VMS') {
183 return $ENV{'PWD'} = $ENV{'DEFAULT'}
184 }
4aecb5b5 185 elsif ($^O eq 'MacOS') {
186 return $ENV{'PWD'} = cwd();
187 }
3b8e3443 188 elsif ($^O eq 'MSWin32') {
4ffa1610 189 $ENV{'PWD'} = $newpwd;
3b8e3443 190 return 1;
191 }
748a9306 192
392d8ab8 193 if ($newdir =~ m#^/#s) {
a0d0e21e 194 $ENV{'PWD'} = $newdir;
4633a7c4 195 } else {
196 my @curdir = split(m#/#,$ENV{'PWD'});
197 @curdir = ('') unless @curdir;
198 my $component;
a0d0e21e 199 foreach $component (split(m#/#, $newdir)) {
200 next if $component eq '.';
201 pop(@curdir),next if $component eq '..';
202 push(@curdir,$component);
203 }
204 $ENV{'PWD'} = join('/',@curdir) || '/';
205 }
4633a7c4 206 1;
a0d0e21e 207}
208
2ae52c40 209# Now a callout to an XSUB
d57281c6 210sub abs_path
211{
2ae52c40 212 unless ($Booted) {
213 require XSLoader;
214 XSLoader::load("Cwd");
215 ++$Booted;
8b88ae92 216 }
2ae52c40 217 return &Cwd::_abs_path(@_);
8b88ae92 218}
219
e4c51978 220# added function alias for those of us more
221# used to the libc function. --tchrist 27-Jan-00
222*realpath = \&abs_path;
223
96e4d5b1 224sub fast_abs_path {
225 my $cwd = getcwd();
3c65c14b 226 my $path = @_ ? shift : '.';
e79e61e4 227 CORE::chdir($path) || croak "Cannot chdir to $path:$!";
96e4d5b1 228 my $realpath = getcwd();
e79e61e4 229 CORE::chdir($cwd) || croak "Cannot chdir back to $cwd:$!";
96e4d5b1 230 $realpath;
8b88ae92 231}
232
e4c51978 233# added function alias to follow principle of least surprise
234# based on previous aliasing. --tchrist 27-Jan-00
235*fast_realpath = \&fast_abs_path;
236
4633a7c4 237
238# --- PORTING SECTION ---
239
240# VMS: $ENV{'DEFAULT'} points to default directory at all times
bd3fa61c 241# 06-Mar-1996 Charles Bailey bailey@newman.upenn.edu
c6538b72 242# Note: Use of Cwd::chdir() causes the logical name PWD to be defined
8b88ae92 243# in the process logical name table as the default device and directory
244# seen by Perl. This may not be the same as the default device
4633a7c4 245# and directory seen by DCL after Perl exits, since the effects
246# the CRTL chdir() function persist only until Perl exits.
4633a7c4 247
248sub _vms_cwd {
96e4d5b1 249 return $ENV{'DEFAULT'};
250}
251
252sub _vms_abs_path {
253 return $ENV{'DEFAULT'} unless @_;
254 my $path = VMS::Filespec::pathify($_[0]);
255 croak("Invalid path name $_[0]") unless defined $path;
256 return VMS::Filespec::rmsexpand($path);
4633a7c4 257}
68dc0745 258
4633a7c4 259sub _os2_cwd {
260 $ENV{'PWD'} = `cmd /c cd`;
261 chop $ENV{'PWD'};
262 $ENV{'PWD'} =~ s:\\:/:g ;
263 return $ENV{'PWD'};
264}
265
96e4d5b1 266sub _win32_cwd {
2d7a9237 267 $ENV{'PWD'} = Win32::GetCwd();
96e4d5b1 268 $ENV{'PWD'} =~ s:\\:/:g ;
269 return $ENV{'PWD'};
270}
271
272*_NT_cwd = \&_win32_cwd if (!defined &_NT_cwd &&
2d7a9237 273 defined &Win32::GetCwd);
96e4d5b1 274
275*_NT_cwd = \&_os2_cwd unless defined &_NT_cwd;
68dc0745 276
39e571d4 277sub _dos_cwd {
278 if (!defined &Dos::GetCwd) {
279 $ENV{'PWD'} = `command /c cd`;
280 chop $ENV{'PWD'};
281 $ENV{'PWD'} =~ s:\\:/:g ;
282 } else {
283 $ENV{'PWD'} = Dos::GetCwd();
284 }
55497cff 285 return $ENV{'PWD'};
286}
287
7fbf1995 288sub _qnx_cwd {
289 $ENV{'PWD'} = `/usr/bin/fullpath -t`;
290 chop $ENV{'PWD'};
291 return $ENV{'PWD'};
292}
293
294sub _qnx_abs_path {
fa921dc6 295 my $path = @_ ? shift : '.';
7fbf1995 296 my $realpath=`/usr/bin/fullpath -t $path`;
297 chop $realpath;
298 return $realpath;
299}
300
ed79a026 301sub _epoc_cwd {
302 $ENV{'PWD'} = EPOC::getcwd();
303 return $ENV{'PWD'};
304}
305
ac1ad7f0 306{
db376a24 307 no warnings; # assignments trigger 'subroutine redefined' warning
4633a7c4 308
ac1ad7f0 309 if ($^O eq 'VMS') {
96e4d5b1 310 *cwd = \&_vms_cwd;
311 *getcwd = \&_vms_cwd;
312 *fastcwd = \&_vms_cwd;
313 *fastgetcwd = \&_vms_cwd;
314 *abs_path = \&_vms_abs_path;
315 *fast_abs_path = \&_vms_abs_path;
ac1ad7f0 316 }
317 elsif ($^O eq 'NT' or $^O eq 'MSWin32') {
318 # We assume that &_NT_cwd is defined as an XSUB or in the core.
96e4d5b1 319 *cwd = \&_NT_cwd;
320 *getcwd = \&_NT_cwd;
321 *fastcwd = \&_NT_cwd;
322 *fastgetcwd = \&_NT_cwd;
323 *abs_path = \&fast_abs_path;
ac1ad7f0 324 }
325 elsif ($^O eq 'os2') {
326 # sys_cwd may keep the builtin command
96e4d5b1 327 *cwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd;
328 *getcwd = \&cwd;
329 *fastgetcwd = \&cwd;
330 *fastcwd = \&cwd;
331 *abs_path = \&fast_abs_path;
ac1ad7f0 332 }
39e571d4 333 elsif ($^O eq 'dos') {
334 *cwd = \&_dos_cwd;
335 *getcwd = \&_dos_cwd;
336 *fastgetcwd = \&_dos_cwd;
337 *fastcwd = \&_dos_cwd;
96e4d5b1 338 *abs_path = \&fast_abs_path;
ac1ad7f0 339 }
7fbf1995 340 elsif ($^O eq 'qnx') {
341 *cwd = \&_qnx_cwd;
342 *getcwd = \&_qnx_cwd;
343 *fastgetcwd = \&_qnx_cwd;
344 *fastcwd = \&_qnx_cwd;
345 *abs_path = \&_qnx_abs_path;
346 *fast_abs_path = \&_qnx_abs_path;
347 }
4fabb596 348 elsif ($^O eq 'cygwin') {
1cab015a 349 *getcwd = \&cwd;
350 *fastgetcwd = \&cwd;
351 *fastcwd = \&cwd;
352 *abs_path = \&fast_abs_path;
353 }
ed79a026 354 elsif ($^O eq 'epoc') {
fa6a1c44 355 *cwd = \&_epoc_cwd;
356 *getcwd = \&_epoc_cwd;
ed79a026 357 *fastgetcwd = \&_epoc_cwd;
358 *fastcwd = \&_epoc_cwd;
359 *abs_path = \&fast_abs_path;
360 }
4aecb5b5 361 elsif ($^O eq 'MacOS') {
362 *getcwd = \&cwd;
363 *fastgetcwd = \&cwd;
364 *fastcwd = \&cwd;
365 *abs_path = \&fast_abs_path;
366 }
55497cff 367}
4633a7c4 368
369# package main; eval join('',<DATA>) || die $@; # quick test
370
a0d0e21e 3711;
372
4633a7c4 373__END__
374BEGIN { import Cwd qw(:DEFAULT chdir); }
375print join("\n", cwd, getcwd, fastcwd, "");
376chdir('..');
377print join("\n", cwd, getcwd, fastcwd, "");
378print "$ENV{PWD}\n";