This is patch.2b1e to perl5.002beta1. This is simply
[p5sagit/p5-mst-13.2.git] / lib / Cwd.pm
1 package Cwd;
2 require 5.000;
3 require Exporter;
4 require Config;
5
6 # Use osname for portability switches (doubled to cheaply avoid -w warning)
7 my $osname = $Config::Config{'osname'} || $Config::Config{'osname'};
8
9
10 =head1 NAME
11
12 getcwd - get pathname of current working directory
13
14 =head1 SYNOPSIS
15
16     use Cwd;
17     $dir = cwd;
18
19     use Cwd;
20     $dir = getcwd;
21
22     use Cwd;
23     $dir = fastgetcwd;
24
25     use Cwd 'chdir';
26     chdir "/tmp";
27     print $ENV{'PWD'};
28
29 =head1 DESCRIPTION
30
31 The getcwd() function re-implements the getcwd(3) (or getwd(3)) functions
32 in Perl.
33
34 The fastgetcwd() function looks the same as getcwd(), but runs faster.
35 It's also more dangerous because you might conceivably chdir() out of a
36 directory that you can't chdir() back into.
37
38 The cwd() function looks the same as getcwd and fastgetcwd but is
39 implemented using the most natural and safe form for the current
40 architecture. For most systems it is identical to `pwd` (but without
41 the trailing line terminator). It is recommended that cwd (or another
42 *cwd() function) is used in I<all> code to ensure portability.
43
44 If you ask to override your chdir() built-in function, then your PWD
45 environment variable will be kept up to date.  (See
46 L<perlsub/Overriding builtin functions>.) Note that it will only be
47 kept up to date it all packages which use chdir import it from Cwd.
48
49 =cut
50
51 @ISA = qw(Exporter);
52 @EXPORT = qw(cwd getcwd fastcwd);
53 @EXPORT_OK = qw(chdir);
54
55 # use strict;
56
57 sub _backtick_pwd {  # The 'natural and safe form' for UNIX (pwd may be setuid root)
58     my $cwd;
59     chop($cwd = `pwd`);
60     $cwd;
61
62
63 # Since some ports may predefine cwd internally (e.g., NT)
64 # we take care not to override an existing definition for cwd().
65
66 *cwd = \&_backtick_pwd unless defined &cwd;
67
68
69 # By Brandon S. Allbery
70 #
71 # Usage: $cwd = getcwd();
72
73 sub getcwd
74 {
75     my($dotdots, $cwd, @pst, @cst, $dir, @tst);
76
77     unless (@cst = stat('.'))
78     {
79         warn "stat(.): $!";
80         return '';
81     }
82     $cwd = '';
83     $dotdots = '';
84     do
85     {
86         $dotdots .= '/' if $dotdots;
87         $dotdots .= '..';
88         @pst = @cst;
89         unless (opendir(PARENT, $dotdots))
90         {
91             warn "opendir($dotdots): $!";
92             return '';
93         }
94         unless (@cst = stat($dotdots))
95         {
96             warn "stat($dotdots): $!";
97             closedir(PARENT);
98             return '';
99         }
100         if ($pst[0] == $cst[0] && $pst[1] == $cst[1])
101         {
102             $dir = '';
103         }
104         else
105         {
106             do
107             {
108                 unless (defined ($dir = readdir(PARENT)))
109                 {
110                     warn "readdir($dotdots): $!";
111                     closedir(PARENT);
112                     return '';
113                 }
114                 unless (@tst = lstat("$dotdots/$dir"))
115                 {
116                     warn "lstat($dotdots/$dir): $!";
117                     closedir(PARENT);
118                     return '';
119                 }
120             }
121             while ($dir eq '.' || $dir eq '..' || $tst[0] != $pst[0] ||
122                    $tst[1] != $pst[1]);
123         }
124         $cwd = "$dir/$cwd";
125         closedir(PARENT);
126     } while ($dir);
127     chop($cwd); # drop the trailing /
128     $cwd;
129 }
130
131
132
133 # By John Bazik
134 #
135 # Usage: $cwd = &fastcwd;
136 #
137 # This is a faster version of getcwd.  It's also more dangerous because
138 # you might chdir out of a directory that you can't chdir back into.
139
140 sub fastcwd {
141     my($odev, $oino, $cdev, $cino, $tdev, $tino);
142     my(@path, $path);
143     local(*DIR);
144
145     ($cdev, $cino) = stat('.');
146     for (;;) {
147         my $direntry;
148         ($odev, $oino) = ($cdev, $cino);
149         chdir('..');
150         ($cdev, $cino) = stat('.');
151         last if $odev == $cdev && $oino == $cino;
152         opendir(DIR, '.');
153         for (;;) {
154             $direntry = readdir(DIR);
155             next if $direntry eq '.';
156             next if $direntry eq '..';
157
158             last unless defined $direntry;
159             ($tdev, $tino) = lstat($direntry);
160             last unless $tdev != $odev || $tino != $oino;
161         }
162         closedir(DIR);
163         unshift(@path, $direntry);
164     }
165     chdir($path = '/' . join('/', @path));
166     $path;
167 }
168
169
170 # Keeps track of current working directory in PWD environment var
171 # Usage:
172 #       use Cwd 'chdir';
173 #       chdir $newdir;
174
175 my $chdir_init = 0;
176
177 sub chdir_init {
178     if ($ENV{'PWD'} and $osname ne 'os2') {
179         my($dd,$di) = stat('.');
180         my($pd,$pi) = stat($ENV{'PWD'});
181         if (!defined $dd or !defined $pd or $di != $pi or $dd != $pd) {
182             $ENV{'PWD'} = cwd();
183         }
184     }
185     else {
186         $ENV{'PWD'} = cwd();
187     }
188     # Strip an automounter prefix (where /tmp_mnt/foo/bar == /foo/bar)
189     if ($ENV{'PWD'} =~ m|(/[^/]+(/[^/]+/[^/]+))(.*)|) {
190         my($pd,$pi) = stat($2);
191         my($dd,$di) = stat($1);
192         if (defined $pd and defined $dd and $di == $pi and $dd == $pd) {
193             $ENV{'PWD'}="$2$3";
194         }
195     }
196     $chdir_init = 1;
197 }
198
199 sub chdir {
200     my $newdir = shift || '';   # allow for no arg (chdir to HOME dir)
201     $newdir =~ s|///*|/|g;
202     chdir_init() unless $chdir_init;
203     return 0 unless CORE::chdir $newdir;
204     if ($osname eq 'VMS') { return $ENV{'PWD'} = $ENV{'DEFAULT'} }
205
206     if ($newdir =~ m#^/#) {
207         $ENV{'PWD'} = $newdir;
208     } else {
209         my @curdir = split(m#/#,$ENV{'PWD'});
210         @curdir = ('') unless @curdir;
211         my $component;
212         foreach $component (split(m#/#, $newdir)) {
213             next if $component eq '.';
214             pop(@curdir),next if $component eq '..';
215             push(@curdir,$component);
216         }
217         $ENV{'PWD'} = join('/',@curdir) || '/';
218     }
219     1;
220 }
221
222
223 # --- PORTING SECTION ---
224
225 # VMS: $ENV{'DEFAULT'} points to default directory at all times
226 # 08-Dec-1994  Charles Bailey  bailey@genetics.upenn.edu
227 # Note: Use of Cwd::getcwd() or Cwd::chdir() (but not Cwd::fastcwd())
228 #   causes the logical name PWD to be defined in the process 
229 #   logical name table as the default device and directory 
230 #   seen by Perl. This may not be the same as the default device 
231 #   and directory seen by DCL after Perl exits, since the effects
232 #   the CRTL chdir() function persist only until Perl exits.
233 # This does not apply to other systems (where only chdir() sets PWD).
234
235 sub _vms_cwd {
236     return $ENV{'DEFAULT'}
237 }
238 sub _vms_pwd {
239     return $ENV{'PWD'} = $ENV{'DEFAULT'}
240 }
241 sub _os2_cwd {
242     $ENV{'PWD'} = `cmd /c cd`;
243     chop $ENV{'PWD'};
244     $ENV{'PWD'} =~ s:\\:/:g ;
245     return $ENV{'PWD'};
246 }
247
248 if ($osname eq 'VMS') {
249
250     *cwd        = \&_vms_pwd;
251     *getcwd     = \&_vms_pwd;
252     *fastgetcwd = \&_vms_cwd;
253 }
254 elsif ($osname eq 'NT') {
255
256     *getcwd     = \&cwd;
257     *fastgetcwd = \&cwd;
258 }
259 elsif ($osname eq 'os2') {
260     *cwd     = \&_os2_cwd;
261     *getcwd     = \&_os2_cwd;
262     *fastgetcwd = \&_os2_cwd;
263     *fastcwd = \&_os2_cwd;
264 }
265
266 # package main; eval join('',<DATA>) || die $@; # quick test
267
268 1;
269
270 __END__
271 BEGIN { import Cwd qw(:DEFAULT chdir); }
272 print join("\n", cwd, getcwd, fastcwd, "");
273 chdir('..');
274 print join("\n", cwd, getcwd, fastcwd, "");
275 print "$ENV{PWD}\n";