7 my $xs_version = $VERSION;
10 our @ISA = qw/ Exporter /;
11 our @EXPORT = qw(cwd getcwd fastcwd fastgetcwd);
12 push @EXPORT, qw(getdcwd) if $^O eq 'MSWin32';
13 our @EXPORT_OK = qw(chdir abs_path fast_abs_path realpath fast_realpath);
15 # sys_cwd may keep the builtin command
17 # All the functionality of this module may provided by builtins,
18 # there is no sense to process the rest of the file.
19 # The best choice may be to have this in BEGIN, but how to return from BEGIN?
24 *cwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd;
29 *fast_abs_path = \&sys_abspath if defined &sys_abspath;
30 *abs_path = \&fast_abs_path;
31 *realpath = \&fast_abs_path;
32 *fast_realpath = \&fast_abs_path;
37 # Need to look up the feature settings on VMS. The preferred way is to use the
38 # VMS::Feature module, but that may not be available to dual life modules.
43 if (eval { local $SIG{__DIE__};
45 pop @INC if $INC[-1] eq '.';
46 require VMS::Feature; }) {
52 # Need to look up the UNIX report mode. This may become a dynamic mode
56 if ($use_vms_feature) {
57 $unix_rpt = VMS::Feature::current("filename_unix_report");
59 my $env_unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
60 $unix_rpt = $env_unix_rpt =~ /^[ET1]/i;
65 # Need to look up the EFS character set mode. This may become a dynamic
69 if ($use_vms_feature) {
70 $efs = VMS::Feature::current("efs_charset");
72 my $env_efs = $ENV{'DECC$EFS_CHARSET'} || '';
73 $efs = $env_efs =~ /^[ET1]/i;
79 # If loading the XS stuff doesn't work, we can fall back to pure perl
80 if(! defined &getcwd && defined &DynaLoader::boot_DynaLoader) { # skipped on miniperl
82 XSLoader::load( __PACKAGE__, $xs_version);
85 # Big nasty table of function aliases
92 fastcwd => '_vms_cwd',
93 fastgetcwd => '_vms_cwd',
94 abs_path => '_vms_abs_path',
95 fast_abs_path => '_vms_abs_path',
100 # We assume that &_NT_cwd is defined as an XSUB or in the core.
103 fastcwd => '_NT_cwd',
104 fastgetcwd => '_NT_cwd',
105 abs_path => 'fast_abs_path',
106 realpath => 'fast_abs_path',
112 getcwd => '_dos_cwd',
113 fastgetcwd => '_dos_cwd',
114 fastcwd => '_dos_cwd',
115 abs_path => 'fast_abs_path',
118 # QNX4. QNX6 has a $os of 'nto'.
122 getcwd => '_qnx_cwd',
123 fastgetcwd => '_qnx_cwd',
124 fastcwd => '_qnx_cwd',
125 abs_path => '_qnx_abs_path',
126 fast_abs_path => '_qnx_abs_path',
134 abs_path => 'fast_abs_path',
135 realpath => 'fast_abs_path',
140 getcwd => '_backtick_pwd',
141 fastgetcwd => '_backtick_pwd',
142 fastcwd => '_backtick_pwd',
143 abs_path => 'fast_abs_path',
147 $METHOD_MAP{NT} = $METHOD_MAP{MSWin32};
150 # Find the pwd command in the expected locations. We assume these
151 # are safe. This prevents _backtick_pwd() consulting $ENV{PATH}
152 # so everything works under taint mode.
154 if($^O ne 'MSWin32') {
155 foreach my $try ('/bin/pwd',
157 '/QOpenSys/bin/pwd', # OS/400 PASE.
166 # Android has a built-in pwd. Using $pwd_cmd will DTRT if
167 # this perl was compiled with -Dd_useshellcmds, which is the
168 # default for Android, but the block below is needed for the
169 # miniperl running on the host when cross-compiling, and
170 # potentially for native builds with -Ud_useshellcmds.
171 if ($^O =~ /android/) {
172 # If targetsh is executable, then we're either a full
173 # perl, or a miniperl for a native build.
174 if (-x $Config::Config{targetsh}) {
175 $pwd_cmd = "$Config::Config{targetsh} -c pwd"
178 my $sh = $Config::Config{sh} || (-x '/system/bin/sh' ? '/system/bin/sh' : 'sh');
179 $pwd_cmd = "$sh -c pwd"
183 my $found_pwd_cmd = defined($pwd_cmd);
185 # Isn't this wrong? _backtick_pwd() will fail if someone has
186 # pwd in their path but it is not /bin/pwd or /usr/bin/pwd?
187 # See [perl #16774]. --jhi
192 sub _carp { require Carp; Carp::carp(@_) }
193 sub _croak { require Carp; Carp::croak(@_) }
195 # The 'natural and safe form' for UNIX (pwd may be setuid root)
198 # Localize %ENV entries in a way that won't create new hash keys.
199 # Under AmigaOS we don't want to localize as it stops perl from
200 # finding 'sh' in the PATH.
201 my @localize = grep exists $ENV{$_}, qw(PATH IFS CDPATH ENV BASH_ENV) if $^O ne "amigaos";
202 local @ENV{@localize} if @localize;
204 my $cwd = `$pwd_cmd`;
205 # Belt-and-suspenders in case someone said "undef $/".
207 # `pwd` may fail e.g. if the disk is full
208 chomp($cwd) if defined $cwd;
212 # Since some ports may predefine cwd internally (e.g., NT)
213 # we take care not to override an existing definition for cwd().
215 unless ($METHOD_MAP{$^O}{cwd} or defined &cwd) {
216 # The pwd command is not available in some chroot(2)'ed environments
217 my $sep = $Config::Config{path_sep} || ':';
218 my $os = $^O; # Protect $^O from tainting
221 # Try again to find a pwd, this time searching the whole PATH.
222 if (defined $ENV{PATH} and $os ne 'MSWin32') { # no pwd on Windows
223 my @candidates = split($sep, $ENV{PATH});
224 while (!$found_pwd_cmd and @candidates) {
225 my $candidate = shift @candidates;
226 $found_pwd_cmd = 1 if -x "$candidate/pwd";
232 *cwd = \&_backtick_pwd;
239 if ($^O eq 'cygwin') {
240 # We need to make sure cwd() is called with no args, because it's
241 # got an arg-less prototype and will die if args are present.
243 my $orig_cwd = \&cwd;
244 *cwd = sub { &$orig_cwd() }
248 # set a reasonable (and very safe) default for fastgetcwd, in case it
249 # isn't redefined later (20001212 rspier)
252 # A non-XS version of getcwd() - also used to bootstrap the perl build
253 # process, when miniperl is running and no XS loading happens.
261 # Usage: $cwd = &fastcwd;
263 # This is a faster version of getcwd. It's also more dangerous because
264 # you might chdir out of a directory that you can't chdir back into.
267 my($odev, $oino, $cdev, $cino, $tdev, $tino);
271 my($orig_cdev, $orig_cino) = stat('.');
272 ($cdev, $cino) = ($orig_cdev, $orig_cino);
275 ($odev, $oino) = ($cdev, $cino);
276 CORE::chdir('..') || return undef;
277 ($cdev, $cino) = stat('.');
278 last if $odev == $cdev && $oino == $cino;
279 opendir(DIR, '.') || return undef;
281 $direntry = readdir(DIR);
282 last unless defined $direntry;
283 next if $direntry eq '.';
284 next if $direntry eq '..';
286 ($tdev, $tino) = lstat($direntry);
287 last unless $tdev != $odev || $tino != $oino;
290 return undef unless defined $direntry; # should never happen
291 unshift(@path, $direntry);
293 $path = '/' . join('/', @path);
294 if ($^O eq 'apollo') { $path = "/".$path; }
295 # At this point $path may be tainted (if tainting) and chdir would fail.
296 # Untaint it then check that we landed where we started.
297 $path =~ /^(.*)\z/s # untaint
298 && CORE::chdir($1) or return undef;
299 ($cdev, $cino) = stat('.');
300 die "Unstable directory path, current directory changed unexpectedly"
301 if $cdev != $orig_cdev || $cino != $orig_cino;
304 if (not defined &fastcwd) { *fastcwd = \&fastcwd_ }
307 # Keeps track of current working directory in PWD environment var
315 if ($ENV{'PWD'} and $^O ne 'os2' and $^O ne 'dos' and $^O ne 'MSWin32') {
316 my($dd,$di) = stat('.');
317 my($pd,$pi) = stat($ENV{'PWD'});
318 if (!defined $dd or !defined $pd or $di != $pi or $dd != $pd) {
324 $wd = Win32::GetFullPathName($wd) if $^O eq 'MSWin32';
327 # Strip an automounter prefix (where /tmp_mnt/foo/bar == /foo/bar)
328 if ($^O ne 'MSWin32' and $ENV{'PWD'} =~ m|(/[^/]+(/[^/]+/[^/]+))(.*)|s) {
329 my($pd,$pi) = stat($2);
330 my($dd,$di) = stat($1);
331 if (defined $pd and defined $dd and $di == $pi and $dd == $pd) {
339 my $newdir = @_ ? shift : ''; # allow for no arg (chdir to HOME dir)
340 if ($^O eq "cygwin") {
341 $newdir =~ s|\A///+|//|;
342 $newdir =~ s|(?<=[^/])//+|/|g;
344 elsif ($^O ne 'MSWin32') {
345 $newdir =~ s|///*|/|g;
347 chdir_init() unless $chdir_init;
349 if ($^O eq 'MSWin32') {
350 # get the full path name *before* the chdir()
351 $newpwd = Win32::GetFullPathName($newdir);
354 return 0 unless CORE::chdir $newdir;
357 return $ENV{'PWD'} = $ENV{'DEFAULT'}
359 elsif ($^O eq 'MSWin32') {
360 $ENV{'PWD'} = $newpwd;
364 if (ref $newdir eq 'GLOB') { # in case a file/dir handle is passed in
366 } elsif ($newdir =~ m#^/#s) {
367 $ENV{'PWD'} = $newdir;
369 my @curdir = split(m#/#,$ENV{'PWD'});
370 @curdir = ('') unless @curdir;
372 foreach $component (split(m#/#, $newdir)) {
373 next if $component eq '.';
374 pop(@curdir),next if $component eq '..';
375 push(@curdir,$component);
377 $ENV{'PWD'} = join('/',@curdir) || '/';
385 my $start = @_ ? shift : '.';
386 my($dotdots, $cwd, @pst, @cst, $dir, @tst);
388 unless (@cst = stat( $start ))
394 # Make sure we can be invoked on plain files, not just directories.
395 # NOTE that this routine assumes that '/' is the only directory separator.
397 my ($dir, $file) = $start =~ m{^(.*)/(.+)$}
398 or return cwd() . '/' . $start;
400 # Can't use "-l _" here, because the previous stat was a stat(), not an lstat().
402 my $link_target = readlink($start);
403 die "Can't resolve link $start: $!" unless defined $link_target;
406 $link_target = $dir . '/' . $link_target
407 unless File::Spec->file_name_is_absolute($link_target);
409 return abs_path($link_target);
412 return $dir ? abs_path($dir) . "/$file" : "/$file";
422 unless (opendir(PARENT, $dotdots))
424 # probably a permissions issue. Try the native command.
426 return File::Spec->rel2abs( $start, _backtick_pwd() );
428 unless (@cst = stat($dotdots))
435 if ($pst[0] == $cst[0] && $pst[1] == $cst[1])
443 unless (defined ($dir = readdir(PARENT)))
447 $! = Errno::ENOENT();
450 $tst[0] = $pst[0]+1 unless (@tst = lstat("$dotdots/$dir"))
452 while ($dir eq '.' || $dir eq '..' || $tst[0] != $pst[0] ||
455 $cwd = (defined $dir ? "$dir" : "" ) . "/$cwd" ;
457 } while (defined $dir);
458 chop($cwd) unless $cwd eq '/'; # drop the trailing /
465 local $ENV{PWD} = $ENV{PWD} || ''; # Guard against clobberage
468 my $path = @_ ? shift : ($Curdir ||= File::Spec->curdir);
470 # Detaint else we'll explode in taint mode. This is safe because
471 # we're not doing anything dangerous with it.
472 ($path) = $path =~ /(.*)/s;
473 ($cwd) = $cwd =~ /(.*)/s;
476 _croak("$path: No such file or directory");
480 # Make sure we can be invoked on plain files, not just directories.
482 my ($vol, $dir, $file) = File::Spec->splitpath($path);
483 return File::Spec->catfile($cwd, $path) unless length $dir;
486 my $link_target = readlink($path);
487 die "Can't resolve link $path: $!" unless defined $link_target;
489 $link_target = File::Spec->catpath($vol, $dir, $link_target)
490 unless File::Spec->file_name_is_absolute($link_target);
492 return fast_abs_path($link_target);
495 return $dir eq File::Spec->rootdir
496 ? File::Spec->catpath($vol, $dir, $file)
497 : fast_abs_path(File::Spec->catpath($vol, $dir, '')) . '/' . $file;
500 if (!CORE::chdir($path)) {
501 _croak("Cannot chdir to $path: $!");
503 my $realpath = getcwd();
504 if (! ((-d $cwd) && (CORE::chdir($cwd)))) {
505 _croak("Cannot chdir back to $cwd: $!");
510 # added function alias to follow principle of least surprise
511 # based on previous aliasing. --tchrist 27-Jan-00
512 *fast_realpath = \&fast_abs_path;
515 # --- PORTING SECTION ---
517 # VMS: $ENV{'DEFAULT'} points to default directory at all times
518 # 06-Mar-1996 Charles Bailey bailey@newman.upenn.edu
519 # Note: Use of Cwd::chdir() causes the logical name PWD to be defined
520 # in the process logical name table as the default device and directory
521 # seen by Perl. This may not be the same as the default device
522 # and directory seen by DCL after Perl exits, since the effects
523 # the CRTL chdir() function persist only until Perl exits.
526 return $ENV{'DEFAULT'};
530 return $ENV{'DEFAULT'} unless @_;
534 my $unix_rpt = _vms_unix_rpt;
536 if (defined &VMS::Filespec::vmsrealpath) {
540 $path_unix = 1 if ($path =~ m#(?<=\^)/#);
541 $path_unix = 1 if ($path =~ /^\.\.?$/);
542 $path_vms = 1 if ($path =~ m#[\[<\]]#);
543 $path_vms = 1 if ($path =~ /^--?$/);
545 my $unix_mode = $path_unix;
547 # In case of a tie, the Unix report mode decides.
548 if ($path_vms == $path_unix) {
549 $unix_mode = $unix_rpt;
551 $unix_mode = 0 if $path_vms;
557 return VMS::Filespec::unixrealpath($path);
562 my $new_path = VMS::Filespec::vmsrealpath($path);
564 # Perl expects directories to be in directory format
565 $new_path = VMS::Filespec::pathify($new_path) if -d $path;
569 # Fallback to older algorithm if correct ones are not
573 my $link_target = readlink($path);
574 die "Can't resolve link $path: $!" unless defined $link_target;
576 return _vms_abs_path($link_target);
579 # may need to turn foo.dir into [.foo]
580 my $pathified = VMS::Filespec::pathify($path);
581 $path = $pathified if defined $pathified;
583 return VMS::Filespec::rmsexpand($path);
587 my $pwd = `cmd /c cd`;
594 sub _win32_cwd_simple {
604 $pwd = Win32::GetCwd();
610 *_NT_cwd = defined &Win32::GetCwd ? \&_win32_cwd : \&_win32_cwd_simple;
614 if (!defined &Dos::GetCwd) {
615 chomp($pwd = `command /c cd`);
618 $pwd = Dos::GetCwd();
625 local $ENV{PATH} = '';
626 local $ENV{CDPATH} = '';
627 local $ENV{ENV} = '';
628 my $pwd = `/usr/bin/fullpath -t`;
635 local $ENV{PATH} = '';
636 local $ENV{CDPATH} = '';
637 local $ENV{ENV} = '';
638 my $path = @_ ? shift : '.';
641 defined( open(REALPATH, '-|') || exec '/usr/bin/fullpath', '-t', $path ) or
642 die "Can't open /usr/bin/fullpath: $!";
643 my $realpath = <REALPATH>;
649 # Now that all the base-level functions are set up, alias the
650 # user-level functions to the right places
652 if (exists $METHOD_MAP{$^O}) {
653 my $map = $METHOD_MAP{$^O};
654 foreach my $name (keys %$map) {
655 local $^W = 0; # assignments trigger 'subroutine redefined' warning
657 *{$name} = \&{$map->{$name}};
661 # In case the XS version doesn't load.
662 *abs_path = \&_perl_abs_path unless defined &abs_path;
663 *getcwd = \&_perl_getcwd unless defined &getcwd;
665 # added function alias for those of us more
666 # used to the libc function. --tchrist 27-Jan-00
667 *realpath = \&abs_path;
674 Cwd - get pathname of current working directory
682 my $abs_path = abs_path($file);
686 This module provides functions for determining the pathname of the
687 current working directory. It is recommended that getcwd (or another
688 *cwd() function) be used in I<all> code to ensure portability.
690 By default, it exports the functions cwd(), getcwd(), fastcwd(), and
691 fastgetcwd() (and, on Win32, getdcwd()) into the caller's namespace.
694 =head2 getcwd and friends
696 Each of these functions are called without arguments and return the
697 absolute path of the current working directory.
705 Returns the current working directory. On error returns C<undef>,
706 with C<$!> set to indicate the error.
708 Exposes the POSIX function getcwd(3) or re-implements it if it's not
715 The cwd() is the most natural form for the current architecture. For
716 most systems it is identical to `pwd` (but without the trailing line
723 A more dangerous version of getcwd(), but potentially faster.
725 It might conceivably chdir() you out of a directory that it can't
726 chdir() you back into. If fastcwd encounters a problem it will return
727 undef but will probably leave you in a different directory. For a
728 measure of extra security, if everything appears to have worked, the
729 fastcwd() function will check that it leaves you in the same directory
730 that it started in. If it has changed it will C<die> with the message
731 "Unstable directory path, current directory changed
732 unexpectedly". That should never happen.
736 my $cwd = fastgetcwd();
738 The fastgetcwd() function is provided as a synonym for cwd().
743 my $cwd = getdcwd('C:');
745 The getdcwd() function is also provided on Win32 to get the current working
746 directory on the specified drive, since Windows maintains a separate current
747 working directory for each drive. If no drive is specified then the current
750 This function simply calls the Microsoft C library _getdcwd() function.
755 =head2 abs_path and friends
757 These functions are exported only on request. They each take a single
758 argument and return the absolute pathname for it. If no argument is
759 given they'll use the current working directory.
765 my $abs_path = abs_path($file);
767 Uses the same algorithm as getcwd(). Symbolic links and relative-path
768 components ("." and "..") are resolved to return the canonical
769 pathname, just like realpath(3). On error returns C<undef>, with C<$!>
770 set to indicate the error.
774 my $abs_path = realpath($file);
776 A synonym for abs_path().
780 my $abs_path = fast_abs_path($file);
782 A more dangerous, but potentially faster version of abs_path.
788 If you ask to override your chdir() built-in function,
792 then your PWD environment variable will be kept up to date. Note that
793 it will only be kept up to date if all packages which use chdir import
803 Since the path separators are different on some operating systems ('/'
804 on Unix, ':' on MacPerl, etc...) we recommend you use the File::Spec
805 modules wherever portability is a concern.
809 Actually, on Mac OS, the C<getcwd()>, C<fastgetcwd()> and C<fastcwd()>
810 functions are all aliases for the C<cwd()> function, which, on Mac OS,
811 calls `pwd`. Likewise, the C<abs_path()> function is an alias for
818 Originally by the perl5-porters.
820 Maintained by Ken Williams <KWILLIAMS@cpan.org>
824 Copyright (c) 2004 by the Perl 5 Porters. All rights reserved.
826 This program is free software; you can redistribute it and/or modify
827 it under the same terms as Perl itself.
829 Portions of the C code in this library are copyright (c) 1994 by the
830 Regents of the University of California. All rights reserved. The
831 license on this code is compatible with the licensing of the rest of
832 the distribution - please see the source code in F<Cwd.xs> for the