Filename | /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/5.18.2/darwin-2level/Cwd.pm |
Statements | Executed 50 statements in 2.89ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
168 | 2 | 2 | 1.26ms | 1.26ms | abs_path (xsub) | Cwd::
1 | 1 | 1 | 36µs | 36µs | bootstrap (xsub) | Cwd::
1 | 1 | 1 | 28µs | 28µs | getcwd (xsub) | Cwd::
1 | 1 | 1 | 20µs | 20µs | CORE:fteexec (opcode) | Cwd::
1 | 1 | 1 | 19µs | 34µs | BEGIN@170 | Cwd::
1 | 1 | 1 | 14µs | 27µs | BEGIN@842 | Cwd::
1 | 1 | 1 | 9µs | 25µs | BEGIN@171 | Cwd::
1 | 1 | 1 | 8µs | 63µs | BEGIN@172 | Cwd::
1 | 1 | 1 | 5µs | 5µs | BEGIN@209 | Cwd::
1 | 1 | 1 | 4µs | 4µs | CORE:regcomp (opcode) | Cwd::
1 | 1 | 1 | 700ns | 700ns | CORE:match (opcode) | Cwd::
0 | 0 | 0 | 0s | 0s | __ANON__[:422] | Cwd::
0 | 0 | 0 | 0s | 0s | _backtick_pwd | Cwd::
0 | 0 | 0 | 0s | 0s | _carp | Cwd::
0 | 0 | 0 | 0s | 0s | _croak | Cwd::
0 | 0 | 0 | 0s | 0s | _dos_cwd | Cwd::
0 | 0 | 0 | 0s | 0s | _epoc_cwd | Cwd::
0 | 0 | 0 | 0s | 0s | _os2_cwd | Cwd::
0 | 0 | 0 | 0s | 0s | _perl_abs_path | Cwd::
0 | 0 | 0 | 0s | 0s | _perl_getcwd | Cwd::
0 | 0 | 0 | 0s | 0s | _qnx_abs_path | Cwd::
0 | 0 | 0 | 0s | 0s | _qnx_cwd | Cwd::
0 | 0 | 0 | 0s | 0s | _vms_abs_path | Cwd::
0 | 0 | 0 | 0s | 0s | _vms_cwd | Cwd::
0 | 0 | 0 | 0s | 0s | _vms_efs | Cwd::
0 | 0 | 0 | 0s | 0s | _vms_unix_rpt | Cwd::
0 | 0 | 0 | 0s | 0s | _win32_cwd | Cwd::
0 | 0 | 0 | 0s | 0s | _win32_cwd_simple | Cwd::
0 | 0 | 0 | 0s | 0s | chdir | Cwd::
0 | 0 | 0 | 0s | 0s | chdir_init | Cwd::
0 | 0 | 0 | 0s | 0s | fast_abs_path | Cwd::
0 | 0 | 0 | 0s | 0s | fastcwd_ | Cwd::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Cwd; | ||||
2 | |||||
3 | =head1 NAME | ||||
4 | |||||
5 | Cwd - get pathname of current working directory | ||||
6 | |||||
7 | =head1 SYNOPSIS | ||||
8 | |||||
9 | use Cwd; | ||||
10 | my $dir = getcwd; | ||||
11 | |||||
12 | use Cwd 'abs_path'; | ||||
13 | my $abs_path = abs_path($file); | ||||
14 | |||||
15 | =head1 DESCRIPTION | ||||
16 | |||||
17 | This module provides functions for determining the pathname of the | ||||
18 | current working directory. It is recommended that getcwd (or another | ||||
19 | *cwd() function) be used in I<all> code to ensure portability. | ||||
20 | |||||
21 | By default, it exports the functions cwd(), getcwd(), fastcwd(), and | ||||
22 | fastgetcwd() (and, on Win32, getdcwd()) into the caller's namespace. | ||||
23 | |||||
24 | |||||
25 | =head2 getcwd and friends | ||||
26 | |||||
27 | Each of these functions are called without arguments and return the | ||||
28 | absolute path of the current working directory. | ||||
29 | |||||
30 | =over 4 | ||||
31 | |||||
32 | =item getcwd | ||||
33 | |||||
34 | my $cwd = getcwd(); | ||||
35 | |||||
36 | Returns the current working directory. | ||||
37 | |||||
38 | Exposes the POSIX function getcwd(3) or re-implements it if it's not | ||||
39 | available. | ||||
40 | |||||
41 | =item cwd | ||||
42 | |||||
43 | my $cwd = cwd(); | ||||
44 | |||||
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 | ||||
47 | terminator). | ||||
48 | |||||
49 | =item fastcwd | ||||
50 | |||||
51 | my $cwd = fastcwd(); | ||||
52 | |||||
53 | A more dangerous version of getcwd(), but potentially faster. | ||||
54 | |||||
55 | It might conceivably chdir() you out of a directory that it can't | ||||
56 | chdir() you back into. If fastcwd encounters a problem it will return | ||||
57 | undef but will probably leave you in a different directory. For a | ||||
58 | measure of extra security, if everything appears to have worked, the | ||||
59 | fastcwd() function will check that it leaves you in the same directory | ||||
60 | that it started in. If it has changed it will C<die> with the message | ||||
61 | "Unstable directory path, current directory changed | ||||
62 | unexpectedly". That should never happen. | ||||
63 | |||||
64 | =item fastgetcwd | ||||
65 | |||||
66 | my $cwd = fastgetcwd(); | ||||
67 | |||||
68 | The fastgetcwd() function is provided as a synonym for cwd(). | ||||
69 | |||||
70 | =item getdcwd | ||||
71 | |||||
72 | my $cwd = getdcwd(); | ||||
73 | my $cwd = getdcwd('C:'); | ||||
74 | |||||
75 | The getdcwd() function is also provided on Win32 to get the current working | ||||
76 | directory on the specified drive, since Windows maintains a separate current | ||||
77 | working directory for each drive. If no drive is specified then the current | ||||
78 | drive is assumed. | ||||
79 | |||||
80 | This function simply calls the Microsoft C library _getdcwd() function. | ||||
81 | |||||
82 | =back | ||||
83 | |||||
84 | |||||
85 | =head2 abs_path and friends | ||||
86 | |||||
87 | These functions are exported only on request. They each take a single | ||||
88 | argument and return the absolute pathname for it. If no argument is | ||||
89 | given they'll use the current working directory. | ||||
90 | |||||
91 | =over 4 | ||||
92 | |||||
93 | =item abs_path | ||||
94 | |||||
95 | my $abs_path = abs_path($file); | ||||
96 | |||||
97 | Uses the same algorithm as getcwd(). Symbolic links and relative-path | ||||
98 | components ("." and "..") are resolved to return the canonical | ||||
99 | pathname, just like realpath(3). | ||||
100 | |||||
101 | =item realpath | ||||
102 | |||||
103 | my $abs_path = realpath($file); | ||||
104 | |||||
105 | A synonym for abs_path(). | ||||
106 | |||||
107 | =item fast_abs_path | ||||
108 | |||||
109 | my $abs_path = fast_abs_path($file); | ||||
110 | |||||
111 | A more dangerous, but potentially faster version of abs_path. | ||||
112 | |||||
113 | =back | ||||
114 | |||||
115 | =head2 $ENV{PWD} | ||||
116 | |||||
117 | If you ask to override your chdir() built-in function, | ||||
118 | |||||
119 | use Cwd qw(chdir); | ||||
120 | |||||
121 | then your PWD environment variable will be kept up to date. Note that | ||||
122 | it will only be kept up to date if all packages which use chdir import | ||||
123 | it from Cwd. | ||||
124 | |||||
125 | |||||
126 | =head1 NOTES | ||||
127 | |||||
128 | =over 4 | ||||
129 | |||||
130 | =item * | ||||
131 | |||||
132 | Since the path separators are different on some operating systems ('/' | ||||
133 | on Unix, ':' on MacPerl, etc...) we recommend you use the File::Spec | ||||
134 | modules wherever portability is a concern. | ||||
135 | |||||
136 | =item * | ||||
137 | |||||
138 | Actually, on Mac OS, the C<getcwd()>, C<fastgetcwd()> and C<fastcwd()> | ||||
139 | functions are all aliases for the C<cwd()> function, which, on Mac OS, | ||||
140 | calls `pwd`. Likewise, the C<abs_path()> function is an alias for | ||||
141 | C<fast_abs_path()>. | ||||
142 | |||||
143 | =back | ||||
144 | |||||
145 | =head1 AUTHOR | ||||
146 | |||||
147 | Originally by the perl5-porters. | ||||
148 | |||||
149 | Maintained by Ken Williams <KWILLIAMS@cpan.org> | ||||
150 | |||||
151 | =head1 COPYRIGHT | ||||
152 | |||||
153 | Copyright (c) 2004 by the Perl 5 Porters. All rights reserved. | ||||
154 | |||||
155 | This program is free software; you can redistribute it and/or modify | ||||
156 | it under the same terms as Perl itself. | ||||
157 | |||||
158 | Portions of the C code in this library are copyright (c) 1994 by the | ||||
159 | Regents of the University of California. All rights reserved. The | ||||
160 | license on this code is compatible with the licensing of the rest of | ||||
161 | the distribution - please see the source code in F<Cwd.xs> for the | ||||
162 | details. | ||||
163 | |||||
164 | =head1 SEE ALSO | ||||
165 | |||||
166 | L<File::chdir> | ||||
167 | |||||
168 | =cut | ||||
169 | |||||
170 | 2 | 26µs | 2 | 49µs | # spent 34µs (19+15) within Cwd::BEGIN@170 which was called:
# once (19µs+15µs) by Module::Pluggable::Object::BEGIN@4 at line 170 # spent 34µs making 1 call to Cwd::BEGIN@170
# spent 15µs making 1 call to strict::import |
171 | 2 | 27µs | 2 | 41µs | # spent 25µs (9+16) within Cwd::BEGIN@171 which was called:
# once (9µs+16µs) by Module::Pluggable::Object::BEGIN@4 at line 171 # spent 25µs making 1 call to Cwd::BEGIN@171
# spent 16µs making 1 call to Exporter::import |
172 | 2 | 173µs | 2 | 118µs | # spent 63µs (8+55) within Cwd::BEGIN@172 which was called:
# once (8µs+55µs) by Module::Pluggable::Object::BEGIN@4 at line 172 # spent 63µs making 1 call to Cwd::BEGIN@172
# spent 55µs making 1 call to vars::import |
173 | |||||
174 | 1 | 500ns | $VERSION = '3.47'; | ||
175 | 1 | 300ns | my $xs_version = $VERSION; | ||
176 | 1 | 800ns | $VERSION =~ tr/_//; | ||
177 | |||||
178 | 1 | 9µs | @ISA = qw/ Exporter /; | ||
179 | 1 | 1µs | @EXPORT = qw(cwd getcwd fastcwd fastgetcwd); | ||
180 | 1 | 800ns | push @EXPORT, qw(getdcwd) if $^O eq 'MSWin32'; | ||
181 | 1 | 1µs | @EXPORT_OK = qw(chdir abs_path fast_abs_path realpath fast_realpath); | ||
182 | |||||
183 | # sys_cwd may keep the builtin command | ||||
184 | |||||
185 | # All the functionality of this module may provided by builtins, | ||||
186 | # there is no sense to process the rest of the file. | ||||
187 | # The best choice may be to have this in BEGIN, but how to return from BEGIN? | ||||
188 | |||||
189 | 1 | 200ns | if ($^O eq 'os2') { | ||
190 | local $^W = 0; | ||||
191 | |||||
192 | *cwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd; | ||||
193 | *getcwd = \&cwd; | ||||
194 | *fastgetcwd = \&cwd; | ||||
195 | *fastcwd = \&cwd; | ||||
196 | |||||
197 | *fast_abs_path = \&sys_abspath if defined &sys_abspath; | ||||
198 | *abs_path = \&fast_abs_path; | ||||
199 | *realpath = \&fast_abs_path; | ||||
200 | *fast_realpath = \&fast_abs_path; | ||||
201 | |||||
202 | return 1; | ||||
203 | } | ||||
204 | |||||
205 | # Need to look up the feature settings on VMS. The preferred way is to use the | ||||
206 | # VMS::Feature module, but that may not be available to dual life modules. | ||||
207 | |||||
208 | 1 | 100ns | my $use_vms_feature; | ||
209 | # spent 5µs within Cwd::BEGIN@209 which was called:
# once (5µs+0s) by Module::Pluggable::Object::BEGIN@4 at line 215 | ||||
210 | 1 | 9µs | if ($^O eq 'VMS') { | ||
211 | if (eval { local $SIG{__DIE__}; require VMS::Feature; }) { | ||||
212 | $use_vms_feature = 1; | ||||
213 | } | ||||
214 | } | ||||
215 | 1 | 2.44ms | 1 | 5µs | } # spent 5µs making 1 call to Cwd::BEGIN@209 |
216 | |||||
217 | # Need to look up the UNIX report mode. This may become a dynamic mode | ||||
218 | # in the future. | ||||
219 | sub _vms_unix_rpt { | ||||
220 | my $unix_rpt; | ||||
221 | if ($use_vms_feature) { | ||||
222 | $unix_rpt = VMS::Feature::current("filename_unix_report"); | ||||
223 | } else { | ||||
224 | my $env_unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || ''; | ||||
225 | $unix_rpt = $env_unix_rpt =~ /^[ET1]/i; | ||||
226 | } | ||||
227 | return $unix_rpt; | ||||
228 | } | ||||
229 | |||||
230 | # Need to look up the EFS character set mode. This may become a dynamic | ||||
231 | # mode in the future. | ||||
232 | sub _vms_efs { | ||||
233 | my $efs; | ||||
234 | if ($use_vms_feature) { | ||||
235 | $efs = VMS::Feature::current("efs_charset"); | ||||
236 | } else { | ||||
237 | my $env_efs = $ENV{'DECC$EFS_CHARSET'} || ''; | ||||
238 | $efs = $env_efs =~ /^[ET1]/i; | ||||
239 | } | ||||
240 | return $efs; | ||||
241 | } | ||||
242 | |||||
243 | |||||
244 | # If loading the XS stuff doesn't work, we can fall back to pure perl | ||||
245 | 1 | 400ns | unless (defined &getcwd) { | ||
246 | eval { | ||||
247 | if ( $] >= 5.006 ) { | ||||
248 | require XSLoader; | ||||
249 | XSLoader::load( __PACKAGE__, $xs_version); | ||||
250 | } else { | ||||
251 | require DynaLoader; | ||||
252 | push @ISA, 'DynaLoader'; | ||||
253 | __PACKAGE__->bootstrap( $xs_version ); | ||||
254 | } | ||||
255 | }; | ||||
256 | } | ||||
257 | |||||
258 | # Big nasty table of function aliases | ||||
259 | 1 | 11µs | my %METHOD_MAP = | ||
260 | ( | ||||
261 | VMS => | ||||
262 | { | ||||
263 | cwd => '_vms_cwd', | ||||
264 | getcwd => '_vms_cwd', | ||||
265 | fastcwd => '_vms_cwd', | ||||
266 | fastgetcwd => '_vms_cwd', | ||||
267 | abs_path => '_vms_abs_path', | ||||
268 | fast_abs_path => '_vms_abs_path', | ||||
269 | }, | ||||
270 | |||||
271 | MSWin32 => | ||||
272 | { | ||||
273 | # We assume that &_NT_cwd is defined as an XSUB or in the core. | ||||
274 | cwd => '_NT_cwd', | ||||
275 | getcwd => '_NT_cwd', | ||||
276 | fastcwd => '_NT_cwd', | ||||
277 | fastgetcwd => '_NT_cwd', | ||||
278 | abs_path => 'fast_abs_path', | ||||
279 | realpath => 'fast_abs_path', | ||||
280 | }, | ||||
281 | |||||
282 | dos => | ||||
283 | { | ||||
284 | cwd => '_dos_cwd', | ||||
285 | getcwd => '_dos_cwd', | ||||
286 | fastgetcwd => '_dos_cwd', | ||||
287 | fastcwd => '_dos_cwd', | ||||
288 | abs_path => 'fast_abs_path', | ||||
289 | }, | ||||
290 | |||||
291 | # QNX4. QNX6 has a $os of 'nto'. | ||||
292 | qnx => | ||||
293 | { | ||||
294 | cwd => '_qnx_cwd', | ||||
295 | getcwd => '_qnx_cwd', | ||||
296 | fastgetcwd => '_qnx_cwd', | ||||
297 | fastcwd => '_qnx_cwd', | ||||
298 | abs_path => '_qnx_abs_path', | ||||
299 | fast_abs_path => '_qnx_abs_path', | ||||
300 | }, | ||||
301 | |||||
302 | cygwin => | ||||
303 | { | ||||
304 | getcwd => 'cwd', | ||||
305 | fastgetcwd => 'cwd', | ||||
306 | fastcwd => 'cwd', | ||||
307 | abs_path => 'fast_abs_path', | ||||
308 | realpath => 'fast_abs_path', | ||||
309 | }, | ||||
310 | |||||
311 | epoc => | ||||
312 | { | ||||
313 | cwd => '_epoc_cwd', | ||||
314 | getcwd => '_epoc_cwd', | ||||
315 | fastgetcwd => '_epoc_cwd', | ||||
316 | fastcwd => '_epoc_cwd', | ||||
317 | abs_path => 'fast_abs_path', | ||||
318 | }, | ||||
319 | |||||
320 | MacOS => | ||||
321 | { | ||||
322 | getcwd => 'cwd', | ||||
323 | fastgetcwd => 'cwd', | ||||
324 | fastcwd => 'cwd', | ||||
325 | abs_path => 'fast_abs_path', | ||||
326 | }, | ||||
327 | ); | ||||
328 | |||||
329 | 1 | 900ns | $METHOD_MAP{NT} = $METHOD_MAP{MSWin32}; | ||
330 | |||||
331 | |||||
332 | # Find the pwd command in the expected locations. We assume these | ||||
333 | # are safe. This prevents _backtick_pwd() consulting $ENV{PATH} | ||||
334 | # so everything works under taint mode. | ||||
335 | 1 | 0s | my $pwd_cmd; | ||
336 | 1 | 400ns | foreach my $try ('/bin/pwd', | ||
337 | '/usr/bin/pwd', | ||||
338 | '/QOpenSys/bin/pwd', # OS/400 PASE. | ||||
339 | ) { | ||||
340 | |||||
341 | 1 | 27µs | 1 | 20µs | if( -x $try ) { # spent 20µs making 1 call to Cwd::CORE:fteexec |
342 | 1 | 300ns | $pwd_cmd = $try; | ||
343 | 1 | 800ns | last; | ||
344 | } | ||||
345 | } | ||||
346 | |||||
347 | # Android has a built-in pwd. Using $pwd_cmd will DTRT if | ||||
348 | # this perl was compiled with -Dd_useshellcmds, which is the | ||||
349 | # default for Android, but the block below is needed for the | ||||
350 | # miniperl running on the host when cross-compiling, and | ||||
351 | # potentially for native builds with -Ud_useshellcmds. | ||||
352 | 1 | 5µs | 1 | 700ns | if ($^O =~ /android/) { # spent 700ns making 1 call to Cwd::CORE:match |
353 | # If targetsh is executable, then we're either a full | ||||
354 | # perl, or a miniperl for a native build. | ||||
355 | if (-x $Config::Config{targetsh}) { | ||||
356 | $pwd_cmd = "$Config::Config{targetsh} -c pwd" | ||||
357 | } | ||||
358 | else { | ||||
359 | $pwd_cmd = "$Config::Config{sh} -c pwd" | ||||
360 | } | ||||
361 | } | ||||
362 | |||||
363 | 1 | 400ns | my $found_pwd_cmd = defined($pwd_cmd); | ||
364 | 1 | 100ns | unless ($pwd_cmd) { | ||
365 | # Isn't this wrong? _backtick_pwd() will fail if someone has | ||||
366 | # pwd in their path but it is not /bin/pwd or /usr/bin/pwd? | ||||
367 | # See [perl #16774]. --jhi | ||||
368 | $pwd_cmd = 'pwd'; | ||||
369 | } | ||||
370 | |||||
371 | # Lazy-load Carp | ||||
372 | sub _carp { require Carp; Carp::carp(@_) } | ||||
373 | sub _croak { require Carp; Carp::croak(@_) } | ||||
374 | |||||
375 | # The 'natural and safe form' for UNIX (pwd may be setuid root) | ||||
376 | sub _backtick_pwd { | ||||
377 | # Localize %ENV entries in a way that won't create new hash keys | ||||
378 | my @localize = grep exists $ENV{$_}, qw(PATH IFS CDPATH ENV BASH_ENV); | ||||
379 | local @ENV{@localize}; | ||||
380 | |||||
381 | my $cwd = `$pwd_cmd`; | ||||
382 | # Belt-and-suspenders in case someone said "undef $/". | ||||
383 | local $/ = "\n"; | ||||
384 | # `pwd` may fail e.g. if the disk is full | ||||
385 | chomp($cwd) if defined $cwd; | ||||
386 | $cwd; | ||||
387 | } | ||||
388 | |||||
389 | # Since some ports may predefine cwd internally (e.g., NT) | ||||
390 | # we take care not to override an existing definition for cwd(). | ||||
391 | |||||
392 | 1 | 1µs | unless ($METHOD_MAP{$^O}{cwd} or defined &cwd) { | ||
393 | # The pwd command is not available in some chroot(2)'ed environments | ||||
394 | 1 | 6µs | 1 | 4µs | my $sep = $Config::Config{path_sep} || ':'; # spent 4µs making 1 call to Config::FETCH |
395 | 1 | 500ns | my $os = $^O; # Protect $^O from tainting | ||
396 | |||||
397 | |||||
398 | # Try again to find a pwd, this time searching the whole PATH. | ||||
399 | 1 | 800ns | if (defined $ENV{PATH} and $os ne 'MSWin32') { # no pwd on Windows | ||
400 | 1 | 13µs | 1 | 4µs | my @candidates = split($sep, $ENV{PATH}); # spent 4µs making 1 call to Cwd::CORE:regcomp |
401 | 1 | 2µs | while (!$found_pwd_cmd and @candidates) { | ||
402 | my $candidate = shift @candidates; | ||||
403 | $found_pwd_cmd = 1 if -x "$candidate/pwd"; | ||||
404 | } | ||||
405 | } | ||||
406 | |||||
407 | # MacOS has some special magic to make `pwd` work. | ||||
408 | 1 | 1µs | if( $os eq 'MacOS' || $found_pwd_cmd ) | ||
409 | { | ||||
410 | *cwd = \&_backtick_pwd; | ||||
411 | } | ||||
412 | else { | ||||
413 | *cwd = \&getcwd; | ||||
414 | } | ||||
415 | } | ||||
416 | |||||
417 | 1 | 500ns | if ($^O eq 'cygwin') { | ||
418 | # We need to make sure cwd() is called with no args, because it's | ||||
419 | # got an arg-less prototype and will die if args are present. | ||||
420 | local $^W = 0; | ||||
421 | my $orig_cwd = \&cwd; | ||||
422 | *cwd = sub { &$orig_cwd() } | ||||
423 | } | ||||
424 | |||||
425 | |||||
426 | # set a reasonable (and very safe) default for fastgetcwd, in case it | ||||
427 | # isn't redefined later (20001212 rspier) | ||||
428 | 1 | 400ns | *fastgetcwd = \&cwd; | ||
429 | |||||
430 | # A non-XS version of getcwd() - also used to bootstrap the perl build | ||||
431 | # process, when miniperl is running and no XS loading happens. | ||||
432 | sub _perl_getcwd | ||||
433 | { | ||||
434 | abs_path('.'); | ||||
435 | } | ||||
436 | |||||
437 | # By John Bazik | ||||
438 | # | ||||
439 | # Usage: $cwd = &fastcwd; | ||||
440 | # | ||||
441 | # This is a faster version of getcwd. It's also more dangerous because | ||||
442 | # you might chdir out of a directory that you can't chdir back into. | ||||
443 | |||||
444 | sub fastcwd_ { | ||||
445 | my($odev, $oino, $cdev, $cino, $tdev, $tino); | ||||
446 | my(@path, $path); | ||||
447 | local(*DIR); | ||||
448 | |||||
449 | my($orig_cdev, $orig_cino) = stat('.'); | ||||
450 | ($cdev, $cino) = ($orig_cdev, $orig_cino); | ||||
451 | for (;;) { | ||||
452 | my $direntry; | ||||
453 | ($odev, $oino) = ($cdev, $cino); | ||||
454 | CORE::chdir('..') || return undef; | ||||
455 | ($cdev, $cino) = stat('.'); | ||||
456 | last if $odev == $cdev && $oino == $cino; | ||||
457 | opendir(DIR, '.') || return undef; | ||||
458 | for (;;) { | ||||
459 | $direntry = readdir(DIR); | ||||
460 | last unless defined $direntry; | ||||
461 | next if $direntry eq '.'; | ||||
462 | next if $direntry eq '..'; | ||||
463 | |||||
464 | ($tdev, $tino) = lstat($direntry); | ||||
465 | last unless $tdev != $odev || $tino != $oino; | ||||
466 | } | ||||
467 | closedir(DIR); | ||||
468 | return undef unless defined $direntry; # should never happen | ||||
469 | unshift(@path, $direntry); | ||||
470 | } | ||||
471 | $path = '/' . join('/', @path); | ||||
472 | if ($^O eq 'apollo') { $path = "/".$path; } | ||||
473 | # At this point $path may be tainted (if tainting) and chdir would fail. | ||||
474 | # Untaint it then check that we landed where we started. | ||||
475 | $path =~ /^(.*)\z/s # untaint | ||||
476 | && CORE::chdir($1) or return undef; | ||||
477 | ($cdev, $cino) = stat('.'); | ||||
478 | die "Unstable directory path, current directory changed unexpectedly" | ||||
479 | if $cdev != $orig_cdev || $cino != $orig_cino; | ||||
480 | $path; | ||||
481 | } | ||||
482 | 1 | 200ns | if (not defined &fastcwd) { *fastcwd = \&fastcwd_ } | ||
483 | |||||
484 | |||||
485 | # Keeps track of current working directory in PWD environment var | ||||
486 | # Usage: | ||||
487 | # use Cwd 'chdir'; | ||||
488 | # chdir $newdir; | ||||
489 | |||||
490 | 1 | 100ns | my $chdir_init = 0; | ||
491 | |||||
492 | sub chdir_init { | ||||
493 | if ($ENV{'PWD'} and $^O ne 'os2' and $^O ne 'dos' and $^O ne 'MSWin32') { | ||||
494 | my($dd,$di) = stat('.'); | ||||
495 | my($pd,$pi) = stat($ENV{'PWD'}); | ||||
496 | if (!defined $dd or !defined $pd or $di != $pi or $dd != $pd) { | ||||
497 | $ENV{'PWD'} = cwd(); | ||||
498 | } | ||||
499 | } | ||||
500 | else { | ||||
501 | my $wd = cwd(); | ||||
502 | $wd = Win32::GetFullPathName($wd) if $^O eq 'MSWin32'; | ||||
503 | $ENV{'PWD'} = $wd; | ||||
504 | } | ||||
505 | # Strip an automounter prefix (where /tmp_mnt/foo/bar == /foo/bar) | ||||
506 | if ($^O ne 'MSWin32' and $ENV{'PWD'} =~ m|(/[^/]+(/[^/]+/[^/]+))(.*)|s) { | ||||
507 | my($pd,$pi) = stat($2); | ||||
508 | my($dd,$di) = stat($1); | ||||
509 | if (defined $pd and defined $dd and $di == $pi and $dd == $pd) { | ||||
510 | $ENV{'PWD'}="$2$3"; | ||||
511 | } | ||||
512 | } | ||||
513 | $chdir_init = 1; | ||||
514 | } | ||||
515 | |||||
516 | sub chdir { | ||||
517 | my $newdir = @_ ? shift : ''; # allow for no arg (chdir to HOME dir) | ||||
518 | $newdir =~ s|///*|/|g unless $^O eq 'MSWin32'; | ||||
519 | chdir_init() unless $chdir_init; | ||||
520 | my $newpwd; | ||||
521 | if ($^O eq 'MSWin32') { | ||||
522 | # get the full path name *before* the chdir() | ||||
523 | $newpwd = Win32::GetFullPathName($newdir); | ||||
524 | } | ||||
525 | |||||
526 | return 0 unless CORE::chdir $newdir; | ||||
527 | |||||
528 | if ($^O eq 'VMS') { | ||||
529 | return $ENV{'PWD'} = $ENV{'DEFAULT'} | ||||
530 | } | ||||
531 | elsif ($^O eq 'MacOS') { | ||||
532 | return $ENV{'PWD'} = cwd(); | ||||
533 | } | ||||
534 | elsif ($^O eq 'MSWin32') { | ||||
535 | $ENV{'PWD'} = $newpwd; | ||||
536 | return 1; | ||||
537 | } | ||||
538 | |||||
539 | if (ref $newdir eq 'GLOB') { # in case a file/dir handle is passed in | ||||
540 | $ENV{'PWD'} = cwd(); | ||||
541 | } elsif ($newdir =~ m#^/#s) { | ||||
542 | $ENV{'PWD'} = $newdir; | ||||
543 | } else { | ||||
544 | my @curdir = split(m#/#,$ENV{'PWD'}); | ||||
545 | @curdir = ('') unless @curdir; | ||||
546 | my $component; | ||||
547 | foreach $component (split(m#/#, $newdir)) { | ||||
548 | next if $component eq '.'; | ||||
549 | pop(@curdir),next if $component eq '..'; | ||||
550 | push(@curdir,$component); | ||||
551 | } | ||||
552 | $ENV{'PWD'} = join('/',@curdir) || '/'; | ||||
553 | } | ||||
554 | 1; | ||||
555 | } | ||||
556 | |||||
557 | |||||
558 | sub _perl_abs_path | ||||
559 | { | ||||
560 | my $start = @_ ? shift : '.'; | ||||
561 | my($dotdots, $cwd, @pst, @cst, $dir, @tst); | ||||
562 | |||||
563 | unless (@cst = stat( $start )) | ||||
564 | { | ||||
565 | _carp("stat($start): $!"); | ||||
566 | return ''; | ||||
567 | } | ||||
568 | |||||
569 | unless (-d _) { | ||||
570 | # Make sure we can be invoked on plain files, not just directories. | ||||
571 | # NOTE that this routine assumes that '/' is the only directory separator. | ||||
572 | |||||
573 | my ($dir, $file) = $start =~ m{^(.*)/(.+)$} | ||||
574 | or return cwd() . '/' . $start; | ||||
575 | |||||
576 | # Can't use "-l _" here, because the previous stat was a stat(), not an lstat(). | ||||
577 | if (-l $start) { | ||||
578 | my $link_target = readlink($start); | ||||
579 | die "Can't resolve link $start: $!" unless defined $link_target; | ||||
580 | |||||
581 | require File::Spec; | ||||
582 | $link_target = $dir . '/' . $link_target | ||||
583 | unless File::Spec->file_name_is_absolute($link_target); | ||||
584 | |||||
585 | return abs_path($link_target); | ||||
586 | } | ||||
587 | |||||
588 | return $dir ? abs_path($dir) . "/$file" : "/$file"; | ||||
589 | } | ||||
590 | |||||
591 | $cwd = ''; | ||||
592 | $dotdots = $start; | ||||
593 | do | ||||
594 | { | ||||
595 | $dotdots .= '/..'; | ||||
596 | @pst = @cst; | ||||
597 | local *PARENT; | ||||
598 | unless (opendir(PARENT, $dotdots)) | ||||
599 | { | ||||
600 | # probably a permissions issue. Try the native command. | ||||
601 | require File::Spec; | ||||
602 | return File::Spec->rel2abs( $start, _backtick_pwd() ); | ||||
603 | } | ||||
604 | unless (@cst = stat($dotdots)) | ||||
605 | { | ||||
606 | _carp("stat($dotdots): $!"); | ||||
607 | closedir(PARENT); | ||||
608 | return ''; | ||||
609 | } | ||||
610 | if ($pst[0] == $cst[0] && $pst[1] == $cst[1]) | ||||
611 | { | ||||
612 | $dir = undef; | ||||
613 | } | ||||
614 | else | ||||
615 | { | ||||
616 | do | ||||
617 | { | ||||
618 | unless (defined ($dir = readdir(PARENT))) | ||||
619 | { | ||||
620 | _carp("readdir($dotdots): $!"); | ||||
621 | closedir(PARENT); | ||||
622 | return ''; | ||||
623 | } | ||||
624 | $tst[0] = $pst[0]+1 unless (@tst = lstat("$dotdots/$dir")) | ||||
625 | } | ||||
626 | while ($dir eq '.' || $dir eq '..' || $tst[0] != $pst[0] || | ||||
627 | $tst[1] != $pst[1]); | ||||
628 | } | ||||
629 | $cwd = (defined $dir ? "$dir" : "" ) . "/$cwd" ; | ||||
630 | closedir(PARENT); | ||||
631 | } while (defined $dir); | ||||
632 | chop($cwd) unless $cwd eq '/'; # drop the trailing / | ||||
633 | $cwd; | ||||
634 | } | ||||
635 | |||||
636 | |||||
637 | 1 | 0s | my $Curdir; | ||
638 | sub fast_abs_path { | ||||
639 | local $ENV{PWD} = $ENV{PWD} || ''; # Guard against clobberage | ||||
640 | my $cwd = getcwd(); | ||||
641 | require File::Spec; | ||||
642 | my $path = @_ ? shift : ($Curdir ||= File::Spec->curdir); | ||||
643 | |||||
644 | # Detaint else we'll explode in taint mode. This is safe because | ||||
645 | # we're not doing anything dangerous with it. | ||||
646 | ($path) = $path =~ /(.*)/s; | ||||
647 | ($cwd) = $cwd =~ /(.*)/s; | ||||
648 | |||||
649 | unless (-e $path) { | ||||
650 | _croak("$path: No such file or directory"); | ||||
651 | } | ||||
652 | |||||
653 | unless (-d _) { | ||||
654 | # Make sure we can be invoked on plain files, not just directories. | ||||
655 | |||||
656 | my ($vol, $dir, $file) = File::Spec->splitpath($path); | ||||
657 | return File::Spec->catfile($cwd, $path) unless length $dir; | ||||
658 | |||||
659 | if (-l $path) { | ||||
660 | my $link_target = readlink($path); | ||||
661 | die "Can't resolve link $path: $!" unless defined $link_target; | ||||
662 | |||||
663 | $link_target = File::Spec->catpath($vol, $dir, $link_target) | ||||
664 | unless File::Spec->file_name_is_absolute($link_target); | ||||
665 | |||||
666 | return fast_abs_path($link_target); | ||||
667 | } | ||||
668 | |||||
669 | return $dir eq File::Spec->rootdir | ||||
670 | ? File::Spec->catpath($vol, $dir, $file) | ||||
671 | : fast_abs_path(File::Spec->catpath($vol, $dir, '')) . '/' . $file; | ||||
672 | } | ||||
673 | |||||
674 | if (!CORE::chdir($path)) { | ||||
675 | _croak("Cannot chdir to $path: $!"); | ||||
676 | } | ||||
677 | my $realpath = getcwd(); | ||||
678 | if (! ((-d $cwd) && (CORE::chdir($cwd)))) { | ||||
679 | _croak("Cannot chdir back to $cwd: $!"); | ||||
680 | } | ||||
681 | $realpath; | ||||
682 | } | ||||
683 | |||||
684 | # added function alias to follow principle of least surprise | ||||
685 | # based on previous aliasing. --tchrist 27-Jan-00 | ||||
686 | 1 | 200ns | *fast_realpath = \&fast_abs_path; | ||
687 | |||||
688 | |||||
689 | # --- PORTING SECTION --- | ||||
690 | |||||
691 | # VMS: $ENV{'DEFAULT'} points to default directory at all times | ||||
692 | # 06-Mar-1996 Charles Bailey bailey@newman.upenn.edu | ||||
693 | # Note: Use of Cwd::chdir() causes the logical name PWD to be defined | ||||
694 | # in the process logical name table as the default device and directory | ||||
695 | # seen by Perl. This may not be the same as the default device | ||||
696 | # and directory seen by DCL after Perl exits, since the effects | ||||
697 | # the CRTL chdir() function persist only until Perl exits. | ||||
698 | |||||
699 | sub _vms_cwd { | ||||
700 | return $ENV{'DEFAULT'}; | ||||
701 | } | ||||
702 | |||||
703 | sub _vms_abs_path { | ||||
704 | return $ENV{'DEFAULT'} unless @_; | ||||
705 | my $path = shift; | ||||
706 | |||||
707 | my $efs = _vms_efs; | ||||
708 | my $unix_rpt = _vms_unix_rpt; | ||||
709 | |||||
710 | if (defined &VMS::Filespec::vmsrealpath) { | ||||
711 | my $path_unix = 0; | ||||
712 | my $path_vms = 0; | ||||
713 | |||||
714 | $path_unix = 1 if ($path =~ m#(?<=\^)/#); | ||||
715 | $path_unix = 1 if ($path =~ /^\.\.?$/); | ||||
716 | $path_vms = 1 if ($path =~ m#[\[<\]]#); | ||||
717 | $path_vms = 1 if ($path =~ /^--?$/); | ||||
718 | |||||
719 | my $unix_mode = $path_unix; | ||||
720 | if ($efs) { | ||||
721 | # In case of a tie, the Unix report mode decides. | ||||
722 | if ($path_vms == $path_unix) { | ||||
723 | $unix_mode = $unix_rpt; | ||||
724 | } else { | ||||
725 | $unix_mode = 0 if $path_vms; | ||||
726 | } | ||||
727 | } | ||||
728 | |||||
729 | if ($unix_mode) { | ||||
730 | # Unix format | ||||
731 | return VMS::Filespec::unixrealpath($path); | ||||
732 | } | ||||
733 | |||||
734 | # VMS format | ||||
735 | |||||
736 | my $new_path = VMS::Filespec::vmsrealpath($path); | ||||
737 | |||||
738 | # Perl expects directories to be in directory format | ||||
739 | $new_path = VMS::Filespec::pathify($new_path) if -d $path; | ||||
740 | return $new_path; | ||||
741 | } | ||||
742 | |||||
743 | # Fallback to older algorithm if correct ones are not | ||||
744 | # available. | ||||
745 | |||||
746 | if (-l $path) { | ||||
747 | my $link_target = readlink($path); | ||||
748 | die "Can't resolve link $path: $!" unless defined $link_target; | ||||
749 | |||||
750 | return _vms_abs_path($link_target); | ||||
751 | } | ||||
752 | |||||
753 | # may need to turn foo.dir into [.foo] | ||||
754 | my $pathified = VMS::Filespec::pathify($path); | ||||
755 | $path = $pathified if defined $pathified; | ||||
756 | |||||
757 | return VMS::Filespec::rmsexpand($path); | ||||
758 | } | ||||
759 | |||||
760 | sub _os2_cwd { | ||||
761 | $ENV{'PWD'} = `cmd /c cd`; | ||||
762 | chomp $ENV{'PWD'}; | ||||
763 | $ENV{'PWD'} =~ s:\\:/:g ; | ||||
764 | return $ENV{'PWD'}; | ||||
765 | } | ||||
766 | |||||
767 | sub _win32_cwd_simple { | ||||
768 | $ENV{'PWD'} = `cd`; | ||||
769 | chomp $ENV{'PWD'}; | ||||
770 | $ENV{'PWD'} =~ s:\\:/:g ; | ||||
771 | return $ENV{'PWD'}; | ||||
772 | } | ||||
773 | |||||
774 | sub _win32_cwd { | ||||
775 | # Need to avoid taking any sort of reference to the typeglob or the code in | ||||
776 | # the optree, so that this tests the runtime state of things, as the | ||||
777 | # ExtUtils::MakeMaker tests for "miniperl" need to be able to fake things at | ||||
778 | # runtime by deleting the subroutine. *foo{THING} syntax on a symbol table | ||||
779 | # lookup avoids needing a string eval, which has been reported to cause | ||||
780 | # problems (for reasons that we haven't been able to get to the bottom of - | ||||
781 | # rt.cpan.org #56225) | ||||
782 | if (*{$DynaLoader::{boot_DynaLoader}}{CODE}) { | ||||
783 | $ENV{'PWD'} = Win32::GetCwd(); | ||||
784 | } | ||||
785 | else { # miniperl | ||||
786 | chomp($ENV{'PWD'} = `cd`); | ||||
787 | } | ||||
788 | $ENV{'PWD'} =~ s:\\:/:g ; | ||||
789 | return $ENV{'PWD'}; | ||||
790 | } | ||||
791 | |||||
792 | 1 | 400ns | *_NT_cwd = defined &Win32::GetCwd ? \&_win32_cwd : \&_win32_cwd_simple; | ||
793 | |||||
794 | sub _dos_cwd { | ||||
795 | if (!defined &Dos::GetCwd) { | ||||
796 | $ENV{'PWD'} = `command /c cd`; | ||||
797 | chomp $ENV{'PWD'}; | ||||
798 | $ENV{'PWD'} =~ s:\\:/:g ; | ||||
799 | } else { | ||||
800 | $ENV{'PWD'} = Dos::GetCwd(); | ||||
801 | } | ||||
802 | return $ENV{'PWD'}; | ||||
803 | } | ||||
804 | |||||
805 | sub _qnx_cwd { | ||||
806 | local $ENV{PATH} = ''; | ||||
807 | local $ENV{CDPATH} = ''; | ||||
808 | local $ENV{ENV} = ''; | ||||
809 | $ENV{'PWD'} = `/usr/bin/fullpath -t`; | ||||
810 | chomp $ENV{'PWD'}; | ||||
811 | return $ENV{'PWD'}; | ||||
812 | } | ||||
813 | |||||
814 | sub _qnx_abs_path { | ||||
815 | local $ENV{PATH} = ''; | ||||
816 | local $ENV{CDPATH} = ''; | ||||
817 | local $ENV{ENV} = ''; | ||||
818 | my $path = @_ ? shift : '.'; | ||||
819 | local *REALPATH; | ||||
820 | |||||
821 | defined( open(REALPATH, '-|') || exec '/usr/bin/fullpath', '-t', $path ) or | ||||
822 | die "Can't open /usr/bin/fullpath: $!"; | ||||
823 | my $realpath = <REALPATH>; | ||||
824 | close REALPATH; | ||||
825 | chomp $realpath; | ||||
826 | return $realpath; | ||||
827 | } | ||||
828 | |||||
829 | sub _epoc_cwd { | ||||
830 | $ENV{'PWD'} = EPOC::getcwd(); | ||||
831 | return $ENV{'PWD'}; | ||||
832 | } | ||||
833 | |||||
834 | |||||
835 | # Now that all the base-level functions are set up, alias the | ||||
836 | # user-level functions to the right places | ||||
837 | |||||
838 | 1 | 700ns | if (exists $METHOD_MAP{$^O}) { | ||
839 | 1 | 300ns | my $map = $METHOD_MAP{$^O}; | ||
840 | 1 | 1µs | foreach my $name (keys %$map) { | ||
841 | local $^W = 0; # assignments trigger 'subroutine redefined' warning | ||||
842 | 2 | 87µs | 2 | 40µs | # spent 27µs (14+13) within Cwd::BEGIN@842 which was called:
# once (14µs+13µs) by Module::Pluggable::Object::BEGIN@4 at line 842 # spent 27µs making 1 call to Cwd::BEGIN@842
# spent 13µs making 1 call to strict::unimport |
843 | *{$name} = \&{$map->{$name}}; | ||||
844 | } | ||||
845 | } | ||||
846 | |||||
847 | # In case the XS version doesn't load. | ||||
848 | 1 | 100ns | *abs_path = \&_perl_abs_path unless defined &abs_path; | ||
849 | 1 | 100ns | *getcwd = \&_perl_getcwd unless defined &getcwd; | ||
850 | |||||
851 | # added function alias for those of us more | ||||
852 | # used to the libc function. --tchrist 27-Jan-00 | ||||
853 | 1 | 300ns | *realpath = \&abs_path; | ||
854 | |||||
855 | 1 | 42µs | 1; | ||
# spent 20µs within Cwd::CORE:fteexec which was called:
# once (20µs+0s) by Module::Pluggable::Object::BEGIN@4 at line 341 | |||||
# spent 700ns within Cwd::CORE:match which was called:
# once (700ns+0s) by Module::Pluggable::Object::BEGIN@4 at line 352 | |||||
# spent 4µs within Cwd::CORE:regcomp which was called:
# once (4µs+0s) by Module::Pluggable::Object::BEGIN@4 at line 400 | |||||
# spent 1.26ms within Cwd::abs_path which was called 168 times, avg 8µs/call:
# 113 times (947µs+0s) by Path::Tiny::realpath at line 1221 of Path/Tiny.pm, avg 8µs/call
# 55 times (313µs+0s) by File::HomeDir::Darwin::Cocoa::_find_folder at line 128 of File/HomeDir/Darwin/Cocoa.pm, avg 6µs/call | |||||
# spent 36µs within Cwd::bootstrap which was called:
# once (36µs+0s) by DynaLoader::bootstrap at line 217 of DynaLoader.pm | |||||
# spent 28µs within Cwd::getcwd which was called:
# once (28µs+0s) by File::Find::_find_opt at line 484 of File/Find.pm |