Upgrade to Test::Harness 2.30.
[p5sagit/p5-mst-13.2.git] / lib / Cwd.pm
CommitLineData
a0d0e21e 1package Cwd;
3b825e41 2use 5.006;
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;
04929354 11 my $dir = getcwd;
4633a7c4 12
04929354 13 use Cwd 'abs_path';
14 my $abs_path = abs_path($file);
f06db76b 15
04929354 16=head1 DESCRIPTION
902bacac 17
04929354 18This module provides functions for determining the pathname of the
19current working directory. It is recommended that getcwd (or another
20*cwd() function) be used in I<all> code to ensure portability.
f06db76b 21
04929354 22By default, it exports the functions cwd(), getcwd(), fastcwd(), and
23fastgetcwd() into the caller's namespace.
f06db76b 24
20408e3c 25
04929354 26=head2 getcwd and friends
20408e3c 27
04929354 28Each of these functions are called without arguments and return the
29absolute path of the current working directory.
f06db76b 30
04929354 31=over 4
32
33=item getcwd
34
35 my $cwd = getcwd();
36
37Returns the current working directory.
38
39Re-implements the getcwd(3) (or getwd(3)) functions in Perl.
40
bde417e1 41Taint-safe.
42
04929354 43=item cwd
44
45 my $cwd = cwd();
46
47The cwd() is the most natural form for the current architecture. For
48most systems it is identical to `pwd` (but without the trailing line
49terminator).
50
248785eb 51Taint-safe.
04929354 52
53=item fastcwd
54
55 my $cwd = fastcwd();
56
57A more dangerous version of getcwd(), but potentially faster.
58
59It might conceivably chdir() you out of a directory that it can't
60chdir() you back into. If fastcwd encounters a problem it will return
61undef but will probably leave you in a different directory. For a
62measure of extra security, if everything appears to have worked, the
63fastcwd() function will check that it leaves you in the same directory
64that it started in. If it has changed it will C<die> with the message
65"Unstable directory path, current directory changed
66unexpectedly". That should never happen.
67
68=item fastgetcwd
69
70 my $cwd = fastgetcwd();
f06db76b 71
902bacac 72The fastgetcwd() function is provided as a synonym for cwd().
fb73857a 73
04929354 74=back
75
902bacac 76
04929354 77=head2 abs_path and friends
78
79These functions are exported only on request. They each take a single
80argument and return the absolute pathname for it.
81
82=over 4
83
84=item abs_path
85
86 my $abs_path = abs_path($file);
87
88Uses the same algorithm as getcwd(). Symbolic links and relative-path
89components ("." and "..") are resolved to return the canonical
90pathname, just like realpath(3).
91
bde417e1 92Taint-safe.
93
04929354 94=item realpath
95
96 my $abs_path = realpath($file);
97
98A synonym for abs_path().
99
bde417e1 100Taint-safe.
101
04929354 102=item fast_abs_path
103
510179aa 104 my $abs_path = fast_abs_path($file);
04929354 105
106A more dangerous, but potentially faster version of abs_path.
107
248785eb 108This function is B<Not> taint-safe : you can't use it in programs
109that work under taint mode.
bde417e1 110
04929354 111=back
112
113=head2 $ENV{PWD}
114
115If you ask to override your chdir() built-in function,
116
117 use Cwd qw(chdir);
118
119then your PWD environment variable will be kept up to date. Note that
120it will only be kept up to date if all packages which use chdir import
121it from Cwd.
4633a7c4 122
4633a7c4 123
4d6b4052 124=head1 NOTES
125
126=over 4
127
128=item *
129
04929354 130Since the path seperators are different on some operating systems ('/'
131on Unix, ':' on MacPerl, etc...) we recommend you use the File::Spec
132modules wherever portability is a concern.
133
04929354 134=item *
4d6b4052 135
136Actually, on Mac OS, the C<getcwd()>, C<fastgetcwd()> and C<fastcwd()>
137functions are all aliases for the C<cwd()> function, which, on Mac OS,
138calls `pwd`. Likewise, the C<abs_path()> function is an alias for
139C<fast_abs_path()>.
140
141=back
142
04929354 143=head1 SEE ALSO
144
145L<File::chdir>
146
f06db76b 147=cut
148
b060a406 149use strict;
96e4d5b1 150
151use Carp;
152
2af1ab88 153our $VERSION = '2.07';
96e4d5b1 154
b060a406 155use base qw/ Exporter /;
156our @EXPORT = qw(cwd getcwd fastcwd fastgetcwd);
157our @EXPORT_OK = qw(chdir abs_path fast_abs_path realpath fast_realpath);
a0d0e21e 158
f5f423e4 159# sys_cwd may keep the builtin command
160
161# All the functionality of this module may provided by builtins,
162# there is no sense to process the rest of the file.
163# The best choice may be to have this in BEGIN, but how to return from BEGIN?
164
165if ($^O eq 'os2' && defined &sys_cwd && defined &sys_abspath) {
166 local $^W = 0;
167 *cwd = \&sys_cwd;
168 *getcwd = \&cwd;
169 *fastgetcwd = \&cwd;
170 *fastcwd = \&cwd;
171 *abs_path = \&sys_abspath;
172 *fast_abs_path = \&abs_path;
173 *realpath = \&abs_path;
174 *fast_realpath = \&abs_path;
175 return 1;
176}
177
f22d8e4b 178eval {
179 require XSLoader;
716e3b8c 180 undef *Cwd::fastcwd; # avoid redefinition warning
f22d8e4b 181 XSLoader::load('Cwd');
182};
4633a7c4 183
96e4d5b1 184
3547aa9a 185# Find the pwd command in the expected locations. We assume these
186# are safe. This prevents _backtick_pwd() consulting $ENV{PATH}
187# so everything works under taint mode.
188my $pwd_cmd;
189foreach my $try (qw(/bin/pwd /usr/bin/pwd)) {
190 if( -x $try ) {
191 $pwd_cmd = $try;
192 last;
193 }
194}
522b859a 195unless ($pwd_cmd) {
196 if (-x '/QOpenSys/bin/pwd') { # OS/400 PASE.
197 $pwd_cmd = '/QOpenSys/bin/pwd' ;
198 } else {
199 # Isn't this wrong? _backtick_pwd() will fail if somenone has
200 # pwd in their path but it is not /bin/pwd or /usr/bin/pwd?
201 # See [perl #16774]. --jhi
202 $pwd_cmd = 'pwd';
203 }
204}
3547aa9a 205
206# The 'natural and safe form' for UNIX (pwd may be setuid root)
8b88ae92 207sub _backtick_pwd {
db281859 208 local @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)};
3547aa9a 209 my $cwd = `$pwd_cmd`;
ac3b20cb 210 # Belt-and-suspenders in case someone said "undef $/".
5cf6da5f 211 local $/ = "\n";
ac3b20cb 212 # `pwd` may fail e.g. if the disk is full
7e03f963 213 chomp($cwd) if defined $cwd;
4633a7c4 214 $cwd;
8b88ae92 215}
4633a7c4 216
217# Since some ports may predefine cwd internally (e.g., NT)
218# we take care not to override an existing definition for cwd().
219
ea54c8bd 220unless(defined &cwd) {
221 # The pwd command is not available in some chroot(2)'ed environments
73b801a6 222 if( $^O eq 'MacOS' || (defined $ENV{PATH} &&
223 grep { -x "$_/pwd" } split(':', $ENV{PATH})) )
224 {
ea54c8bd 225 *cwd = \&_backtick_pwd;
226 }
227 else {
228 *cwd = \&getcwd;
229 }
230}
a0d0e21e 231
1f4f94f5 232# set a reasonable (and very safe) default for fastgetcwd, in case it
233# isn't redefined later (20001212 rspier)
234*fastgetcwd = \&cwd;
748a9306 235
a0d0e21e 236# By Brandon S. Allbery
237#
238# Usage: $cwd = getcwd();
239
240sub getcwd
241{
07569ed3 242 abs_path('.');
a0d0e21e 243}
244
a0c9c202 245
246# By John Bazik
247#
248# Usage: $cwd = &fastcwd;
249#
250# This is a faster version of getcwd. It's also more dangerous because
251# you might chdir out of a directory that you can't chdir back into.
252
253sub fastcwd {
254 my($odev, $oino, $cdev, $cino, $tdev, $tino);
255 my(@path, $path);
256 local(*DIR);
257
258 my($orig_cdev, $orig_cino) = stat('.');
259 ($cdev, $cino) = ($orig_cdev, $orig_cino);
260 for (;;) {
261 my $direntry;
262 ($odev, $oino) = ($cdev, $cino);
263 CORE::chdir('..') || return undef;
264 ($cdev, $cino) = stat('.');
265 last if $odev == $cdev && $oino == $cino;
266 opendir(DIR, '.') || return undef;
267 for (;;) {
268 $direntry = readdir(DIR);
269 last unless defined $direntry;
270 next if $direntry eq '.';
271 next if $direntry eq '..';
272
273 ($tdev, $tino) = lstat($direntry);
274 last unless $tdev != $odev || $tino != $oino;
275 }
276 closedir(DIR);
277 return undef unless defined $direntry; # should never happen
278 unshift(@path, $direntry);
279 }
280 $path = '/' . join('/', @path);
281 if ($^O eq 'apollo') { $path = "/".$path; }
282 # At this point $path may be tainted (if tainting) and chdir would fail.
248785eb 283 # Untaint it then check that we landed where we started.
284 $path =~ /^(.*)\z/s # untaint
285 && CORE::chdir($1) or return undef;
a0c9c202 286 ($cdev, $cino) = stat('.');
287 die "Unstable directory path, current directory changed unexpectedly"
288 if $cdev != $orig_cdev || $cino != $orig_cino;
289 $path;
290}
291
292
4633a7c4 293# Keeps track of current working directory in PWD environment var
a0d0e21e 294# Usage:
295# use Cwd 'chdir';
296# chdir $newdir;
297
4633a7c4 298my $chdir_init = 0;
a0d0e21e 299
4633a7c4 300sub chdir_init {
3b8e3443 301 if ($ENV{'PWD'} and $^O ne 'os2' and $^O ne 'dos' and $^O ne 'MSWin32') {
a0d0e21e 302 my($dd,$di) = stat('.');
303 my($pd,$pi) = stat($ENV{'PWD'});
304 if (!defined $dd or !defined $pd or $di != $pi or $dd != $pd) {
4633a7c4 305 $ENV{'PWD'} = cwd();
a0d0e21e 306 }
307 }
308 else {
3b8e3443 309 my $wd = cwd();
310 $wd = Win32::GetFullPathName($wd) if $^O eq 'MSWin32';
311 $ENV{'PWD'} = $wd;
a0d0e21e 312 }
4633a7c4 313 # Strip an automounter prefix (where /tmp_mnt/foo/bar == /foo/bar)
3b8e3443 314 if ($^O ne 'MSWin32' and $ENV{'PWD'} =~ m|(/[^/]+(/[^/]+/[^/]+))(.*)|s) {
a0d0e21e 315 my($pd,$pi) = stat($2);
316 my($dd,$di) = stat($1);
317 if (defined $pd and defined $dd and $di == $pi and $dd == $pd) {
318 $ENV{'PWD'}="$2$3";
319 }
320 }
321 $chdir_init = 1;
322}
323
324sub chdir {
22978713 325 my $newdir = @_ ? shift : ''; # allow for no arg (chdir to HOME dir)
3b8e3443 326 $newdir =~ s|///*|/|g unless $^O eq 'MSWin32';
a0d0e21e 327 chdir_init() unless $chdir_init;
4ffa1610 328 my $newpwd;
329 if ($^O eq 'MSWin32') {
330 # get the full path name *before* the chdir()
331 $newpwd = Win32::GetFullPathName($newdir);
332 }
333
4633a7c4 334 return 0 unless CORE::chdir $newdir;
4ffa1610 335
3b8e3443 336 if ($^O eq 'VMS') {
337 return $ENV{'PWD'} = $ENV{'DEFAULT'}
338 }
4aecb5b5 339 elsif ($^O eq 'MacOS') {
340 return $ENV{'PWD'} = cwd();
341 }
3b8e3443 342 elsif ($^O eq 'MSWin32') {
4ffa1610 343 $ENV{'PWD'} = $newpwd;
3b8e3443 344 return 1;
345 }
748a9306 346
392d8ab8 347 if ($newdir =~ m#^/#s) {
a0d0e21e 348 $ENV{'PWD'} = $newdir;
4633a7c4 349 } else {
350 my @curdir = split(m#/#,$ENV{'PWD'});
351 @curdir = ('') unless @curdir;
352 my $component;
a0d0e21e 353 foreach $component (split(m#/#, $newdir)) {
354 next if $component eq '.';
355 pop(@curdir),next if $component eq '..';
356 push(@curdir,$component);
357 }
358 $ENV{'PWD'} = join('/',@curdir) || '/';
359 }
4633a7c4 360 1;
a0d0e21e 361}
362
a0c9c202 363
364# In case the XS version doesn't load.
365*abs_path = \&_perl_abs_path unless defined &abs_path;
366sub _perl_abs_path
367{
368 my $start = @_ ? shift : '.';
369 my($dotdots, $cwd, @pst, @cst, $dir, @tst);
370
371 unless (@cst = stat( $start ))
372 {
373 carp "stat($start): $!";
374 return '';
375 }
376 $cwd = '';
377 $dotdots = $start;
378 do
379 {
380 $dotdots .= '/..';
381 @pst = @cst;
a25ef67d 382 local *PARENT;
a0c9c202 383 unless (opendir(PARENT, $dotdots))
384 {
385 carp "opendir($dotdots): $!";
386 return '';
387 }
388 unless (@cst = stat($dotdots))
389 {
390 carp "stat($dotdots): $!";
391 closedir(PARENT);
392 return '';
393 }
394 if ($pst[0] == $cst[0] && $pst[1] == $cst[1])
395 {
396 $dir = undef;
397 }
398 else
399 {
400 do
401 {
402 unless (defined ($dir = readdir(PARENT)))
403 {
404 carp "readdir($dotdots): $!";
405 closedir(PARENT);
406 return '';
407 }
408 $tst[0] = $pst[0]+1 unless (@tst = lstat("$dotdots/$dir"))
409 }
410 while ($dir eq '.' || $dir eq '..' || $tst[0] != $pst[0] ||
411 $tst[1] != $pst[1]);
412 }
413 $cwd = (defined $dir ? "$dir" : "" ) . "/$cwd" ;
414 closedir(PARENT);
415 } while (defined $dir);
416 chop($cwd) unless $cwd eq '/'; # drop the trailing /
417 $cwd;
418}
419
420
e4c51978 421# added function alias for those of us more
422# used to the libc function. --tchrist 27-Jan-00
423*realpath = \&abs_path;
424
96e4d5b1 425sub fast_abs_path {
426 my $cwd = getcwd();
4d6b4052 427 require File::Spec;
428 my $path = @_ ? shift : File::Spec->curdir;
926cbafe 429 CORE::chdir($path) || croak "Cannot chdir to $path: $!";
96e4d5b1 430 my $realpath = getcwd();
bde417e1 431 -d $cwd && CORE::chdir($cwd) ||
926cbafe 432 croak "Cannot chdir back to $cwd: $!";
96e4d5b1 433 $realpath;
8b88ae92 434}
435
e4c51978 436# added function alias to follow principle of least surprise
437# based on previous aliasing. --tchrist 27-Jan-00
438*fast_realpath = \&fast_abs_path;
439
4633a7c4 440
441# --- PORTING SECTION ---
442
443# VMS: $ENV{'DEFAULT'} points to default directory at all times
bd3fa61c 444# 06-Mar-1996 Charles Bailey bailey@newman.upenn.edu
c6538b72 445# Note: Use of Cwd::chdir() causes the logical name PWD to be defined
8b88ae92 446# in the process logical name table as the default device and directory
447# seen by Perl. This may not be the same as the default device
4633a7c4 448# and directory seen by DCL after Perl exits, since the effects
449# the CRTL chdir() function persist only until Perl exits.
4633a7c4 450
451sub _vms_cwd {
96e4d5b1 452 return $ENV{'DEFAULT'};
453}
454
455sub _vms_abs_path {
456 return $ENV{'DEFAULT'} unless @_;
457 my $path = VMS::Filespec::pathify($_[0]);
458 croak("Invalid path name $_[0]") unless defined $path;
459 return VMS::Filespec::rmsexpand($path);
4633a7c4 460}
68dc0745 461
4633a7c4 462sub _os2_cwd {
463 $ENV{'PWD'} = `cmd /c cd`;
464 chop $ENV{'PWD'};
aa6b7957 465 $ENV{'PWD'} =~ s:\\:/:g ;
4633a7c4 466 return $ENV{'PWD'};
467}
468
96e4d5b1 469sub _win32_cwd {
2d7a9237 470 $ENV{'PWD'} = Win32::GetCwd();
aa6b7957 471 $ENV{'PWD'} =~ s:\\:/:g ;
96e4d5b1 472 return $ENV{'PWD'};
473}
474
475*_NT_cwd = \&_win32_cwd if (!defined &_NT_cwd &&
2d7a9237 476 defined &Win32::GetCwd);
96e4d5b1 477
478*_NT_cwd = \&_os2_cwd unless defined &_NT_cwd;
68dc0745 479
39e571d4 480sub _dos_cwd {
481 if (!defined &Dos::GetCwd) {
482 $ENV{'PWD'} = `command /c cd`;
483 chop $ENV{'PWD'};
aa6b7957 484 $ENV{'PWD'} =~ s:\\:/:g ;
39e571d4 485 } else {
486 $ENV{'PWD'} = Dos::GetCwd();
487 }
55497cff 488 return $ENV{'PWD'};
489}
490
7fbf1995 491sub _qnx_cwd {
35b807ef 492 local $ENV{PATH} = '';
493 local $ENV{CDPATH} = '';
494 local $ENV{ENV} = '';
7fbf1995 495 $ENV{'PWD'} = `/usr/bin/fullpath -t`;
496 chop $ENV{'PWD'};
497 return $ENV{'PWD'};
498}
499
500sub _qnx_abs_path {
35b807ef 501 local $ENV{PATH} = '';
502 local $ENV{CDPATH} = '';
503 local $ENV{ENV} = '';
fa921dc6 504 my $path = @_ ? shift : '.';
7fbf1995 505 my $realpath=`/usr/bin/fullpath -t $path`;
506 chop $realpath;
507 return $realpath;
508}
509
ed79a026 510sub _epoc_cwd {
511 $ENV{'PWD'} = EPOC::getcwd();
512 return $ENV{'PWD'};
513}
514
ac1ad7f0 515{
db376a24 516 no warnings; # assignments trigger 'subroutine redefined' warning
4633a7c4 517
ac1ad7f0 518 if ($^O eq 'VMS') {
96e4d5b1 519 *cwd = \&_vms_cwd;
520 *getcwd = \&_vms_cwd;
521 *fastcwd = \&_vms_cwd;
522 *fastgetcwd = \&_vms_cwd;
523 *abs_path = \&_vms_abs_path;
524 *fast_abs_path = \&_vms_abs_path;
ac1ad7f0 525 }
526 elsif ($^O eq 'NT' or $^O eq 'MSWin32') {
527 # We assume that &_NT_cwd is defined as an XSUB or in the core.
96e4d5b1 528 *cwd = \&_NT_cwd;
529 *getcwd = \&_NT_cwd;
530 *fastcwd = \&_NT_cwd;
531 *fastgetcwd = \&_NT_cwd;
532 *abs_path = \&fast_abs_path;
cade0c02 533 *realpath = \&fast_abs_path;
ac1ad7f0 534 }
535 elsif ($^O eq 'os2') {
536 # sys_cwd may keep the builtin command
96e4d5b1 537 *cwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd;
538 *getcwd = \&cwd;
539 *fastgetcwd = \&cwd;
540 *fastcwd = \&cwd;
541 *abs_path = \&fast_abs_path;
ac1ad7f0 542 }
39e571d4 543 elsif ($^O eq 'dos') {
544 *cwd = \&_dos_cwd;
545 *getcwd = \&_dos_cwd;
546 *fastgetcwd = \&_dos_cwd;
547 *fastcwd = \&_dos_cwd;
96e4d5b1 548 *abs_path = \&fast_abs_path;
ac1ad7f0 549 }
7438b6ad 550 elsif ($^O =~ m/^(?:qnx|nto)$/ ) {
7fbf1995 551 *cwd = \&_qnx_cwd;
552 *getcwd = \&_qnx_cwd;
553 *fastgetcwd = \&_qnx_cwd;
554 *fastcwd = \&_qnx_cwd;
555 *abs_path = \&_qnx_abs_path;
556 *fast_abs_path = \&_qnx_abs_path;
557 }
4fabb596 558 elsif ($^O eq 'cygwin') {
1cab015a 559 *getcwd = \&cwd;
560 *fastgetcwd = \&cwd;
561 *fastcwd = \&cwd;
562 *abs_path = \&fast_abs_path;
563 }
ed79a026 564 elsif ($^O eq 'epoc') {
fa6a1c44 565 *cwd = \&_epoc_cwd;
566 *getcwd = \&_epoc_cwd;
ed79a026 567 *fastgetcwd = \&_epoc_cwd;
568 *fastcwd = \&_epoc_cwd;
569 *abs_path = \&fast_abs_path;
570 }
4aecb5b5 571 elsif ($^O eq 'MacOS') {
572 *getcwd = \&cwd;
573 *fastgetcwd = \&cwd;
574 *fastcwd = \&cwd;
575 *abs_path = \&fast_abs_path;
576 }
55497cff 577}
4633a7c4 578
4633a7c4 579
a0d0e21e 5801;