6 Cwd - get pathname of current working directory
14 my $abs_path = abs_path($file);
18 This module provides functions for determining the pathname of the
19 current working directory. It is recommended that getcwd (or another
20 *cwd() function) be used in I<all> code to ensure portability.
22 By default, it exports the functions cwd(), getcwd(), fastcwd(), and
23 fastgetcwd() into the caller's namespace.
26 =head2 getcwd and friends
28 Each of these functions are called without arguments and return the
29 absolute path of the current working directory.
37 Returns the current working directory.
39 Re-implements the getcwd(3) (or getwd(3)) functions in Perl.
45 The cwd() is the most natural form for the current architecture. For
46 most systems it is identical to `pwd` (but without the trailing line
49 Unfortunately, cwd() tends to break if called under taint mode.
55 A more dangerous version of getcwd(), but potentially faster.
57 It might conceivably chdir() you out of a directory that it can't
58 chdir() you back into. If fastcwd encounters a problem it will return
59 undef but will probably leave you in a different directory. For a
60 measure of extra security, if everything appears to have worked, the
61 fastcwd() function will check that it leaves you in the same directory
62 that it started in. If it has changed it will C<die> with the message
63 "Unstable directory path, current directory changed
64 unexpectedly". That should never happen.
68 my $cwd = fastgetcwd();
70 The fastgetcwd() function is provided as a synonym for cwd().
75 =head2 abs_path and friends
77 These functions are exported only on request. They each take a single
78 argument and return the absolute pathname for it.
84 my $abs_path = abs_path($file);
86 Uses the same algorithm as getcwd(). Symbolic links and relative-path
87 components ("." and "..") are resolved to return the canonical
88 pathname, just like realpath(3).
92 my $abs_path = realpath($file);
94 A synonym for abs_path().
98 my $abs_path = abs_path($file);
100 A more dangerous, but potentially faster version of abs_path.
106 If you ask to override your chdir() built-in function,
110 then your PWD environment variable will be kept up to date. Note that
111 it will only be kept up to date if all packages which use chdir import
121 Since the path seperators are different on some operating systems ('/'
122 on Unix, ':' on MacPerl, etc...) we recommend you use the File::Spec
123 modules wherever portability is a concern.
129 Actually, on Mac OS, the C<getcwd()>, C<fastgetcwd()> and C<fastcwd()>
130 functions are all aliases for the C<cwd()> function, which, on Mac OS,
131 calls `pwd`. Likewise, the C<abs_path()> function is an alias for
148 our $VERSION = '2.06';
150 use base qw/ Exporter /;
151 our @EXPORT = qw(cwd getcwd fastcwd fastgetcwd);
152 our @EXPORT_OK = qw(chdir abs_path fast_abs_path realpath fast_realpath);
154 # sys_cwd may keep the builtin command
156 # All the functionality of this module may provided by builtins,
157 # there is no sense to process the rest of the file.
158 # The best choice may be to have this in BEGIN, but how to return from BEGIN?
160 if ($^O eq 'os2' && defined &sys_cwd && defined &sys_abspath) {
166 *abs_path = \&sys_abspath;
167 *fast_abs_path = \&abs_path;
168 *realpath = \&abs_path;
169 *fast_realpath = \&abs_path;
175 XSLoader::load('Cwd');
179 # Find the pwd command in the expected locations. We assume these
180 # are safe. This prevents _backtick_pwd() consulting $ENV{PATH}
181 # so everything works under taint mode.
183 foreach my $try (qw(/bin/pwd /usr/bin/pwd)) {
191 # The 'natural and safe form' for UNIX (pwd may be setuid root)
193 my $cwd = `$pwd_cmd`;
194 # `pwd` may fail e.g. if the disk is full
195 chomp($cwd) if defined $cwd;
199 # Since some ports may predefine cwd internally (e.g., NT)
200 # we take care not to override an existing definition for cwd().
202 unless(defined &cwd) {
203 # The pwd command is not available in some chroot(2)'ed environments
204 if($^O eq 'MacOS' || grep { -x "$_/pwd" } split(':', $ENV{PATH})) {
205 *cwd = \&_backtick_pwd;
212 # set a reasonable (and very safe) default for fastgetcwd, in case it
213 # isn't redefined later (20001212 rspier)
216 # By Brandon S. Allbery
218 # Usage: $cwd = getcwd();
225 # Keeps track of current working directory in PWD environment var
233 if ($ENV{'PWD'} and $^O ne 'os2' and $^O ne 'dos' and $^O ne 'MSWin32') {
234 my($dd,$di) = stat('.');
235 my($pd,$pi) = stat($ENV{'PWD'});
236 if (!defined $dd or !defined $pd or $di != $pi or $dd != $pd) {
242 $wd = Win32::GetFullPathName($wd) if $^O eq 'MSWin32';
245 # Strip an automounter prefix (where /tmp_mnt/foo/bar == /foo/bar)
246 if ($^O ne 'MSWin32' and $ENV{'PWD'} =~ m|(/[^/]+(/[^/]+/[^/]+))(.*)|s) {
247 my($pd,$pi) = stat($2);
248 my($dd,$di) = stat($1);
249 if (defined $pd and defined $dd and $di == $pi and $dd == $pd) {
257 my $newdir = @_ ? shift : ''; # allow for no arg (chdir to HOME dir)
258 $newdir =~ s|///*|/|g unless $^O eq 'MSWin32';
259 chdir_init() unless $chdir_init;
261 if ($^O eq 'MSWin32') {
262 # get the full path name *before* the chdir()
263 $newpwd = Win32::GetFullPathName($newdir);
266 return 0 unless CORE::chdir $newdir;
269 return $ENV{'PWD'} = $ENV{'DEFAULT'}
271 elsif ($^O eq 'MacOS') {
272 return $ENV{'PWD'} = cwd();
274 elsif ($^O eq 'MSWin32') {
275 $ENV{'PWD'} = $newpwd;
279 if ($newdir =~ m#^/#s) {
280 $ENV{'PWD'} = $newdir;
282 my @curdir = split(m#/#,$ENV{'PWD'});
283 @curdir = ('') unless @curdir;
285 foreach $component (split(m#/#, $newdir)) {
286 next if $component eq '.';
287 pop(@curdir),next if $component eq '..';
288 push(@curdir,$component);
290 $ENV{'PWD'} = join('/',@curdir) || '/';
295 # added function alias for those of us more
296 # used to the libc function. --tchrist 27-Jan-00
297 *realpath = \&abs_path;
302 my $path = @_ ? shift : File::Spec->curdir;
303 CORE::chdir($path) || croak "Cannot chdir to $path:$!";
304 my $realpath = getcwd();
305 CORE::chdir($cwd) || croak "Cannot chdir back to $cwd:$!";
309 # added function alias to follow principle of least surprise
310 # based on previous aliasing. --tchrist 27-Jan-00
311 *fast_realpath = \&fast_abs_path;
314 # --- PORTING SECTION ---
316 # VMS: $ENV{'DEFAULT'} points to default directory at all times
317 # 06-Mar-1996 Charles Bailey bailey@newman.upenn.edu
318 # Note: Use of Cwd::chdir() causes the logical name PWD to be defined
319 # in the process logical name table as the default device and directory
320 # seen by Perl. This may not be the same as the default device
321 # and directory seen by DCL after Perl exits, since the effects
322 # the CRTL chdir() function persist only until Perl exits.
325 return $ENV{'DEFAULT'};
329 return $ENV{'DEFAULT'} unless @_;
330 my $path = VMS::Filespec::pathify($_[0]);
331 croak("Invalid path name $_[0]") unless defined $path;
332 return VMS::Filespec::rmsexpand($path);
336 $ENV{'PWD'} = `cmd /c cd`;
338 $ENV{'PWD'} =~ s:\\:/:g ;
343 $ENV{'PWD'} = Win32::GetCwd();
344 $ENV{'PWD'} =~ s:\\:/:g ;
348 *_NT_cwd = \&_win32_cwd if (!defined &_NT_cwd &&
349 defined &Win32::GetCwd);
351 *_NT_cwd = \&_os2_cwd unless defined &_NT_cwd;
354 if (!defined &Dos::GetCwd) {
355 $ENV{'PWD'} = `command /c cd`;
357 $ENV{'PWD'} =~ s:\\:/:g ;
359 $ENV{'PWD'} = Dos::GetCwd();
365 $ENV{'PWD'} = `/usr/bin/fullpath -t`;
371 my $path = @_ ? shift : '.';
372 my $realpath=`/usr/bin/fullpath -t $path`;
378 $ENV{'PWD'} = EPOC::getcwd();
383 no warnings; # assignments trigger 'subroutine redefined' warning
387 *getcwd = \&_vms_cwd;
388 *fastcwd = \&_vms_cwd;
389 *fastgetcwd = \&_vms_cwd;
390 *abs_path = \&_vms_abs_path;
391 *fast_abs_path = \&_vms_abs_path;
393 elsif ($^O eq 'NT' or $^O eq 'MSWin32') {
394 # We assume that &_NT_cwd is defined as an XSUB or in the core.
397 *fastcwd = \&_NT_cwd;
398 *fastgetcwd = \&_NT_cwd;
399 *abs_path = \&fast_abs_path;
401 elsif ($^O eq 'os2') {
402 # sys_cwd may keep the builtin command
403 *cwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd;
407 *abs_path = \&fast_abs_path;
409 elsif ($^O eq 'dos') {
411 *getcwd = \&_dos_cwd;
412 *fastgetcwd = \&_dos_cwd;
413 *fastcwd = \&_dos_cwd;
414 *abs_path = \&fast_abs_path;
416 elsif ($^O =~ m/^(?:qnx|nto)$/ ) {
418 *getcwd = \&_qnx_cwd;
419 *fastgetcwd = \&_qnx_cwd;
420 *fastcwd = \&_qnx_cwd;
421 *abs_path = \&_qnx_abs_path;
422 *fast_abs_path = \&_qnx_abs_path;
424 elsif ($^O eq 'cygwin') {
428 *abs_path = \&fast_abs_path;
430 elsif ($^O eq 'epoc') {
432 *getcwd = \&_epoc_cwd;
433 *fastgetcwd = \&_epoc_cwd;
434 *fastcwd = \&_epoc_cwd;
435 *abs_path = \&fast_abs_path;
437 elsif ($^O eq 'MacOS') {
441 *abs_path = \&fast_abs_path;
445 # package main; eval join('',<DATA>) || die $@; # quick test
450 BEGIN { import Cwd qw(:DEFAULT chdir); }
451 print join("\n", cwd, getcwd, fastcwd, "");
453 print join("\n", cwd, getcwd, fastcwd, "");