This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
avoid a prototype warning assigning \&getcwd to *cwd
[perl5.git] / dist / PathTools / Cwd.pm
CommitLineData
a0d0e21e 1package Cwd;
b060a406 2use strict;
a9939470 3use Exporter;
99f36a73 4
b9a5a78f 5
87c4c246 6our $VERSION = '3.81';
4a4ab19c 7my $xs_version = $VERSION;
4f642d62 8$VERSION =~ tr/_//d;
96e4d5b1 9
1a58b39a
N
10our @ISA = qw/ Exporter /;
11our @EXPORT = qw(cwd getcwd fastcwd fastgetcwd);
09122b95 12push @EXPORT, qw(getdcwd) if $^O eq 'MSWin32';
1a58b39a 13our @EXPORT_OK = qw(chdir abs_path fast_abs_path realpath fast_realpath);
a0d0e21e 14
f5f423e4
IZ
15# sys_cwd may keep the builtin command
16
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?
20
a9939470 21if ($^O eq 'os2') {
f5f423e4 22 local $^W = 0;
a9939470
NC
23
24 *cwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd;
25 *getcwd = \&cwd;
26 *fastgetcwd = \&cwd;
27 *fastcwd = \&cwd;
28
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;
33
f5f423e4
IZ
34 return 1;
35}
36
53e80d0b
JM
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.
39
40my $use_vms_feature;
41BEGIN {
42 if ($^O eq 'VMS') {
8901ddee
TC
43 if (eval { local $SIG{__DIE__};
44 local @INC = @INC;
45 pop @INC if $INC[-1] eq '.';
46 require VMS::Feature; }) {
53e80d0b
JM
47 $use_vms_feature = 1;
48 }
49 }
50}
51
52# Need to look up the UNIX report mode. This may become a dynamic mode
53# in the future.
54sub _vms_unix_rpt {
55 my $unix_rpt;
56 if ($use_vms_feature) {
57 $unix_rpt = VMS::Feature::current("filename_unix_report");
58 } else {
59 my $env_unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
a7884731 60 $unix_rpt = $env_unix_rpt =~ /^[ET1]/i;
53e80d0b
JM
61 }
62 return $unix_rpt;
63}
64
65# Need to look up the EFS character set mode. This may become a dynamic
66# mode in the future.
67sub _vms_efs {
68 my $efs;
69 if ($use_vms_feature) {
70 $efs = VMS::Feature::current("efs_charset");
71 } else {
72 my $env_efs = $ENV{'DECC$EFS_CHARSET'} || '';
a7884731 73 $efs = $env_efs =~ /^[ET1]/i;
53e80d0b
JM
74 }
75 return $efs;
76}
77
78
b04f6d36 79# If loading the XS stuff doesn't work, we can fall back to pure perl
b9a5a78f
N
80if(! defined &getcwd && defined &DynaLoader::boot_DynaLoader) { # skipped on miniperl
81 require XSLoader;
82 XSLoader::load( __PACKAGE__, $xs_version);
07f43755 83}
4633a7c4 84
09122b95
RGS
85# Big nasty table of function aliases
86my %METHOD_MAP =
87 (
88 VMS =>
89 {
90 cwd => '_vms_cwd',
91 getcwd => '_vms_cwd',
92 fastcwd => '_vms_cwd',
93 fastgetcwd => '_vms_cwd',
94 abs_path => '_vms_abs_path',
95 fast_abs_path => '_vms_abs_path',
96 },
97
98 MSWin32 =>
99 {
100 # We assume that &_NT_cwd is defined as an XSUB or in the core.
101 cwd => '_NT_cwd',
102 getcwd => '_NT_cwd',
103 fastcwd => '_NT_cwd',
104 fastgetcwd => '_NT_cwd',
105 abs_path => 'fast_abs_path',
106 realpath => 'fast_abs_path',
107 },
108
109 dos =>
110 {
111 cwd => '_dos_cwd',
112 getcwd => '_dos_cwd',
113 fastgetcwd => '_dos_cwd',
114 fastcwd => '_dos_cwd',
115 abs_path => 'fast_abs_path',
116 },
117
58ccccf6 118 # QNX4. QNX6 has a $os of 'nto'.
09122b95
RGS
119 qnx =>
120 {
121 cwd => '_qnx_cwd',
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',
127 },
128
129 cygwin =>
130 {
131 getcwd => 'cwd',
132 fastgetcwd => 'cwd',
133 fastcwd => 'cwd',
134 abs_path => 'fast_abs_path',
135 realpath => 'fast_abs_path',
136 },
137
027b7a30
AB
138 amigaos =>
139 {
140 getcwd => '_backtick_pwd',
141 fastgetcwd => '_backtick_pwd',
142 fastcwd => '_backtick_pwd',
143 abs_path => 'fast_abs_path',
144 }
09122b95
RGS
145 );
146
147$METHOD_MAP{NT} = $METHOD_MAP{MSWin32};
09122b95 148
96e4d5b1 149
3547aa9a
MS
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.
153my $pwd_cmd;
f4eedc6b
DD
154if($^O ne 'MSWin32') {
155 foreach my $try ('/bin/pwd',
156 '/usr/bin/pwd',
157 '/QOpenSys/bin/pwd', # OS/400 PASE.
158 ) {
159 if( -x $try ) {
160 $pwd_cmd = $try;
161 last;
162 }
3547aa9a
MS
163 }
164}
b2fc9074
BF
165
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.
171if ($^O =~ /android/) {
172 # If targetsh is executable, then we're either a full
173 # perl, or a miniperl for a native build.
ac9c4ce8 174 if ( exists($Config::Config{targetsh}) && -x $Config::Config{targetsh}) {
b2fc9074
BF
175 $pwd_cmd = "$Config::Config{targetsh} -c pwd"
176 }
177 else {
e7d73da9
BF
178 my $sh = $Config::Config{sh} || (-x '/system/bin/sh' ? '/system/bin/sh' : 'sh');
179 $pwd_cmd = "$sh -c pwd"
b2fc9074
BF
180 }
181}
182
fa52125f 183my $found_pwd_cmd = defined($pwd_cmd);
3547aa9a 184
a9939470
NC
185# Lazy-load Carp
186sub _carp { require Carp; Carp::carp(@_) }
187sub _croak { require Carp; Carp::croak(@_) }
188
3547aa9a 189# The 'natural and safe form' for UNIX (pwd may be setuid root)
8b88ae92 190sub _backtick_pwd {
027b7a30
AB
191
192 # Localize %ENV entries in a way that won't create new hash keys.
193 # Under AmigaOS we don't want to localize as it stops perl from
194 # finding 'sh' in the PATH.
195 my @localize = grep exists $ENV{$_}, qw(PATH IFS CDPATH ENV BASH_ENV) if $^O ne "amigaos";
196 local @ENV{@localize} if @localize;
f6342b4b 197
3547aa9a 198 my $cwd = `$pwd_cmd`;
ac3b20cb 199 # Belt-and-suspenders in case someone said "undef $/".
5cf6da5f 200 local $/ = "\n";
ac3b20cb 201 # `pwd` may fail e.g. if the disk is full
7e03f963 202 chomp($cwd) if defined $cwd;
4633a7c4 203 $cwd;
8b88ae92 204}
4633a7c4
LW
205
206# Since some ports may predefine cwd internally (e.g., NT)
207# we take care not to override an existing definition for cwd().
208
09122b95 209unless ($METHOD_MAP{$^O}{cwd} or defined &cwd) {
e229c0fe 210 if( $found_pwd_cmd )
73b801a6 211 {
ea54c8bd
EC
212 *cwd = \&_backtick_pwd;
213 }
214 else {
44e4059b
TC
215 # getcwd() might have an empty prototype
216 *cwd = sub { getcwd(); };
ea54c8bd
EC
217 }
218}
a0d0e21e 219
23bb49fa
SP
220if ($^O eq 'cygwin') {
221 # We need to make sure cwd() is called with no args, because it's
222 # got an arg-less prototype and will die if args are present.
223 local $^W = 0;
224 my $orig_cwd = \&cwd;
225 *cwd = sub { &$orig_cwd() }
226}
227
228
1f4f94f5
RS
229# set a reasonable (and very safe) default for fastgetcwd, in case it
230# isn't redefined later (20001212 rspier)
231*fastgetcwd = \&cwd;
748a9306 232
c47834cd
RGS
233# A non-XS version of getcwd() - also used to bootstrap the perl build
234# process, when miniperl is running and no XS loading happens.
a7a23d71
NC
235sub _perl_getcwd
236{
237 abs_path('.');
238}
239
a0c9c202
JH
240# By John Bazik
241#
242# Usage: $cwd = &fastcwd;
243#
244# This is a faster version of getcwd. It's also more dangerous because
245# you might chdir out of a directory that you can't chdir back into.
246
99f36a73 247sub fastcwd_ {
a0c9c202
JH
248 my($odev, $oino, $cdev, $cino, $tdev, $tino);
249 my(@path, $path);
250 local(*DIR);
251
252 my($orig_cdev, $orig_cino) = stat('.');
253 ($cdev, $cino) = ($orig_cdev, $orig_cino);
254 for (;;) {
255 my $direntry;
256 ($odev, $oino) = ($cdev, $cino);
257 CORE::chdir('..') || return undef;
258 ($cdev, $cino) = stat('.');
73a0855d 259 last if $odev == $cdev && $oino eq $cino;
a0c9c202
JH
260 opendir(DIR, '.') || return undef;
261 for (;;) {
262 $direntry = readdir(DIR);
263 last unless defined $direntry;
264 next if $direntry eq '.';
265 next if $direntry eq '..';
266
267 ($tdev, $tino) = lstat($direntry);
73a0855d 268 last unless $tdev != $odev || $tino ne $oino;
a0c9c202
JH
269 }
270 closedir(DIR);
271 return undef unless defined $direntry; # should never happen
272 unshift(@path, $direntry);
273 }
274 $path = '/' . join('/', @path);
275 if ($^O eq 'apollo') { $path = "/".$path; }
276 # At this point $path may be tainted (if tainting) and chdir would fail.
248785eb
RGS
277 # Untaint it then check that we landed where we started.
278 $path =~ /^(.*)\z/s # untaint
279 && CORE::chdir($1) or return undef;
a0c9c202
JH
280 ($cdev, $cino) = stat('.');
281 die "Unstable directory path, current directory changed unexpectedly"
73a0855d 282 if $cdev != $orig_cdev || $cino ne $orig_cino;
a0c9c202
JH
283 $path;
284}
99f36a73 285if (not defined &fastcwd) { *fastcwd = \&fastcwd_ }
a0c9c202
JH
286
287
4633a7c4 288# Keeps track of current working directory in PWD environment var
a0d0e21e
LW
289# Usage:
290# use Cwd 'chdir';
291# chdir $newdir;
292
4633a7c4 293my $chdir_init = 0;
a0d0e21e 294
4633a7c4 295sub chdir_init {
3b8e3443 296 if ($ENV{'PWD'} and $^O ne 'os2' and $^O ne 'dos' and $^O ne 'MSWin32') {
a0d0e21e
LW
297 my($dd,$di) = stat('.');
298 my($pd,$pi) = stat($ENV{'PWD'});
73a0855d 299 if (!defined $dd or !defined $pd or $di ne $pi or $dd != $pd) {
4633a7c4 300 $ENV{'PWD'} = cwd();
a0d0e21e
LW
301 }
302 }
303 else {
3b8e3443
GS
304 my $wd = cwd();
305 $wd = Win32::GetFullPathName($wd) if $^O eq 'MSWin32';
306 $ENV{'PWD'} = $wd;
a0d0e21e 307 }
4633a7c4 308 # Strip an automounter prefix (where /tmp_mnt/foo/bar == /foo/bar)
3b8e3443 309 if ($^O ne 'MSWin32' and $ENV{'PWD'} =~ m|(/[^/]+(/[^/]+/[^/]+))(.*)|s) {
a0d0e21e
LW
310 my($pd,$pi) = stat($2);
311 my($dd,$di) = stat($1);
73a0855d 312 if (defined $pd and defined $dd and $di ne $pi and $dd == $pd) {
a0d0e21e
LW
313 $ENV{'PWD'}="$2$3";
314 }
315 }
316 $chdir_init = 1;
317}
318
319sub chdir {
22978713 320 my $newdir = @_ ? shift : ''; # allow for no arg (chdir to HOME dir)
faa5d6ec
TC
321 if ($^O eq "cygwin") {
322 $newdir =~ s|\A///+|//|;
323 $newdir =~ s|(?<=[^/])//+|/|g;
324 }
325 elsif ($^O ne 'MSWin32') {
326 $newdir =~ s|///*|/|g;
327 }
a0d0e21e 328 chdir_init() unless $chdir_init;
4ffa1610
JH
329 my $newpwd;
330 if ($^O eq 'MSWin32') {
331 # get the full path name *before* the chdir()
332 $newpwd = Win32::GetFullPathName($newdir);
333 }
334
4633a7c4 335 return 0 unless CORE::chdir $newdir;
4ffa1610 336
3b8e3443
GS
337 if ($^O eq 'VMS') {
338 return $ENV{'PWD'} = $ENV{'DEFAULT'}
339 }
340 elsif ($^O eq 'MSWin32') {
4ffa1610 341 $ENV{'PWD'} = $newpwd;
3b8e3443
GS
342 return 1;
343 }
748a9306 344
e9475de8
SP
345 if (ref $newdir eq 'GLOB') { # in case a file/dir handle is passed in
346 $ENV{'PWD'} = cwd();
347 } elsif ($newdir =~ m#^/#s) {
a0d0e21e 348 $ENV{'PWD'} = $newdir;
4633a7c4
LW
349 } else {
350 my @curdir = split(m#/#,$ENV{'PWD'});
351 @curdir = ('') unless @curdir;
352 my $component;
a0d0e21e
LW
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
LW
361}
362
a0c9c202 363
99f36a73 364sub _perl_abs_path
a0c9c202
JH
365{
366 my $start = @_ ? shift : '.';
367 my($dotdots, $cwd, @pst, @cst, $dir, @tst);
368
369 unless (@cst = stat( $start ))
370 {
d2e38af7 371 return undef;
a0c9c202 372 }
09122b95
RGS
373
374 unless (-d _) {
375 # Make sure we can be invoked on plain files, not just directories.
376 # NOTE that this routine assumes that '/' is the only directory separator.
377
378 my ($dir, $file) = $start =~ m{^(.*)/(.+)$}
379 or return cwd() . '/' . $start;
380
275e8705
RGS
381 # Can't use "-l _" here, because the previous stat was a stat(), not an lstat().
382 if (-l $start) {
09122b95
RGS
383 my $link_target = readlink($start);
384 die "Can't resolve link $start: $!" unless defined $link_target;
385
386 require File::Spec;
387 $link_target = $dir . '/' . $link_target
388 unless File::Spec->file_name_is_absolute($link_target);
389
390 return abs_path($link_target);
391 }
392
99f36a73 393 return $dir ? abs_path($dir) . "/$file" : "/$file";
09122b95
RGS
394 }
395
a0c9c202
JH
396 $cwd = '';
397 $dotdots = $start;
398 do
399 {
400 $dotdots .= '/..';
401 @pst = @cst;
a25ef67d 402 local *PARENT;
a0c9c202
JH
403 unless (opendir(PARENT, $dotdots))
404 {
815d526f 405 return undef;
a0c9c202
JH
406 }
407 unless (@cst = stat($dotdots))
408 {
d2e38af7 409 my $e = $!;
a0c9c202 410 closedir(PARENT);
d2e38af7
Z
411 $! = $e;
412 return undef;
a0c9c202 413 }
73a0855d 414 if ($pst[0] == $cst[0] && $pst[1] eq $cst[1])
a0c9c202
JH
415 {
416 $dir = undef;
417 }
418 else
419 {
420 do
421 {
422 unless (defined ($dir = readdir(PARENT)))
423 {
a0c9c202 424 closedir(PARENT);
d2e38af7
Z
425 require Errno;
426 $! = Errno::ENOENT();
427 return undef;
a0c9c202
JH
428 }
429 $tst[0] = $pst[0]+1 unless (@tst = lstat("$dotdots/$dir"))
430 }
431 while ($dir eq '.' || $dir eq '..' || $tst[0] != $pst[0] ||
73a0855d 432 $tst[1] ne $pst[1]);
a0c9c202
JH
433 }
434 $cwd = (defined $dir ? "$dir" : "" ) . "/$cwd" ;
435 closedir(PARENT);
436 } while (defined $dir);
437 chop($cwd) unless $cwd eq '/'; # drop the trailing /
438 $cwd;
439}
440
441
3ee63918 442my $Curdir;
96e4d5b1 443sub fast_abs_path {
99f36a73 444 local $ENV{PWD} = $ENV{PWD} || ''; # Guard against clobberage
96e4d5b1 445 my $cwd = getcwd();
a97021b9 446 defined $cwd or return undef;
4d6b4052 447 require File::Spec;
3ee63918
MS
448 my $path = @_ ? shift : ($Curdir ||= File::Spec->curdir);
449
450 # Detaint else we'll explode in taint mode. This is safe because
451 # we're not doing anything dangerous with it.
9f28c638
JB
452 ($path) = $path =~ /(.*)/s;
453 ($cwd) = $cwd =~ /(.*)/s;
3ee63918 454
09122b95 455 unless (-e $path) {
a97021b9
Z
456 require Errno;
457 $! = Errno::ENOENT();
458 return undef;
09122b95
RGS
459 }
460
461 unless (-d _) {
462 # Make sure we can be invoked on plain files, not just directories.
463
464 my ($vol, $dir, $file) = File::Spec->splitpath($path);
465 return File::Spec->catfile($cwd, $path) unless length $dir;
466
467 if (-l $path) {
468 my $link_target = readlink($path);
a97021b9 469 defined $link_target or return undef;
09122b95
RGS
470
471 $link_target = File::Spec->catpath($vol, $dir, $link_target)
472 unless File::Spec->file_name_is_absolute($link_target);
473
474 return fast_abs_path($link_target);
475 }
476
d6802e43 477 return $dir eq File::Spec->rootdir
99f36a73
RGS
478 ? File::Spec->catpath($vol, $dir, $file)
479 : fast_abs_path(File::Spec->catpath($vol, $dir, '')) . '/' . $file;
09122b95
RGS
480 }
481
e2ba406b 482 if (!CORE::chdir($path)) {
a97021b9 483 return undef;
e2ba406b 484 }
96e4d5b1 485 my $realpath = getcwd();
e2ba406b 486 if (! ((-d $cwd) && (CORE::chdir($cwd)))) {
a9939470 487 _croak("Cannot chdir back to $cwd: $!");
e2ba406b 488 }
96e4d5b1 489 $realpath;
8b88ae92
NIS
490}
491
e4c51978
GS
492# added function alias to follow principle of least surprise
493# based on previous aliasing. --tchrist 27-Jan-00
494*fast_realpath = \&fast_abs_path;
495
4633a7c4
LW
496
497# --- PORTING SECTION ---
498
499# VMS: $ENV{'DEFAULT'} points to default directory at all times
bd3fa61c 500# 06-Mar-1996 Charles Bailey bailey@newman.upenn.edu
c6538b72 501# Note: Use of Cwd::chdir() causes the logical name PWD to be defined
8b88ae92
NIS
502# in the process logical name table as the default device and directory
503# seen by Perl. This may not be the same as the default device
4633a7c4
LW
504# and directory seen by DCL after Perl exits, since the effects
505# the CRTL chdir() function persist only until Perl exits.
4633a7c4
LW
506
507sub _vms_cwd {
96e4d5b1 508 return $ENV{'DEFAULT'};
509}
510
511sub _vms_abs_path {
512 return $ENV{'DEFAULT'} unless @_;
61729915 513 my $path = shift;
9d7d9729 514
53e80d0b
JM
515 my $efs = _vms_efs;
516 my $unix_rpt = _vms_unix_rpt;
517
518 if (defined &VMS::Filespec::vmsrealpath) {
519 my $path_unix = 0;
520 my $path_vms = 0;
521
522 $path_unix = 1 if ($path =~ m#(?<=\^)/#);
523 $path_unix = 1 if ($path =~ /^\.\.?$/);
524 $path_vms = 1 if ($path =~ m#[\[<\]]#);
525 $path_vms = 1 if ($path =~ /^--?$/);
526
527 my $unix_mode = $path_unix;
528 if ($efs) {
529 # In case of a tie, the Unix report mode decides.
530 if ($path_vms == $path_unix) {
531 $unix_mode = $unix_rpt;
532 } else {
533 $unix_mode = 0 if $path_vms;
534 }
535 }
9d7d9729 536
53e80d0b 537 if ($unix_mode) {
bf7c0a3d 538 # Unix format
53e80d0b 539 return VMS::Filespec::unixrealpath($path);
bf7c0a3d
SP
540 }
541
542 # VMS format
543
53e80d0b 544 my $new_path = VMS::Filespec::vmsrealpath($path);
bf7c0a3d
SP
545
546 # Perl expects directories to be in directory format
547 $new_path = VMS::Filespec::pathify($new_path) if -d $path;
548 return $new_path;
549 }
550
551 # Fallback to older algorithm if correct ones are not
552 # available.
553
53e80d0b
JM
554 if (-l $path) {
555 my $link_target = readlink($path);
556 die "Can't resolve link $path: $!" unless defined $link_target;
557
558 return _vms_abs_path($link_target);
559 }
560
61729915
CB
561 # may need to turn foo.dir into [.foo]
562 my $pathified = VMS::Filespec::pathify($path);
563 $path = $pathified if defined $pathified;
564
96e4d5b1 565 return VMS::Filespec::rmsexpand($path);
4633a7c4 566}
68dc0745 567
4633a7c4 568sub _os2_cwd {
3480fbaa
DD
569 my $pwd = `cmd /c cd`;
570 chomp $pwd;
571 $pwd =~ s:\\:/:g ;
572 $ENV{'PWD'} = $pwd;
573 return $pwd;
4633a7c4
LW
574}
575
8440aeb0 576sub _win32_cwd_simple {
3480fbaa
DD
577 my $pwd = `cd`;
578 chomp $pwd;
579 $pwd =~ s:\\:/:g ;
580 $ENV{'PWD'} = $pwd;
581 return $pwd;
8440aeb0 582}
583
96e4d5b1 584sub _win32_cwd {
3480fbaa 585 my $pwd;
8f1332ed 586 $pwd = Win32::GetCwd();
3480fbaa
DD
587 $pwd =~ s:\\:/:g ;
588 $ENV{'PWD'} = $pwd;
589 return $pwd;
96e4d5b1 590}
591
8440aeb0 592*_NT_cwd = defined &Win32::GetCwd ? \&_win32_cwd : \&_win32_cwd_simple;
68dc0745 593
39e571d4 594sub _dos_cwd {
3480fbaa 595 my $pwd;
39e571d4 596 if (!defined &Dos::GetCwd) {
3480fbaa
DD
597 chomp($pwd = `command /c cd`);
598 $pwd =~ s:\\:/:g ;
39e571d4 599 } else {
3480fbaa 600 $pwd = Dos::GetCwd();
39e571d4 601 }
3480fbaa
DD
602 $ENV{'PWD'} = $pwd;
603 return $pwd;
55497cff 604}
605
7fbf1995 606sub _qnx_cwd {
35b807ef
NA
607 local $ENV{PATH} = '';
608 local $ENV{CDPATH} = '';
609 local $ENV{ENV} = '';
3480fbaa
DD
610 my $pwd = `/usr/bin/fullpath -t`;
611 chomp $pwd;
612 $ENV{'PWD'} = $pwd;
613 return $pwd;
7fbf1995
NA
614}
615
616sub _qnx_abs_path {
35b807ef
NA
617 local $ENV{PATH} = '';
618 local $ENV{CDPATH} = '';
619 local $ENV{ENV} = '';
fa921dc6 620 my $path = @_ ? shift : '.';
39741d73
MS
621 local *REALPATH;
622
99f36a73 623 defined( open(REALPATH, '-|') || exec '/usr/bin/fullpath', '-t', $path ) or
39741d73
MS
624 die "Can't open /usr/bin/fullpath: $!";
625 my $realpath = <REALPATH>;
626 close REALPATH;
627 chomp $realpath;
7fbf1995
NA
628 return $realpath;
629}
630
09122b95
RGS
631# Now that all the base-level functions are set up, alias the
632# user-level functions to the right places
633
634if (exists $METHOD_MAP{$^O}) {
635 my $map = $METHOD_MAP{$^O};
636 foreach my $name (keys %$map) {
99f36a73 637 local $^W = 0; # assignments trigger 'subroutine redefined' warning
09122b95
RGS
638 no strict 'refs';
639 *{$name} = \&{$map->{$name}};
640 }
55497cff 641}
4633a7c4 642
0a4d1779
TC
643# built-in from 5.30
644*getcwd = \&Internals::getcwd
645 if !defined &getcwd && defined &Internals::getcwd;
646
99f36a73
RGS
647# In case the XS version doesn't load.
648*abs_path = \&_perl_abs_path unless defined &abs_path;
a7a23d71 649*getcwd = \&_perl_getcwd unless defined &getcwd;
99f36a73
RGS
650
651# added function alias for those of us more
652# used to the libc function. --tchrist 27-Jan-00
653*realpath = \&abs_path;
4633a7c4 654
a0d0e21e 6551;
f4eedc6b
DD
656__END__
657
658=head1 NAME
659
660Cwd - get pathname of current working directory
661
662=head1 SYNOPSIS
663
664 use Cwd;
665 my $dir = getcwd;
666
667 use Cwd 'abs_path';
668 my $abs_path = abs_path($file);
669
670=head1 DESCRIPTION
671
672This module provides functions for determining the pathname of the
673current working directory. It is recommended that getcwd (or another
674*cwd() function) be used in I<all> code to ensure portability.
675
676By default, it exports the functions cwd(), getcwd(), fastcwd(), and
677fastgetcwd() (and, on Win32, getdcwd()) into the caller's namespace.
678
679
680=head2 getcwd and friends
681
682Each of these functions are called without arguments and return the
683absolute path of the current working directory.
684
685=over 4
686
687=item getcwd
688
689 my $cwd = getcwd();
690
d2e38af7
Z
691Returns the current working directory. On error returns C<undef>,
692with C<$!> set to indicate the error.
f4eedc6b
DD
693
694Exposes the POSIX function getcwd(3) or re-implements it if it's not
695available.
696
697=item cwd
698
699 my $cwd = cwd();
700
701The cwd() is the most natural form for the current architecture. For
702most systems it is identical to `pwd` (but without the trailing line
703terminator).
704
705=item fastcwd
706
707 my $cwd = fastcwd();
708
709A more dangerous version of getcwd(), but potentially faster.
710
711It might conceivably chdir() you out of a directory that it can't
712chdir() you back into. If fastcwd encounters a problem it will return
713undef but will probably leave you in a different directory. For a
714measure of extra security, if everything appears to have worked, the
715fastcwd() function will check that it leaves you in the same directory
716that it started in. If it has changed it will C<die> with the message
717"Unstable directory path, current directory changed
718unexpectedly". That should never happen.
719
720=item fastgetcwd
721
722 my $cwd = fastgetcwd();
723
724The fastgetcwd() function is provided as a synonym for cwd().
725
726=item getdcwd
727
728 my $cwd = getdcwd();
729 my $cwd = getdcwd('C:');
730
731The getdcwd() function is also provided on Win32 to get the current working
732directory on the specified drive, since Windows maintains a separate current
733working directory for each drive. If no drive is specified then the current
734drive is assumed.
735
736This function simply calls the Microsoft C library _getdcwd() function.
737
738=back
739
740
741=head2 abs_path and friends
742
743These functions are exported only on request. They each take a single
744argument and return the absolute pathname for it. If no argument is
745given they'll use the current working directory.
746
747=over 4
748
749=item abs_path
750
751 my $abs_path = abs_path($file);
752
753Uses the same algorithm as getcwd(). Symbolic links and relative-path
754components ("." and "..") are resolved to return the canonical
d2e38af7
Z
755pathname, just like realpath(3). On error returns C<undef>, with C<$!>
756set to indicate the error.
f4eedc6b
DD
757
758=item realpath
759
760 my $abs_path = realpath($file);
761
762A synonym for abs_path().
763
764=item fast_abs_path
765
766 my $abs_path = fast_abs_path($file);
767
768A more dangerous, but potentially faster version of abs_path.
769
770=back
771
772=head2 $ENV{PWD}
773
774If you ask to override your chdir() built-in function,
775
776 use Cwd qw(chdir);
777
778then your PWD environment variable will be kept up to date. Note that
779it will only be kept up to date if all packages which use chdir import
780it from Cwd.
781
782
783=head1 NOTES
784
785=over 4
786
787=item *
788
789Since the path separators are different on some operating systems ('/'
790on Unix, ':' on MacPerl, etc...) we recommend you use the File::Spec
791modules wherever portability is a concern.
792
793=item *
794
795Actually, on Mac OS, the C<getcwd()>, C<fastgetcwd()> and C<fastcwd()>
796functions are all aliases for the C<cwd()> function, which, on Mac OS,
797calls `pwd`. Likewise, the C<abs_path()> function is an alias for
798C<fast_abs_path()>.
799
800=back
801
802=head1 AUTHOR
803
804Originally by the perl5-porters.
805
806Maintained by Ken Williams <KWILLIAMS@cpan.org>
807
808=head1 COPYRIGHT
809
810Copyright (c) 2004 by the Perl 5 Porters. All rights reserved.
811
812This program is free software; you can redistribute it and/or modify
813it under the same terms as Perl itself.
814
815Portions of the C code in this library are copyright (c) 1994 by the
816Regents of the University of California. All rights reserved. The
817license on this code is compatible with the licensing of the rest of
818the distribution - please see the source code in F<Cwd.xs> for the
819details.
820
821=head1 SEE ALSO
822
823L<File::chdir>
824
825=cut