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