Commit | Line | Data |
a0d0e21e |
1 | package Cwd; |
2 | require 5.000; |
a0d0e21e |
3 | |
f06db76b |
4 | =head1 NAME |
5 | |
6 | getcwd - get pathname of current working directory |
7 | |
8 | =head1 SYNOPSIS |
9 | |
4633a7c4 |
10 | use Cwd; |
11 | $dir = cwd; |
12 | |
13 | use Cwd; |
14 | $dir = getcwd; |
f06db76b |
15 | |
16 | use Cwd; |
4633a7c4 |
17 | $dir = fastgetcwd; |
f06db76b |
18 | |
19 | use Cwd 'chdir'; |
20 | chdir "/tmp"; |
21 | print $ENV{'PWD'}; |
22 | |
e4c51978 |
23 | use Cwd 'abs_path'; # aka realpath() |
20408e3c |
24 | print abs_path($ENV{'PWD'}); |
25 | |
26 | use Cwd 'fast_abs_path'; |
27 | print fast_abs_path($ENV{'PWD'}); |
28 | |
f06db76b |
29 | =head1 DESCRIPTION |
30 | |
31 | The getcwd() function re-implements the getcwd(3) (or getwd(3)) functions |
4633a7c4 |
32 | in Perl. |
f06db76b |
33 | |
20408e3c |
34 | The abs_path() function takes a single argument and returns the |
e4c51978 |
35 | absolute pathname for that argument. It uses the same algorithm |
36 | as getcwd(). (Actually, getcwd() is abs_path(".")) Symbolic links |
37 | and relative-path components ("." and "..") are resolved to return |
38 | the canonical pathname, just like realpath(3). Also callable as |
39 | realpath(). |
20408e3c |
40 | |
cb1a09d0 |
41 | The fastcwd() function looks the same as getcwd(), but runs faster. |
fb73857a |
42 | It's also more dangerous because it might conceivably chdir() you out |
43 | of a directory that it can't chdir() you back into. If fastcwd |
44 | encounters a problem it will return undef but will probably leave you |
45 | in a different directory. For a measure of extra security, if |
46 | everything appears to have worked, the fastcwd() function will check |
47 | that it leaves you in the same directory that it started in. If it has |
48 | changed it will C<die> with the message "Unstable directory path, |
49 | current directory changed unexpectedly". That should never happen. |
f06db76b |
50 | |
20408e3c |
51 | The fast_abs_path() function looks the same as abs_path(), but runs faster. |
52 | And like fastcwd() is more dangerous. |
53 | |
4633a7c4 |
54 | The cwd() function looks the same as getcwd and fastgetcwd but is |
55 | implemented using the most natural and safe form for the current |
56 | architecture. For most systems it is identical to `pwd` (but without |
fb73857a |
57 | the trailing line terminator). |
58 | |
59 | It is recommended that cwd (or another *cwd() function) is used in |
60 | I<all> code to ensure portability. |
4633a7c4 |
61 | |
62 | If you ask to override your chdir() built-in function, then your PWD |
63 | environment variable will be kept up to date. (See |
55497cff |
64 | L<perlsub/Overriding Builtin Functions>.) Note that it will only be |
1fef88e7 |
65 | kept up to date if all packages which use chdir import it from Cwd. |
4633a7c4 |
66 | |
f06db76b |
67 | =cut |
68 | |
96e4d5b1 |
69 | ## use strict; |
70 | |
71 | use Carp; |
72 | |
e4c51978 |
73 | $VERSION = '2.02'; |
96e4d5b1 |
74 | |
75 | require Exporter; |
a0d0e21e |
76 | @ISA = qw(Exporter); |
e7ae0116 |
77 | @EXPORT = qw(cwd getcwd fastcwd fastgetcwd); |
e4c51978 |
78 | @EXPORT_OK = qw(chdir abs_path fast_abs_path realpath fast_realpath); |
a0d0e21e |
79 | |
4633a7c4 |
80 | |
8b88ae92 |
81 | # The 'natural and safe form' for UNIX (pwd may be setuid root) |
96e4d5b1 |
82 | |
8b88ae92 |
83 | sub _backtick_pwd { |
4633a7c4 |
84 | my $cwd; |
85 | chop($cwd = `pwd`); |
86 | $cwd; |
8b88ae92 |
87 | } |
4633a7c4 |
88 | |
89 | # Since some ports may predefine cwd internally (e.g., NT) |
90 | # we take care not to override an existing definition for cwd(). |
91 | |
92 | *cwd = \&_backtick_pwd unless defined &cwd; |
a0d0e21e |
93 | |
748a9306 |
94 | |
a0d0e21e |
95 | # By Brandon S. Allbery |
96 | # |
97 | # Usage: $cwd = getcwd(); |
98 | |
99 | sub getcwd |
100 | { |
07569ed3 |
101 | abs_path('.'); |
a0d0e21e |
102 | } |
103 | |
a0d0e21e |
104 | # By John Bazik |
105 | # |
106 | # Usage: $cwd = &fastcwd; |
107 | # |
108 | # This is a faster version of getcwd. It's also more dangerous because |
109 | # you might chdir out of a directory that you can't chdir back into. |
fb73857a |
110 | |
111 | # List of metachars taken from do_exec() in doio.c |
112 | my $quoted_shell_meta = quotemeta('$&*(){}[]";\\|?<>~`'."'\n"); |
a0d0e21e |
113 | |
114 | sub fastcwd { |
115 | my($odev, $oino, $cdev, $cino, $tdev, $tino); |
116 | my(@path, $path); |
117 | local(*DIR); |
118 | |
fb73857a |
119 | my($orig_cdev, $orig_cino) = stat('.'); |
120 | ($cdev, $cino) = ($orig_cdev, $orig_cino); |
a0d0e21e |
121 | for (;;) { |
40000a8c |
122 | my $direntry; |
a0d0e21e |
123 | ($odev, $oino) = ($cdev, $cino); |
e79e61e4 |
124 | CORE::chdir('..') || return undef; |
a0d0e21e |
125 | ($cdev, $cino) = stat('.'); |
126 | last if $odev == $cdev && $oino == $cino; |
fb73857a |
127 | opendir(DIR, '.') || return undef; |
a0d0e21e |
128 | for (;;) { |
40000a8c |
129 | $direntry = readdir(DIR); |
fb73857a |
130 | last unless defined $direntry; |
40000a8c |
131 | next if $direntry eq '.'; |
132 | next if $direntry eq '..'; |
a0d0e21e |
133 | |
40000a8c |
134 | ($tdev, $tino) = lstat($direntry); |
a0d0e21e |
135 | last unless $tdev != $odev || $tino != $oino; |
136 | } |
137 | closedir(DIR); |
fb73857a |
138 | return undef unless defined $direntry; # should never happen |
40000a8c |
139 | unshift(@path, $direntry); |
a0d0e21e |
140 | } |
fb73857a |
141 | $path = '/' . join('/', @path); |
21ac5ced |
142 | if ($^O eq 'apollo') { $path = "/".$path; } |
fb73857a |
143 | # At this point $path may be tainted (if tainting) and chdir would fail. |
144 | # To be more useful we untaint it then check that we landed where we started. |
145 | $path = $1 if $path =~ /^(.*)$/; # untaint |
e79e61e4 |
146 | CORE::chdir($path) || return undef; |
fb73857a |
147 | ($cdev, $cino) = stat('.'); |
148 | die "Unstable directory path, current directory changed unexpectedly" |
149 | if $cdev != $orig_cdev || $cino != $orig_cino; |
a0d0e21e |
150 | $path; |
151 | } |
152 | |
153 | |
4633a7c4 |
154 | # Keeps track of current working directory in PWD environment var |
a0d0e21e |
155 | # Usage: |
156 | # use Cwd 'chdir'; |
157 | # chdir $newdir; |
158 | |
4633a7c4 |
159 | my $chdir_init = 0; |
a0d0e21e |
160 | |
4633a7c4 |
161 | sub chdir_init { |
39e571d4 |
162 | if ($ENV{'PWD'} and $^O ne 'os2' and $^O ne 'dos') { |
a0d0e21e |
163 | my($dd,$di) = stat('.'); |
164 | my($pd,$pi) = stat($ENV{'PWD'}); |
165 | if (!defined $dd or !defined $pd or $di != $pi or $dd != $pd) { |
4633a7c4 |
166 | $ENV{'PWD'} = cwd(); |
a0d0e21e |
167 | } |
168 | } |
169 | else { |
4633a7c4 |
170 | $ENV{'PWD'} = cwd(); |
a0d0e21e |
171 | } |
4633a7c4 |
172 | # Strip an automounter prefix (where /tmp_mnt/foo/bar == /foo/bar) |
a0d0e21e |
173 | if ($ENV{'PWD'} =~ m|(/[^/]+(/[^/]+/[^/]+))(.*)|) { |
174 | my($pd,$pi) = stat($2); |
175 | my($dd,$di) = stat($1); |
176 | if (defined $pd and defined $dd and $di == $pi and $dd == $pd) { |
177 | $ENV{'PWD'}="$2$3"; |
178 | } |
179 | } |
180 | $chdir_init = 1; |
181 | } |
182 | |
183 | sub chdir { |
4633a7c4 |
184 | my $newdir = shift || ''; # allow for no arg (chdir to HOME dir) |
185 | $newdir =~ s|///*|/|g; |
a0d0e21e |
186 | chdir_init() unless $chdir_init; |
4633a7c4 |
187 | return 0 unless CORE::chdir $newdir; |
c6538b72 |
188 | if ($^O eq 'VMS') { return $ENV{'PWD'} = $ENV{'DEFAULT'} } |
748a9306 |
189 | |
a0d0e21e |
190 | if ($newdir =~ m#^/#) { |
191 | $ENV{'PWD'} = $newdir; |
4633a7c4 |
192 | } else { |
193 | my @curdir = split(m#/#,$ENV{'PWD'}); |
194 | @curdir = ('') unless @curdir; |
195 | my $component; |
a0d0e21e |
196 | foreach $component (split(m#/#, $newdir)) { |
197 | next if $component eq '.'; |
198 | pop(@curdir),next if $component eq '..'; |
199 | push(@curdir,$component); |
200 | } |
201 | $ENV{'PWD'} = join('/',@curdir) || '/'; |
202 | } |
4633a7c4 |
203 | 1; |
a0d0e21e |
204 | } |
205 | |
8b88ae92 |
206 | # Taken from Cwd.pm It is really getcwd with an optional |
207 | # parameter instead of '.' |
208 | # |
209 | |
210 | sub abs_path |
211 | { |
07569ed3 |
212 | my $start = @_ ? shift : '.'; |
8b88ae92 |
213 | my($dotdots, $cwd, @pst, @cst, $dir, @tst); |
214 | |
215 | unless (@cst = stat( $start )) |
216 | { |
217 | carp "stat($start): $!"; |
218 | return ''; |
219 | } |
220 | $cwd = ''; |
221 | $dotdots = $start; |
222 | do |
223 | { |
224 | $dotdots .= '/..'; |
225 | @pst = @cst; |
226 | unless (opendir(PARENT, $dotdots)) |
227 | { |
228 | carp "opendir($dotdots): $!"; |
229 | return ''; |
230 | } |
231 | unless (@cst = stat($dotdots)) |
232 | { |
233 | carp "stat($dotdots): $!"; |
234 | closedir(PARENT); |
235 | return ''; |
236 | } |
237 | if ($pst[0] == $cst[0] && $pst[1] == $cst[1]) |
238 | { |
07569ed3 |
239 | $dir = undef; |
8b88ae92 |
240 | } |
241 | else |
242 | { |
243 | do |
244 | { |
245 | unless (defined ($dir = readdir(PARENT))) |
246 | { |
247 | carp "readdir($dotdots): $!"; |
248 | closedir(PARENT); |
249 | return ''; |
250 | } |
251 | $tst[0] = $pst[0]+1 unless (@tst = lstat("$dotdots/$dir")) |
252 | } |
253 | while ($dir eq '.' || $dir eq '..' || $tst[0] != $pst[0] || |
254 | $tst[1] != $pst[1]); |
255 | } |
07569ed3 |
256 | $cwd = (defined $dir ? "$dir" : "" ) . "/$cwd" ; |
8b88ae92 |
257 | closedir(PARENT); |
07569ed3 |
258 | } while (defined $dir); |
259 | chop($cwd) unless $cwd eq '/'; # drop the trailing / |
8b88ae92 |
260 | $cwd; |
261 | } |
262 | |
e4c51978 |
263 | # added function alias for those of us more |
264 | # used to the libc function. --tchrist 27-Jan-00 |
265 | *realpath = \&abs_path; |
266 | |
96e4d5b1 |
267 | sub fast_abs_path { |
268 | my $cwd = getcwd(); |
269 | my $path = shift || '.'; |
e79e61e4 |
270 | CORE::chdir($path) || croak "Cannot chdir to $path:$!"; |
96e4d5b1 |
271 | my $realpath = getcwd(); |
e79e61e4 |
272 | CORE::chdir($cwd) || croak "Cannot chdir back to $cwd:$!"; |
96e4d5b1 |
273 | $realpath; |
8b88ae92 |
274 | } |
275 | |
e4c51978 |
276 | # added function alias to follow principle of least surprise |
277 | # based on previous aliasing. --tchrist 27-Jan-00 |
278 | *fast_realpath = \&fast_abs_path; |
279 | |
4633a7c4 |
280 | |
281 | # --- PORTING SECTION --- |
282 | |
283 | # VMS: $ENV{'DEFAULT'} points to default directory at all times |
bd3fa61c |
284 | # 06-Mar-1996 Charles Bailey bailey@newman.upenn.edu |
c6538b72 |
285 | # Note: Use of Cwd::chdir() causes the logical name PWD to be defined |
8b88ae92 |
286 | # in the process logical name table as the default device and directory |
287 | # seen by Perl. This may not be the same as the default device |
4633a7c4 |
288 | # and directory seen by DCL after Perl exits, since the effects |
289 | # the CRTL chdir() function persist only until Perl exits. |
4633a7c4 |
290 | |
291 | sub _vms_cwd { |
96e4d5b1 |
292 | return $ENV{'DEFAULT'}; |
293 | } |
294 | |
295 | sub _vms_abs_path { |
296 | return $ENV{'DEFAULT'} unless @_; |
297 | my $path = VMS::Filespec::pathify($_[0]); |
298 | croak("Invalid path name $_[0]") unless defined $path; |
299 | return VMS::Filespec::rmsexpand($path); |
4633a7c4 |
300 | } |
68dc0745 |
301 | |
4633a7c4 |
302 | sub _os2_cwd { |
303 | $ENV{'PWD'} = `cmd /c cd`; |
304 | chop $ENV{'PWD'}; |
305 | $ENV{'PWD'} =~ s:\\:/:g ; |
306 | return $ENV{'PWD'}; |
307 | } |
308 | |
96e4d5b1 |
309 | sub _win32_cwd { |
2d7a9237 |
310 | $ENV{'PWD'} = Win32::GetCwd(); |
96e4d5b1 |
311 | $ENV{'PWD'} =~ s:\\:/:g ; |
312 | return $ENV{'PWD'}; |
313 | } |
314 | |
315 | *_NT_cwd = \&_win32_cwd if (!defined &_NT_cwd && |
2d7a9237 |
316 | defined &Win32::GetCwd); |
96e4d5b1 |
317 | |
318 | *_NT_cwd = \&_os2_cwd unless defined &_NT_cwd; |
68dc0745 |
319 | |
39e571d4 |
320 | sub _dos_cwd { |
321 | if (!defined &Dos::GetCwd) { |
322 | $ENV{'PWD'} = `command /c cd`; |
323 | chop $ENV{'PWD'}; |
324 | $ENV{'PWD'} =~ s:\\:/:g ; |
325 | } else { |
326 | $ENV{'PWD'} = Dos::GetCwd(); |
327 | } |
55497cff |
328 | return $ENV{'PWD'}; |
329 | } |
330 | |
7fbf1995 |
331 | sub _qnx_cwd { |
332 | $ENV{'PWD'} = `/usr/bin/fullpath -t`; |
333 | chop $ENV{'PWD'}; |
334 | return $ENV{'PWD'}; |
335 | } |
336 | |
337 | sub _qnx_abs_path { |
338 | my $path = shift || '.'; |
339 | my $realpath=`/usr/bin/fullpath -t $path`; |
340 | chop $realpath; |
341 | return $realpath; |
342 | } |
343 | |
ac1ad7f0 |
344 | { |
db376a24 |
345 | no warnings; # assignments trigger 'subroutine redefined' warning |
4633a7c4 |
346 | |
ac1ad7f0 |
347 | if ($^O eq 'VMS') { |
96e4d5b1 |
348 | *cwd = \&_vms_cwd; |
349 | *getcwd = \&_vms_cwd; |
350 | *fastcwd = \&_vms_cwd; |
351 | *fastgetcwd = \&_vms_cwd; |
352 | *abs_path = \&_vms_abs_path; |
353 | *fast_abs_path = \&_vms_abs_path; |
ac1ad7f0 |
354 | } |
355 | elsif ($^O eq 'NT' or $^O eq 'MSWin32') { |
356 | # We assume that &_NT_cwd is defined as an XSUB or in the core. |
96e4d5b1 |
357 | *cwd = \&_NT_cwd; |
358 | *getcwd = \&_NT_cwd; |
359 | *fastcwd = \&_NT_cwd; |
360 | *fastgetcwd = \&_NT_cwd; |
361 | *abs_path = \&fast_abs_path; |
ac1ad7f0 |
362 | } |
363 | elsif ($^O eq 'os2') { |
364 | # sys_cwd may keep the builtin command |
96e4d5b1 |
365 | *cwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd; |
366 | *getcwd = \&cwd; |
367 | *fastgetcwd = \&cwd; |
368 | *fastcwd = \&cwd; |
369 | *abs_path = \&fast_abs_path; |
ac1ad7f0 |
370 | } |
39e571d4 |
371 | elsif ($^O eq 'dos') { |
372 | *cwd = \&_dos_cwd; |
373 | *getcwd = \&_dos_cwd; |
374 | *fastgetcwd = \&_dos_cwd; |
375 | *fastcwd = \&_dos_cwd; |
96e4d5b1 |
376 | *abs_path = \&fast_abs_path; |
ac1ad7f0 |
377 | } |
7fbf1995 |
378 | elsif ($^O eq 'qnx') { |
379 | *cwd = \&_qnx_cwd; |
380 | *getcwd = \&_qnx_cwd; |
381 | *fastgetcwd = \&_qnx_cwd; |
382 | *fastcwd = \&_qnx_cwd; |
383 | *abs_path = \&_qnx_abs_path; |
384 | *fast_abs_path = \&_qnx_abs_path; |
385 | } |
4fabb596 |
386 | elsif ($^O eq 'cygwin') { |
1cab015a |
387 | *getcwd = \&cwd; |
388 | *fastgetcwd = \&cwd; |
389 | *fastcwd = \&cwd; |
390 | *abs_path = \&fast_abs_path; |
391 | } |
55497cff |
392 | } |
4633a7c4 |
393 | |
394 | # package main; eval join('',<DATA>) || die $@; # quick test |
395 | |
a0d0e21e |
396 | 1; |
397 | |
4633a7c4 |
398 | __END__ |
399 | BEGIN { import Cwd qw(:DEFAULT chdir); } |
400 | print join("\n", cwd, getcwd, fastcwd, ""); |
401 | chdir('..'); |
402 | print join("\n", cwd, getcwd, fastcwd, ""); |
403 | print "$ENV{PWD}\n"; |