Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

456 lines
12 KiB

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