7 @EXPORT = qw(getcwd fastcwd);
8 @EXPORT_OK = qw(chdir);
11 # VMS: $ENV{'DEFAULT'} points to default directory at all times
12 # 08-Dec-1994 Charles Bailey bailey@genetics.upenn.edu
13 # Note: Use of Cwd::getcwd() or Cwd::chdir() (but not Cwd::fastcwd())
14 # causes the logical name PWD to be defined in the process
15 # logical name table as the default device and directory
16 # seen by Perl. This may not be the same as the default device
17 # and directory seen by DCL after Perl exits, since the effects
18 # the CRTL chdir() function persist only until Perl exits.
20 # By Brandon S. Allbery
22 # Usage: $cwd = getcwd();
26 if($Config{'osname'} eq 'VMS') { return $ENV{'PWD'} = $ENV{'DEFAULT'} }
28 my($dotdots, $cwd, @pst, @cst, $dir, @tst);
30 unless (@cst = stat('.'))
39 $dotdots .= '/' if $dotdots;
42 unless (opendir(PARENT, $dotdots))
44 warn "opendir($dotdots): $!";
47 unless (@cst = stat($dotdots))
49 warn "stat($dotdots): $!";
53 if ($pst[0] == $cst[0] && $pst[1] == $cst[1])
61 unless (defined ($dir = readdir(PARENT)))
63 warn "readdir($dotdots): $!";
67 unless (@tst = lstat("$dotdots/$dir"))
69 warn "lstat($dotdots/$dir): $!";
74 while ($dir eq '.' || $dir eq '..' || $tst[0] != $pst[0] ||
80 chop($cwd); # drop the trailing /
88 # Usage: $cwd = &fastcwd;
90 # This is a faster version of getcwd. It's also more dangerous because
91 # you might chdir out of a directory that you can't chdir back into.
94 if($Config{'osname'} eq 'VMS') { return $ENV{'DEFAULT'} }
96 my($odev, $oino, $cdev, $cino, $tdev, $tino);
100 ($cdev, $cino) = stat('.');
103 ($odev, $oino) = ($cdev, $cino);
105 ($cdev, $cino) = stat('.');
106 last if $odev == $cdev && $oino == $cino;
109 $direntry = readdir(DIR);
110 next if $direntry eq '.';
111 next if $direntry eq '..';
113 last unless defined $direntry;
114 ($tdev, $tino) = lstat($direntry);
115 last unless $tdev != $odev || $tino != $oino;
118 unshift(@path, $direntry);
120 chdir($path = '/' . join('/', @path));
125 # keeps track of current working directory in PWD environment var
127 # $RCSfile: pwd.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:11 $
139 my($dd,$di) = stat('.');
140 my($pd,$pi) = stat($ENV{'PWD'});
141 if (!defined $dd or !defined $pd or $di != $pi or $dd != $pd) {
142 chop($ENV{'PWD'} = `pwd`);
146 chop($ENV{'PWD'} = `pwd`);
148 if ($ENV{'PWD'} =~ m|(/[^/]+(/[^/]+/[^/]+))(.*)|) {
149 my($pd,$pi) = stat($2);
150 my($dd,$di) = stat($1);
151 if (defined $pd and defined $dd and $di == $pi and $dd == $pd) {
160 $newdir =~ s|/{2,}|/|g;
161 chdir_init() unless $chdir_init;
162 return 0 unless (CORE::chdir $newdir);
163 if ($Config{'osname'} eq 'VMS') { return $ENV{PWD} = $ENV{DEFAULT} }
165 if ($newdir =~ m#^/#) {
166 $ENV{'PWD'} = $newdir;
168 my(@curdir) = split(m#/#,$ENV{'PWD'});
169 @curdir = '' unless @curdir;
170 foreach $component (split(m#/#, $newdir)) {
171 next if $component eq '.';
172 pop(@curdir),next if $component eq '..';
173 push(@curdir,$component);
175 $ENV{'PWD'} = join('/',@curdir) || '/';