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