7 getcwd - get pathname of current working directory
26 The getcwd() function re-implements the getcwd(3) (or getwd(3)) functions
29 The fastcwd() function looks the same as getcwd(), but runs faster.
30 It's also more dangerous because you might conceivably chdir() out of a
31 directory that you can't chdir() back into.
33 The cwd() function looks the same as getcwd and fastgetcwd but is
34 implemented using the most natural and safe form for the current
35 architecture. For most systems it is identical to `pwd` (but without
36 the trailing line terminator). It is recommended that cwd (or another
37 *cwd() function) is used in I<all> code to ensure portability.
39 If you ask to override your chdir() built-in function, then your PWD
40 environment variable will be kept up to date. (See
41 L<perlsub/Overriding builtin functions>.) Note that it will only be
42 kept up to date if all packages which use chdir import it from Cwd.
47 @EXPORT = qw(cwd getcwd fastcwd fastgetcwd);
48 @EXPORT_OK = qw(chdir);
52 sub _backtick_pwd { # The 'natural and safe form' for UNIX (pwd may be setuid root)
58 # Since some ports may predefine cwd internally (e.g., NT)
59 # we take care not to override an existing definition for cwd().
61 *cwd = \&_backtick_pwd unless defined &cwd;
64 # By Brandon S. Allbery
66 # Usage: $cwd = getcwd();
70 my($dotdots, $cwd, @pst, @cst, $dir, @tst);
72 unless (@cst = stat('.'))
81 $dotdots .= '/' if $dotdots;
84 unless (opendir(PARENT, $dotdots))
86 warn "opendir($dotdots): $!";
89 unless (@cst = stat($dotdots))
91 warn "stat($dotdots): $!";
95 if ($pst[0] == $cst[0] && $pst[1] == $cst[1])
103 unless (defined ($dir = readdir(PARENT)))
105 warn "readdir($dotdots): $!";
109 unless (@tst = lstat("$dotdots/$dir"))
111 warn "lstat($dotdots/$dir): $!";
112 # Just because you can't lstat this directory
113 # doesn't mean you'll never find the right one.
118 while ($dir eq '.' || $dir eq '..' || $tst[0] != $pst[0] ||
124 chop($cwd) unless $cwd eq '/'; # drop the trailing /
132 # Usage: $cwd = &fastcwd;
134 # This is a faster version of getcwd. It's also more dangerous because
135 # you might chdir out of a directory that you can't chdir back into.
138 my($odev, $oino, $cdev, $cino, $tdev, $tino);
142 ($cdev, $cino) = stat('.');
145 ($odev, $oino) = ($cdev, $cino);
147 ($cdev, $cino) = stat('.');
148 last if $odev == $cdev && $oino == $cino;
151 $direntry = readdir(DIR);
152 next if $direntry eq '.';
153 next if $direntry eq '..';
155 last unless defined $direntry;
156 ($tdev, $tino) = lstat($direntry);
157 last unless $tdev != $odev || $tino != $oino;
160 unshift(@path, $direntry);
162 chdir($path = '/' . join('/', @path));
167 # Keeps track of current working directory in PWD environment var
175 if ($ENV{'PWD'} and $^O ne 'os2') {
176 my($dd,$di) = stat('.');
177 my($pd,$pi) = stat($ENV{'PWD'});
178 if (!defined $dd or !defined $pd or $di != $pi or $dd != $pd) {
185 # Strip an automounter prefix (where /tmp_mnt/foo/bar == /foo/bar)
186 if ($ENV{'PWD'} =~ m|(/[^/]+(/[^/]+/[^/]+))(.*)|) {
187 my($pd,$pi) = stat($2);
188 my($dd,$di) = stat($1);
189 if (defined $pd and defined $dd and $di == $pi and $dd == $pd) {
197 my $newdir = shift || ''; # allow for no arg (chdir to HOME dir)
198 $newdir =~ s|///*|/|g;
199 chdir_init() unless $chdir_init;
200 return 0 unless CORE::chdir $newdir;
201 if ($^O eq 'VMS') { return $ENV{'PWD'} = $ENV{'DEFAULT'} }
203 if ($newdir =~ m#^/#) {
204 $ENV{'PWD'} = $newdir;
206 my @curdir = split(m#/#,$ENV{'PWD'});
207 @curdir = ('') unless @curdir;
209 foreach $component (split(m#/#, $newdir)) {
210 next if $component eq '.';
211 pop(@curdir),next if $component eq '..';
212 push(@curdir,$component);
214 $ENV{'PWD'} = join('/',@curdir) || '/';
220 # --- PORTING SECTION ---
222 # VMS: $ENV{'DEFAULT'} points to default directory at all times
223 # 06-Mar-1996 Charles Bailey bailey@genetics.upenn.edu
224 # Note: Use of Cwd::chdir() causes the logical name PWD to be defined
225 # in the process logical name table as the default device and directory
226 # seen by Perl. This may not be the same as the default device
227 # and directory seen by DCL after Perl exits, since the effects
228 # the CRTL chdir() function persist only until Perl exits.
231 return $ENV{'DEFAULT'}
234 $ENV{'PWD'} = `cmd /c cd`;
236 $ENV{'PWD'} =~ s:\\:/:g ;
241 $^W = 0; # assignments trigger 'subroutine redefined' warning
245 *getcwd = \&_vms_cwd;
246 *fastcwd = \&_vms_cwd;
247 *fastgetcwd = \&_vms_cwd;
249 elsif ($^O eq 'NT' or $^O eq 'MSWin32') {
251 # We assume that &_NT_cwd is defined as an XSUB or in the core.
253 *fastcwd = \&_NT_cwd;
254 *fastgetcwd = \&_NT_cwd;
256 elsif ($^O eq 'os2') {
257 # sys_cwd may keep the builtin command
258 *cwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd;
259 *getcwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd;
260 *fastgetcwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd;
261 *fastcwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd;
265 # package main; eval join('',<DATA>) || die $@; # quick test
270 BEGIN { import Cwd qw(:DEFAULT chdir); }
271 print join("\n", cwd, getcwd, fastcwd, "");
273 print join("\n", cwd, getcwd, fastcwd, "");