Source code of Windows XP (NT5)
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.

385 lines
9.7 KiB

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