Integrate change #9530 from maintperl into mainline.
[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
4633a7c4 88
8b88ae92 89# The 'natural and safe form' for UNIX (pwd may be setuid root)
96e4d5b1 90
8b88ae92 91sub _backtick_pwd {
7e03f963 92 my $cwd = `pwd`;
93 # `pwd` may fail e.g. if the disk is full
94 chomp($cwd) if defined $cwd;
4633a7c4 95 $cwd;
8b88ae92 96}
4633a7c4 97
98# Since some ports may predefine cwd internally (e.g., NT)
99# we take care not to override an existing definition for cwd().
100
ea54c8bd 101unless(defined &cwd) {
102 # The pwd command is not available in some chroot(2)'ed environments
4aecb5b5 103 if($^O eq 'MacOS' || grep { -x "$_/pwd" } split(':', $ENV{PATH})) {
ea54c8bd 104 *cwd = \&_backtick_pwd;
105 }
106 else {
107 *cwd = \&getcwd;
108 }
109}
a0d0e21e 110
1f4f94f5 111# set a reasonable (and very safe) default for fastgetcwd, in case it
112# isn't redefined later (20001212 rspier)
113*fastgetcwd = \&cwd;
748a9306 114
a0d0e21e 115# By Brandon S. Allbery
116#
117# Usage: $cwd = getcwd();
118
119sub getcwd
120{
07569ed3 121 abs_path('.');
a0d0e21e 122}
123
0d2079fa 124# Now a callout to an XSUB. We have to delay booting of the XSUB
125# until the first time fastcwd is called since Cwd::cwd is needed in the
126# building of perl when dynamic loading may be unavailable
127my $booted = 0;
a0d0e21e 128sub fastcwd {
0d2079fa 129 unless ($booted) {
130 require XSLoader;
131 XSLoader::load("Cwd");
132 ++$booted;
a0d0e21e 133 }
0d2079fa 134 return &Cwd::_fastcwd;
a0d0e21e 135}
136
137
4633a7c4 138# Keeps track of current working directory in PWD environment var
a0d0e21e 139# Usage:
140# use Cwd 'chdir';
141# chdir $newdir;
142
4633a7c4 143my $chdir_init = 0;
a0d0e21e 144
4633a7c4 145sub chdir_init {
3b8e3443 146 if ($ENV{'PWD'} and $^O ne 'os2' and $^O ne 'dos' and $^O ne 'MSWin32') {
a0d0e21e 147 my($dd,$di) = stat('.');
148 my($pd,$pi) = stat($ENV{'PWD'});
149 if (!defined $dd or !defined $pd or $di != $pi or $dd != $pd) {
4633a7c4 150 $ENV{'PWD'} = cwd();
a0d0e21e 151 }
152 }
153 else {
3b8e3443 154 my $wd = cwd();
155 $wd = Win32::GetFullPathName($wd) if $^O eq 'MSWin32';
156 $ENV{'PWD'} = $wd;
a0d0e21e 157 }
4633a7c4 158 # Strip an automounter prefix (where /tmp_mnt/foo/bar == /foo/bar)
3b8e3443 159 if ($^O ne 'MSWin32' and $ENV{'PWD'} =~ m|(/[^/]+(/[^/]+/[^/]+))(.*)|s) {
a0d0e21e 160 my($pd,$pi) = stat($2);
161 my($dd,$di) = stat($1);
162 if (defined $pd and defined $dd and $di == $pi and $dd == $pd) {
163 $ENV{'PWD'}="$2$3";
164 }
165 }
166 $chdir_init = 1;
167}
168
169sub chdir {
22978713 170 my $newdir = @_ ? shift : ''; # allow for no arg (chdir to HOME dir)
3b8e3443 171 $newdir =~ s|///*|/|g unless $^O eq 'MSWin32';
a0d0e21e 172 chdir_init() unless $chdir_init;
4ffa1610 173 my $newpwd;
174 if ($^O eq 'MSWin32') {
175 # get the full path name *before* the chdir()
176 $newpwd = Win32::GetFullPathName($newdir);
177 }
178
4633a7c4 179 return 0 unless CORE::chdir $newdir;
4ffa1610 180
3b8e3443 181 if ($^O eq 'VMS') {
182 return $ENV{'PWD'} = $ENV{'DEFAULT'}
183 }
4aecb5b5 184 elsif ($^O eq 'MacOS') {
185 return $ENV{'PWD'} = cwd();
186 }
3b8e3443 187 elsif ($^O eq 'MSWin32') {
4ffa1610 188 $ENV{'PWD'} = $newpwd;
3b8e3443 189 return 1;
190 }
748a9306 191
392d8ab8 192 if ($newdir =~ m#^/#s) {
a0d0e21e 193 $ENV{'PWD'} = $newdir;
4633a7c4 194 } else {
195 my @curdir = split(m#/#,$ENV{'PWD'});
196 @curdir = ('') unless @curdir;
197 my $component;
a0d0e21e 198 foreach $component (split(m#/#, $newdir)) {
199 next if $component eq '.';
200 pop(@curdir),next if $component eq '..';
201 push(@curdir,$component);
202 }
203 $ENV{'PWD'} = join('/',@curdir) || '/';
204 }
4633a7c4 205 1;
a0d0e21e 206}
207
d57281c6 208# Taken from Cwd.pm It is really getcwd with an optional
209# parameter instead of '.'
210#
8b88ae92 211
d57281c6 212sub abs_path
213{
214 my $start = @_ ? shift : '.';
215 my($dotdots, $cwd, @pst, @cst, $dir, @tst);
929327b2 216
d57281c6 217 unless (@cst = stat( $start ))
218 {
219 carp "stat($start): $!";
220 return '';
8b88ae92 221 }
d57281c6 222 $cwd = '';
223 $dotdots = $start;
224 do
225 {
226 $dotdots .= '/..';
227 @pst = @cst;
228 unless (opendir(PARENT, $dotdots))
229 {
230 carp "opendir($dotdots): $!";
231 return '';
232 }
233 unless (@cst = stat($dotdots))
234 {
235 carp "stat($dotdots): $!";
236 closedir(PARENT);
237 return '';
238 }
239 if ($pst[0] == $cst[0] && $pst[1] == $cst[1])
240 {
241 $dir = undef;
242 }
243 else
244 {
245 do
246 {
247 unless (defined ($dir = readdir(PARENT)))
248 {
249 carp "readdir($dotdots): $!";
250 closedir(PARENT);
251 return '';
252 }
253 $tst[0] = $pst[0]+1 unless (@tst = lstat("$dotdots/$dir"))
254 }
255 while ($dir eq '.' || $dir eq '..' || $tst[0] != $pst[0] ||
256 $tst[1] != $pst[1]);
257 }
258 $cwd = (defined $dir ? "$dir" : "" ) . "/$cwd" ;
259 closedir(PARENT);
260 } while (defined $dir);
261 chop($cwd) unless $cwd eq '/'; # drop the trailing /
262 $cwd;
8b88ae92 263}
264
e4c51978 265# added function alias for those of us more
266# used to the libc function. --tchrist 27-Jan-00
267*realpath = \&abs_path;
268
96e4d5b1 269sub fast_abs_path {
270 my $cwd = getcwd();
3c65c14b 271 my $path = @_ ? shift : '.';
e79e61e4 272 CORE::chdir($path) || croak "Cannot chdir to $path:$!";
96e4d5b1 273 my $realpath = getcwd();
e79e61e4 274 CORE::chdir($cwd) || croak "Cannot chdir back to $cwd:$!";
96e4d5b1 275 $realpath;
8b88ae92 276}
277
e4c51978 278# added function alias to follow principle of least surprise
279# based on previous aliasing. --tchrist 27-Jan-00
280*fast_realpath = \&fast_abs_path;
281
4633a7c4 282
283# --- PORTING SECTION ---
284
285# VMS: $ENV{'DEFAULT'} points to default directory at all times
bd3fa61c 286# 06-Mar-1996 Charles Bailey bailey@newman.upenn.edu
c6538b72 287# Note: Use of Cwd::chdir() causes the logical name PWD to be defined
8b88ae92 288# in the process logical name table as the default device and directory
289# seen by Perl. This may not be the same as the default device
4633a7c4 290# and directory seen by DCL after Perl exits, since the effects
291# the CRTL chdir() function persist only until Perl exits.
4633a7c4 292
293sub _vms_cwd {
96e4d5b1 294 return $ENV{'DEFAULT'};
295}
296
297sub _vms_abs_path {
298 return $ENV{'DEFAULT'} unless @_;
299 my $path = VMS::Filespec::pathify($_[0]);
300 croak("Invalid path name $_[0]") unless defined $path;
301 return VMS::Filespec::rmsexpand($path);
4633a7c4 302}
68dc0745 303
4633a7c4 304sub _os2_cwd {
305 $ENV{'PWD'} = `cmd /c cd`;
306 chop $ENV{'PWD'};
307 $ENV{'PWD'} =~ s:\\:/:g ;
308 return $ENV{'PWD'};
309}
310
96e4d5b1 311sub _win32_cwd {
2d7a9237 312 $ENV{'PWD'} = Win32::GetCwd();
96e4d5b1 313 $ENV{'PWD'} =~ s:\\:/:g ;
314 return $ENV{'PWD'};
315}
316
317*_NT_cwd = \&_win32_cwd if (!defined &_NT_cwd &&
2d7a9237 318 defined &Win32::GetCwd);
96e4d5b1 319
320*_NT_cwd = \&_os2_cwd unless defined &_NT_cwd;
68dc0745 321
39e571d4 322sub _dos_cwd {
323 if (!defined &Dos::GetCwd) {
324 $ENV{'PWD'} = `command /c cd`;
325 chop $ENV{'PWD'};
326 $ENV{'PWD'} =~ s:\\:/:g ;
327 } else {
328 $ENV{'PWD'} = Dos::GetCwd();
329 }
55497cff 330 return $ENV{'PWD'};
331}
332
7fbf1995 333sub _qnx_cwd {
334 $ENV{'PWD'} = `/usr/bin/fullpath -t`;
335 chop $ENV{'PWD'};
336 return $ENV{'PWD'};
337}
338
339sub _qnx_abs_path {
fa921dc6 340 my $path = @_ ? shift : '.';
7fbf1995 341 my $realpath=`/usr/bin/fullpath -t $path`;
342 chop $realpath;
343 return $realpath;
344}
345
ed79a026 346sub _epoc_cwd {
347 $ENV{'PWD'} = EPOC::getcwd();
348 return $ENV{'PWD'};
349}
350
ac1ad7f0 351{
db376a24 352 no warnings; # assignments trigger 'subroutine redefined' warning
4633a7c4 353
ac1ad7f0 354 if ($^O eq 'VMS') {
96e4d5b1 355 *cwd = \&_vms_cwd;
356 *getcwd = \&_vms_cwd;
357 *fastcwd = \&_vms_cwd;
358 *fastgetcwd = \&_vms_cwd;
359 *abs_path = \&_vms_abs_path;
360 *fast_abs_path = \&_vms_abs_path;
ac1ad7f0 361 }
362 elsif ($^O eq 'NT' or $^O eq 'MSWin32') {
363 # We assume that &_NT_cwd is defined as an XSUB or in the core.
96e4d5b1 364 *cwd = \&_NT_cwd;
365 *getcwd = \&_NT_cwd;
366 *fastcwd = \&_NT_cwd;
367 *fastgetcwd = \&_NT_cwd;
368 *abs_path = \&fast_abs_path;
ac1ad7f0 369 }
370 elsif ($^O eq 'os2') {
371 # sys_cwd may keep the builtin command
96e4d5b1 372 *cwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd;
373 *getcwd = \&cwd;
374 *fastgetcwd = \&cwd;
375 *fastcwd = \&cwd;
376 *abs_path = \&fast_abs_path;
ac1ad7f0 377 }
39e571d4 378 elsif ($^O eq 'dos') {
379 *cwd = \&_dos_cwd;
380 *getcwd = \&_dos_cwd;
381 *fastgetcwd = \&_dos_cwd;
382 *fastcwd = \&_dos_cwd;
96e4d5b1 383 *abs_path = \&fast_abs_path;
ac1ad7f0 384 }
7fbf1995 385 elsif ($^O eq 'qnx') {
386 *cwd = \&_qnx_cwd;
387 *getcwd = \&_qnx_cwd;
388 *fastgetcwd = \&_qnx_cwd;
389 *fastcwd = \&_qnx_cwd;
390 *abs_path = \&_qnx_abs_path;
391 *fast_abs_path = \&_qnx_abs_path;
392 }
4fabb596 393 elsif ($^O eq 'cygwin') {
1cab015a 394 *getcwd = \&cwd;
395 *fastgetcwd = \&cwd;
396 *fastcwd = \&cwd;
397 *abs_path = \&fast_abs_path;
398 }
ed79a026 399 elsif ($^O eq 'epoc') {
fa6a1c44 400 *cwd = \&_epoc_cwd;
401 *getcwd = \&_epoc_cwd;
ed79a026 402 *fastgetcwd = \&_epoc_cwd;
403 *fastcwd = \&_epoc_cwd;
404 *abs_path = \&fast_abs_path;
405 }
4aecb5b5 406 elsif ($^O eq 'MacOS') {
407 *getcwd = \&cwd;
408 *fastgetcwd = \&cwd;
409 *fastcwd = \&cwd;
410 *abs_path = \&fast_abs_path;
411 }
55497cff 412}
4633a7c4 413
414# package main; eval join('',<DATA>) || die $@; # quick test
415
a0d0e21e 4161;
417
4633a7c4 418__END__
419BEGIN { import Cwd qw(:DEFAULT chdir); }
420print join("\n", cwd, getcwd, fastcwd, "");
421chdir('..');
422print join("\n", cwd, getcwd, fastcwd, "");
423print "$ENV{PWD}\n";