8 getcwd - get pathname of current working directory
27 The getcwd() function re-implements the getcwd(3) (or getwd(3)) functions
30 The fastcwd() function looks the same as getcwd(), but runs faster.
31 It's also more dangerous because you might conceivably chdir() out of a
32 directory that you can't chdir() back into.
34 The cwd() function looks the same as getcwd and fastgetcwd but is
35 implemented using the most natural and safe form for the current
36 architecture. For most systems it is identical to `pwd` (but without
37 the trailing line terminator). It is recommended that cwd (or another
38 *cwd() function) is used in I<all> code to ensure portability.
40 If you ask to override your chdir() built-in function, then your PWD
41 environment variable will be kept up to date. (See
42 L<perlsub/Overriding Builtin Functions>.) Note that it will only be
43 kept up to date if all packages which use chdir import it from Cwd.
48 @EXPORT = qw(cwd getcwd fastcwd fastgetcwd);
49 @EXPORT_OK = qw(chdir abs_path fast_abspath);
53 # The 'natural and safe form' for UNIX (pwd may be setuid root)
60 # Since some ports may predefine cwd internally (e.g., NT)
61 # we take care not to override an existing definition for cwd().
63 *cwd = \&_backtick_pwd unless defined &cwd;
66 # By Brandon S. Allbery
68 # Usage: $cwd = getcwd();
72 my($dotdots, $cwd, @pst, @cst, $dir, @tst);
74 unless (@cst = stat('.'))
83 $dotdots .= '/' if $dotdots;
86 unless (opendir(PARENT, $dotdots))
88 warn "opendir($dotdots): $!";
91 unless (@cst = stat($dotdots))
93 warn "stat($dotdots): $!";
97 if ($pst[0] == $cst[0] && $pst[1] == $cst[1])
105 unless (defined ($dir = readdir(PARENT)))
107 warn "readdir($dotdots): $!";
111 unless (@tst = lstat("$dotdots/$dir"))
113 # warn "lstat($dotdots/$dir): $!";
114 # Just because you can't lstat this directory
115 # doesn't mean you'll never find the right one.
120 while ($dir eq '.' || $dir eq '..' || $tst[0] != $pst[0] ||
126 chop($cwd) unless $cwd eq '/'; # drop the trailing /
134 # Usage: $cwd = &fastcwd;
136 # This is a faster version of getcwd. It's also more dangerous because
137 # you might chdir out of a directory that you can't chdir back into.
140 my($odev, $oino, $cdev, $cino, $tdev, $tino);
144 ($cdev, $cino) = stat('.');
147 ($odev, $oino) = ($cdev, $cino);
149 ($cdev, $cino) = stat('.');
150 last if $odev == $cdev && $oino == $cino;
153 $direntry = readdir(DIR);
154 next if $direntry eq '.';
155 next if $direntry eq '..';
157 last unless defined $direntry;
158 ($tdev, $tino) = lstat($direntry);
159 last unless $tdev != $odev || $tino != $oino;
162 unshift(@path, $direntry);
164 chdir($path = '/' . join('/', @path));
169 # Keeps track of current working directory in PWD environment var
177 if ($ENV{'PWD'} and $^O ne 'os2' and $^O ne 'msdos') {
178 my($dd,$di) = stat('.');
179 my($pd,$pi) = stat($ENV{'PWD'});
180 if (!defined $dd or !defined $pd or $di != $pi or $dd != $pd) {
187 # Strip an automounter prefix (where /tmp_mnt/foo/bar == /foo/bar)
188 if ($ENV{'PWD'} =~ m|(/[^/]+(/[^/]+/[^/]+))(.*)|) {
189 my($pd,$pi) = stat($2);
190 my($dd,$di) = stat($1);
191 if (defined $pd and defined $dd and $di == $pi and $dd == $pd) {
199 my $newdir = shift || ''; # allow for no arg (chdir to HOME dir)
200 $newdir =~ s|///*|/|g;
201 chdir_init() unless $chdir_init;
202 return 0 unless CORE::chdir $newdir;
203 if ($^O eq 'VMS') { return $ENV{'PWD'} = $ENV{'DEFAULT'} }
205 if ($newdir =~ m#^/#) {
206 $ENV{'PWD'} = $newdir;
208 my @curdir = split(m#/#,$ENV{'PWD'});
209 @curdir = ('') unless @curdir;
211 foreach $component (split(m#/#, $newdir)) {
212 next if $component eq '.';
213 pop(@curdir),next if $component eq '..';
214 push(@curdir,$component);
216 $ENV{'PWD'} = join('/',@curdir) || '/';
221 # Taken from Cwd.pm It is really getcwd with an optional
222 # parameter instead of '.'
227 my $start = shift || '.';
228 my($dotdots, $cwd, @pst, @cst, $dir, @tst);
230 unless (@cst = stat( $start ))
232 carp "stat($start): $!";
241 unless (opendir(PARENT, $dotdots))
243 carp "opendir($dotdots): $!";
246 unless (@cst = stat($dotdots))
248 carp "stat($dotdots): $!";
252 if ($pst[0] == $cst[0] && $pst[1] == $cst[1])
260 unless (defined ($dir = readdir(PARENT)))
262 carp "readdir($dotdots): $!";
266 $tst[0] = $pst[0]+1 unless (@tst = lstat("$dotdots/$dir"))
268 while ($dir eq '.' || $dir eq '..' || $tst[0] != $pst[0] ||
274 chop($cwd); # drop the trailing /
281 my $path = shift || '.';
282 chdir($path) || croak "Cannot chdir to $path:$!";
283 my $realpath = getcwd();
284 chdir($cwd) || croak "Cannot chdir back to $cwd:$!";
289 # --- PORTING SECTION ---
291 # VMS: $ENV{'DEFAULT'} points to default directory at all times
292 # 06-Mar-1996 Charles Bailey bailey@genetics.upenn.edu
293 # Note: Use of Cwd::chdir() causes the logical name PWD to be defined
294 # in the process logical name table as the default device and directory
295 # seen by Perl. This may not be the same as the default device
296 # and directory seen by DCL after Perl exits, since the effects
297 # the CRTL chdir() function persist only until Perl exits.
300 return $ENV{'DEFAULT'}
304 $ENV{'PWD'} = `cmd /c cd`;
306 $ENV{'PWD'} =~ s:\\:/:g ;
310 *_NT_cwd = \&_os2_cwd unless defined &_NT_cwd;
313 $ENV{'PWD'} = `command /c cd`;
315 $ENV{'PWD'} =~ s:\\:/:g ;
320 local $^W = 0; # assignments trigger 'subroutine redefined' warning
324 *getcwd = \&_vms_cwd;
325 *fastcwd = \&_vms_cwd;
326 *fastgetcwd = \&_vms_cwd;
327 *abs_path = \&fast_abspath;
329 elsif ($^O eq 'NT' or $^O eq 'MSWin32') {
330 # We assume that &_NT_cwd is defined as an XSUB or in the core.
333 *fastcwd = \&_NT_cwd;
334 *fastgetcwd = \&_NT_cwd;
335 *abs_path = \&fast_abspath;
337 elsif ($^O eq 'os2') {
338 # sys_cwd may keep the builtin command
339 *cwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd;
340 *getcwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd;
341 *fastgetcwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd;
342 *fastcwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd;
343 *abs_path = \&fast_abspath;
345 elsif ($^O eq 'msdos') {
347 *getcwd = \&_msdos_cwd;
348 *fastgetcwd = \&_msdos_cwd;
349 *fastcwd = \&_msdos_cwd;
350 *abs_path = \&fast_abspath;
354 # package main; eval join('',<DATA>) || die $@; # quick test
359 BEGIN { import Cwd qw(:DEFAULT chdir); }
360 print join("\n", cwd, getcwd, fastcwd, "");
362 print join("\n", cwd, getcwd, fastcwd, "");