home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Topware / activeperl / ActivePerl / Perl / lib / File / Find.pm < prev    next >
Encoding:
Perl POD Document  |  2002-06-19  |  34.7 KB  |  1,213 lines

  1. package File::Find;
  2. use 5.006;
  3. use strict;
  4. use warnings;
  5. use warnings::register;
  6. our $VERSION = '1.04';
  7. require Exporter;
  8. require Cwd;
  9.  
  10. =head1 NAME
  11.  
  12. File::Find - Traverse a directory tree.
  13.  
  14. =head1 SYNOPSIS
  15.  
  16.     use File::Find;
  17.     find(\&wanted, @directories_to_seach);
  18.     sub wanted { ... }
  19.  
  20.     use File::Find;
  21.     finddepth(\&wanted, @directories_to_search);
  22.     sub wanted { ... }
  23.  
  24.     use File::Find;
  25.     find({ wanted => \&process, follow => 1 }, '.');
  26.  
  27. =head1 DESCRIPTION
  28.  
  29. These are functions for searching through directory trees doing work
  30. on each file found similar to the Unix I<find> command.  File::Find
  31. exports two functions, C<find> and C<finddepth>.  They work similarly
  32. but have subtle differences.
  33.  
  34. =over 4
  35.  
  36. =item B<find>
  37.  
  38.   find(\&wanted,  @directories);
  39.   find(\%options, @directories);
  40.  
  41. find() does a breadth-first search over the given @directories in the
  42. order they are given.  In essense, it works from the top down.
  43.  
  44. For each file or directory found the &wanted subroutine is called (see
  45. below for details).  Additionally, for each directory found it will go
  46. into that directory and continue the search.
  47.  
  48. =item B<finddepth>
  49.  
  50.   finddepth(\&wanted,  @directories);
  51.   finddepth(\%options, @directories);
  52.  
  53. finddepth() works just like find() except it does a depth-first search.
  54. It works from the bottom of the directory tree up.
  55.  
  56. =back
  57.  
  58. =head2 %options
  59.  
  60. The first argument to find() is either a hash reference describing the
  61. operations to be performed for each file, or a code reference.  The
  62. code reference is described in L<The wanted function> below.
  63.  
  64. Here are the possible keys for the hash:
  65.  
  66. =over 3
  67.  
  68. =item C<wanted>
  69.  
  70. The value should be a code reference.  This code reference is
  71. described in L<The wanted function> below.
  72.  
  73. =item C<bydepth>
  74.  
  75. Reports the name of a directory only AFTER all its entries
  76. have been reported.  Entry point finddepth() is a shortcut for
  77. specifying C<{ bydepth =E<gt> 1 }> in the first argument of find().
  78.  
  79. =item C<preprocess>
  80.  
  81. The value should be a code reference. This code reference is used to 
  82. preprocess the current directory. The name of currently processed 
  83. directory is in $File::Find::dir. Your preprocessing function is 
  84. called after readdir() but before the loop that calls the wanted() 
  85. function. It is called with a list of strings (actually file/directory 
  86. names) and is expected to return a list of strings. The code can be 
  87. used to sort the file/directory names alphabetically, numerically, 
  88. or to filter out directory entries based on their name alone. When 
  89. I<follow> or I<follow_fast> are in effect, C<preprocess> is a no-op.
  90.  
  91. =item C<postprocess>
  92.  
  93. The value should be a code reference. It is invoked just before leaving 
  94. the currently processed directory. It is called in void context with no 
  95. arguments. The name of the current directory is in $File::Find::dir. This 
  96. hook is handy for summarizing a directory, such as calculating its disk 
  97. usage. When I<follow> or I<follow_fast> are in effect, C<postprocess> is a 
  98. no-op.
  99.  
  100. =item C<follow>
  101.  
  102. Causes symbolic links to be followed. Since directory trees with symbolic
  103. links (followed) may contain files more than once and may even have
  104. cycles, a hash has to be built up with an entry for each file.
  105. This might be expensive both in space and time for a large
  106. directory tree. See I<follow_fast> and I<follow_skip> below.
  107. If either I<follow> or I<follow_fast> is in effect:
  108.  
  109. =over 6
  110.  
  111. =item *
  112.  
  113. It is guaranteed that an I<lstat> has been called before the user's
  114. I<wanted()> function is called. This enables fast file checks involving S< _>.
  115.  
  116. =item *
  117.  
  118. There is a variable C<$File::Find::fullname> which holds the absolute
  119. pathname of the file with all symbolic links resolved
  120.  
  121. =back
  122.  
  123. =item C<follow_fast>
  124.  
  125. This is similar to I<follow> except that it may report some files more
  126. than once.  It does detect cycles, however.  Since only symbolic links
  127. have to be hashed, this is much cheaper both in space and time.  If
  128. processing a file more than once (by the user's I<wanted()> function)
  129. is worse than just taking time, the option I<follow> should be used.
  130.  
  131. =item C<follow_skip>
  132.  
  133. C<follow_skip==1>, which is the default, causes all files which are
  134. neither directories nor symbolic links to be ignored if they are about
  135. to be processed a second time. If a directory or a symbolic link 
  136. are about to be processed a second time, File::Find dies.
  137. C<follow_skip==0> causes File::Find to die if any file is about to be
  138. processed a second time.
  139. C<follow_skip==2> causes File::Find to ignore any duplicate files and
  140. directories but to proceed normally otherwise.
  141.  
  142. =item C<dangling_symlinks>
  143.  
  144. If true and a code reference, will be called with the symbolic link
  145. name and the directory it lives in as arguments.  Otherwise, if true
  146. and warnings are on, warning "symbolic_link_name is a dangling
  147. symbolic link\n" will be issued.  If false, the dangling symbolic link
  148. will be silently ignored.
  149.  
  150. =item C<no_chdir>
  151.  
  152. Does not C<chdir()> to each directory as it recurses. The wanted()
  153. function will need to be aware of this, of course. In this case,
  154. C<$_> will be the same as C<$File::Find::name>.
  155.  
  156. =item C<untaint>
  157.  
  158. If find is used in taint-mode (-T command line switch or if EUID != UID
  159. or if EGID != GID) then internally directory names have to be untainted
  160. before they can be chdir'ed to. Therefore they are checked against a regular
  161. expression I<untaint_pattern>.  Note that all names passed to the user's 
  162. I<wanted()> function are still tainted. If this option is used while 
  163. not in taint-mode, C<untaint> is a no-op.
  164.  
  165. =item C<untaint_pattern>
  166.  
  167. See above. This should be set using the C<qr> quoting operator.
  168. The default is set to  C<qr|^([-+@\w./]+)$|>. 
  169. Note that the parentheses are vital.
  170.  
  171. =item C<untaint_skip>
  172.  
  173. If set, a directory which fails the I<untaint_pattern> is skipped, 
  174. including all its sub-directories. The default is to 'die' in such a case.
  175.  
  176. =back
  177.  
  178. =head2 The wanted function
  179.  
  180. The wanted() function does whatever verifications you want on each
  181. file and directory.  It takes no arguments but rather does its work
  182. through a collection of variables.
  183.  
  184. =over 4
  185.  
  186. =item C<$File::Find::dir> is the current directory name,
  187.  
  188. =item C<$_> is the current filename within that directory
  189.  
  190. =item C<$File::Find::name> is the complete pathname to the file.
  191.  
  192. =back
  193.  
  194. Don't modify these variables.
  195.  
  196. For example, when examining the file /some/path/foo.ext you will have:
  197.  
  198.     $File::Find::dir  = /some/path/
  199.     $_                = foo.ext
  200.     $File::Find::name = /some/path/foo.ext
  201.  
  202. You are chdir()'d toC<$File::Find::dir> when the function is called,
  203. unless C<no_chdir> was specified. Note that when changing to
  204. directories is in effect the root directory (F</>) is a somewhat
  205. special case inasmuch as the concatenation of C<$File::Find::dir>,
  206. C<'/'> and C<$_> is not literally equal to C<$File::Find::name>. The
  207. table below summarizes all variants:
  208.  
  209.               $File::Find::name  $File::Find::dir  $_
  210.  default      /                  /                 .
  211.  no_chdir=>0  /etc               /                 etc
  212.               /etc/x             /etc              x
  213.  
  214.  no_chdir=>1  /                  /                 /
  215.               /etc               /                 /etc
  216.               /etc/x             /etc              /etc/x
  217.  
  218.  
  219. When <follow> or <follow_fast> are in effect, there is
  220. also a C<$File::Find::fullname>.  The function may set
  221. C<$File::Find::prune> to prune the tree unless C<bydepth> was
  222. specified.  Unless C<follow> or C<follow_fast> is specified, for
  223. compatibility reasons (find.pl, find2perl) there are in addition the
  224. following globals available: C<$File::Find::topdir>,
  225. C<$File::Find::topdev>, C<$File::Find::topino>,
  226. C<$File::Find::topmode> and C<$File::Find::topnlink>.
  227.  
  228. This library is useful for the C<find2perl> tool, which when fed,
  229.  
  230.     find2perl / -name .nfs\* -mtime +7 \
  231.         -exec rm -f {} \; -o -fstype nfs -prune
  232.  
  233. produces something like:
  234.  
  235.     sub wanted {
  236.         /^\.nfs.*\z/s &&
  237.         (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_)) &&
  238.         int(-M _) > 7 &&
  239.         unlink($_)
  240.         ||
  241.         ($nlink || (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_))) &&
  242.         $dev < 0 &&
  243.         ($File::Find::prune = 1);
  244.     }
  245.  
  246. Notice the C<_> in the above C<int(-M _)>: the C<_> is a magical
  247. filehandle that caches the information from the preceding
  248. stat(), lstat(), or filetest.
  249.  
  250. Here's another interesting wanted function.  It will find all symbolic
  251. links that don't resolve:
  252.  
  253.     sub wanted {
  254.          -l && !-e && print "bogus link: $File::Find::name\n";
  255.     }
  256.  
  257. See also the script C<pfind> on CPAN for a nice application of this
  258. module.
  259.  
  260. =head1 WARNINGS
  261.  
  262. If you run your program with the C<-w> switch, or if you use the
  263. C<warnings> pragma, File::Find will report warnings for several weird
  264. situations. You can disable these warnings by putting the statement
  265.  
  266.     no warnings 'File::Find';
  267.  
  268. in the appropriate scope. See L<perllexwarn> for more info about lexical
  269. warnings.
  270.  
  271. =head1 CAVEAT
  272.  
  273. =over 2
  274.  
  275. =item $dont_use_nlink
  276.  
  277. You can set the variable C<$File::Find::dont_use_nlink> to 1, if you want to
  278. force File::Find to always stat directories. This was used for file systems
  279. that do not have an C<nlink> count matching the number of sub-directories.
  280. Examples are ISO-9660 (CD-ROM), AFS, HPFS (OS/2 file system), FAT (DOS file
  281. system) and a couple of others.
  282.  
  283. You shouldn't need to set this variable, since File::Find should now detect
  284. such file systems on-the-fly and switch itself to using stat. This works even
  285. for parts of your file system, like a mounted CD-ROM.
  286.  
  287. If you do set C<$File::Find::dont_use_nlink> to 1, you will notice slow-downs.
  288.  
  289. =item symlinks
  290.  
  291. Be aware that the option to follow symbolic links can be dangerous.
  292. Depending on the structure of the directory tree (including symbolic
  293. links to directories) you might traverse a given (physical) directory
  294. more than once (only if C<follow_fast> is in effect). 
  295. Furthermore, deleting or changing files in a symbolically linked directory
  296. might cause very unpleasant surprises, since you delete or change files
  297. in an unknown directory.
  298.  
  299. =back
  300.  
  301. =head1 NOTES
  302.  
  303. =over 4
  304.  
  305. =item *
  306.  
  307. Mac OS (Classic) users should note a few differences:
  308.  
  309. =over 4
  310.  
  311. =item *   
  312.  
  313. The path separator is ':', not '/', and the current directory is denoted 
  314. as ':', not '.'. You should be careful about specifying relative pathnames. 
  315. While a full path always begins with a volume name, a relative pathname 
  316. should always begin with a ':'.  If specifying a volume name only, a 
  317. trailing ':' is required.
  318.  
  319. =item *   
  320.  
  321. C<$File::Find::dir> is guaranteed to end with a ':'. If C<$_> 
  322. contains the name of a directory, that name may or may not end with a 
  323. ':'. Likewise, C<$File::Find::name>, which contains the complete 
  324. pathname to that directory, and C<$File::Find::fullname>, which holds 
  325. the absolute pathname of that directory with all symbolic links resolved,
  326. may or may not end with a ':'.
  327.  
  328. =item *   
  329.  
  330. The default C<untaint_pattern> (see above) on Mac OS is set to  
  331. C<qr|^(.+)$|>. Note that the parentheses are vital.
  332.  
  333. =item *   
  334.  
  335. The invisible system file "Icon\015" is ignored. While this file may 
  336. appear in every directory, there are some more invisible system files 
  337. on every volume, which are all located at the volume root level (i.e. 
  338. "MacintoshHD:"). These system files are B<not> excluded automatically. 
  339. Your filter may use the following code to recognize invisible files or 
  340. directories (requires Mac::Files):
  341.  
  342.  use Mac::Files;
  343.  
  344.  # invisible() --  returns 1 if file/directory is invisible,  
  345.  # 0 if it's visible or undef if an error occurred
  346.  
  347.  sub invisible($) { 
  348.    my $file = shift;
  349.    my ($fileCat, $fileInfo); 
  350.    my $invisible_flag =  1 << 14; 
  351.  
  352.    if ( $fileCat = FSpGetCatInfo($file) ) {
  353.      if ($fileInfo = $fileCat->ioFlFndrInfo() ) {
  354.        return (($fileInfo->fdFlags & $invisible_flag) && 1);
  355.      }
  356.    }
  357.    return undef;
  358.  }
  359.  
  360. Generally, invisible files are system files, unless an odd application 
  361. decides to use invisible files for its own purposes. To distinguish 
  362. such files from system files, you have to look at the B<type> and B<creator> 
  363. file attributes. The MacPerl built-in functions C<GetFileInfo(FILE)> and 
  364. C<SetFileInfo(CREATOR, TYPE, FILES)> offer access to these attributes 
  365. (see MacPerl.pm for details).
  366.  
  367. Files that appear on the desktop actually reside in an (hidden) directory
  368. named "Desktop Folder" on the particular disk volume. Note that, although
  369. all desktop files appear to be on the same "virtual" desktop, each disk 
  370. volume actually maintains its own "Desktop Folder" directory.
  371.  
  372. =back
  373.  
  374. =back
  375.  
  376. =head1 HISTORY
  377.  
  378. File::Find used to produce incorrect results if called recursively.
  379. During the development of perl 5.8 this bug was fixed.
  380. The first fixed version of File::Find was 1.01.
  381.  
  382. =cut
  383.  
  384. our @ISA = qw(Exporter);
  385. our @EXPORT = qw(find finddepth);
  386.  
  387.  
  388. use strict;
  389. my $Is_VMS;
  390. my $Is_MacOS;
  391.  
  392. require File::Basename;
  393. require File::Spec;
  394.  
  395. # Should ideally be my() not our() but local() currently
  396. # refuses to operate on lexicals
  397.  
  398. our %SLnkSeen;
  399. our ($wanted_callback, $avoid_nlink, $bydepth, $no_chdir, $follow,
  400.     $follow_skip, $full_check, $untaint, $untaint_skip, $untaint_pat,
  401.     $pre_process, $post_process, $dangling_symlinks);
  402.  
  403. sub contract_name {
  404.     my ($cdir,$fn) = @_;
  405.  
  406.     return substr($cdir,0,rindex($cdir,'/')) if $fn eq $File::Find::current_dir;
  407.  
  408.     $cdir = substr($cdir,0,rindex($cdir,'/')+1);
  409.  
  410.     $fn =~ s|^\./||;
  411.  
  412.     my $abs_name= $cdir . $fn;
  413.  
  414.     if (substr($fn,0,3) eq '../') {
  415.        1 while $abs_name =~ s!/[^/]*/\.\./!/!;
  416.     }
  417.  
  418.     return $abs_name;
  419. }
  420.  
  421. # return the absolute name of a directory or file
  422. sub contract_name_Mac {
  423.     my ($cdir,$fn) = @_; 
  424.     my $abs_name;
  425.  
  426.     if ($fn =~ /^(:+)(.*)$/) { # valid pathname starting with a ':'
  427.  
  428.     my $colon_count = length ($1);
  429.     if ($colon_count == 1) {
  430.         $abs_name = $cdir . $2;
  431.         return $abs_name;
  432.     }
  433.     else { 
  434.         # need to move up the tree, but 
  435.         # only if it's not a volume name
  436.         for (my $i=1; $i<$colon_count; $i++) {
  437.         unless ($cdir =~ /^[^:]+:$/) { # volume name
  438.             $cdir =~ s/[^:]+:$//;
  439.         }
  440.         else {
  441.             return undef;
  442.         }
  443.         }
  444.         $abs_name = $cdir . $2;
  445.         return $abs_name;
  446.     }
  447.  
  448.     }
  449.     else {
  450.  
  451.     # $fn may be a valid path to a directory or file or (dangling)
  452.     # symlink, without a leading ':'
  453.     if ( (-e $fn) || (-l $fn) ) {
  454.         if ($fn =~ /^[^:]+:/) { # a volume name like DataHD:*
  455.         return $fn; # $fn is already an absolute path
  456.         }
  457.         else {
  458.         $abs_name = $cdir . $fn;
  459.         return $abs_name;
  460.         }
  461.     }
  462.     else { # argh!, $fn is not a valid directory/file 
  463.          return undef;
  464.     }
  465.     }
  466. }
  467.  
  468. sub PathCombine($$) {
  469.     my ($Base,$Name) = @_;
  470.     my $AbsName;
  471.  
  472.     if ($Is_MacOS) {
  473.     # $Name is the resolved symlink (always a full path on MacOS),
  474.     # i.e. there's no need to call contract_name_Mac()
  475.     $AbsName = $Name; 
  476.  
  477.     # (simple) check for recursion
  478.     if ( ( $Base =~ /^$AbsName/) && (-d $AbsName) ) { # recursion
  479.         return undef;
  480.     }
  481.     }
  482.     else {
  483.     if (substr($Name,0,1) eq '/') {
  484.         $AbsName= $Name;
  485.     }
  486.     else {
  487.         $AbsName= contract_name($Base,$Name);
  488.     }
  489.  
  490.     # (simple) check for recursion
  491.     my $newlen= length($AbsName);
  492.     if ($newlen <= length($Base)) {
  493.         if (($newlen == length($Base) || substr($Base,$newlen,1) eq '/')
  494.         && $AbsName eq substr($Base,0,$newlen))
  495.         {
  496.         return undef;
  497.         }
  498.     }
  499.     }
  500.     return $AbsName;
  501. }
  502.  
  503. sub Follow_SymLink($) {
  504.     my ($AbsName) = @_;
  505.  
  506.     my ($NewName,$DEV, $INO);
  507.     ($DEV, $INO)= lstat $AbsName;
  508.  
  509.     while (-l _) {
  510.     if ($SLnkSeen{$DEV, $INO}++) {
  511.         if ($follow_skip < 2) {
  512.         die "$AbsName is encountered a second time";
  513.         }
  514.         else {
  515.         return undef;
  516.         }
  517.     }
  518.     $NewName= PathCombine($AbsName, readlink($AbsName));
  519.     unless(defined $NewName) {
  520.         if ($follow_skip < 2) {
  521.         die "$AbsName is a recursive symbolic link";
  522.         }
  523.         else {
  524.         return undef;
  525.         }
  526.     }
  527.     else {
  528.         $AbsName= $NewName;
  529.     }
  530.     ($DEV, $INO) = lstat($AbsName);
  531.     return undef unless defined $DEV;  #  dangling symbolic link
  532.     }
  533.  
  534.     if ($full_check && defined $DEV && $SLnkSeen{$DEV, $INO}++) {
  535.     if ( ($follow_skip < 1) || ((-d _) && ($follow_skip < 2)) ) {
  536.         die "$AbsName encountered a second time";
  537.     }
  538.     else {
  539.         return undef;
  540.     }
  541.     }
  542.  
  543.     return $AbsName;
  544. }
  545.  
  546. our($dir, $name, $fullname, $prune);
  547. sub _find_dir_symlnk($$$);
  548. sub _find_dir($$$);
  549.  
  550. # check whether or not a scalar variable is tainted
  551. # (code straight from the Camel, 3rd ed., page 561)
  552. sub is_tainted_pp {
  553.     my $arg = shift;
  554.     my $nada = substr($arg, 0, 0); # zero-length
  555.     local $@;
  556.     eval { eval "# $nada" };
  557.     return length($@) != 0;
  558.  
  559. sub _find_opt {
  560.     my $wanted = shift;
  561.     die "invalid top directory" unless defined $_[0];
  562.  
  563.     # This function must local()ize everything because callbacks may
  564.     # call find() or finddepth()
  565.  
  566.     local %SLnkSeen;
  567.     local ($wanted_callback, $avoid_nlink, $bydepth, $no_chdir, $follow,
  568.     $follow_skip, $full_check, $untaint, $untaint_skip, $untaint_pat,
  569.     $pre_process, $post_process, $dangling_symlinks);
  570.     local($dir, $name, $fullname, $prune);
  571.  
  572.     my $cwd            = $wanted->{bydepth} ? Cwd::fastcwd() : Cwd::getcwd();
  573.     my $cwd_untainted  = $cwd;
  574.     my $check_t_cwd    = 1;
  575.     $wanted_callback   = $wanted->{wanted};
  576.     $bydepth           = $wanted->{bydepth};
  577.     $pre_process       = $wanted->{preprocess};
  578.     $post_process      = $wanted->{postprocess};
  579.     $no_chdir          = $wanted->{no_chdir};
  580.     $full_check        = $wanted->{follow};
  581.     $follow            = $full_check || $wanted->{follow_fast};
  582.     $follow_skip       = $wanted->{follow_skip};
  583.     $untaint           = $wanted->{untaint};
  584.     $untaint_pat       = $wanted->{untaint_pattern};
  585.     $untaint_skip      = $wanted->{untaint_skip};
  586.     $dangling_symlinks = $wanted->{dangling_symlinks};
  587.  
  588.     # for compatibility reasons (find.pl, find2perl)
  589.     local our ($topdir, $topdev, $topino, $topmode, $topnlink);
  590.  
  591.     # a symbolic link to a directory doesn't increase the link count
  592.     $avoid_nlink      = $follow || $File::Find::dont_use_nlink;
  593.     
  594.     my ($abs_dir, $Is_Dir);
  595.  
  596.     Proc_Top_Item:
  597.     foreach my $TOP (@_) {
  598.     my $top_item = $TOP;
  599.  
  600.     if ($Is_MacOS) {
  601.         ($topdev,$topino,$topmode,$topnlink) = $follow ? stat $top_item : lstat $top_item;
  602.         $top_item = ":$top_item"
  603.         if ( (-d _) && ( $top_item !~ /:/ ) );
  604.     }
  605.     else {
  606.         $top_item =~ s|/\z|| unless $top_item eq '/';
  607.         ($topdev,$topino,$topmode,$topnlink) = $follow ? stat $top_item : lstat $top_item;
  608.     }
  609.  
  610.     $Is_Dir= 0;
  611.  
  612.     if ($follow) {
  613.  
  614.         if ($Is_MacOS) {
  615.         $cwd = "$cwd:" unless ($cwd =~ /:$/); # for safety
  616.  
  617.         if ($top_item eq $File::Find::current_dir) {
  618.             $abs_dir = $cwd;
  619.         }
  620.         else {
  621.             $abs_dir = contract_name_Mac($cwd, $top_item);
  622.             unless (defined $abs_dir) {
  623.             warnings::warnif "Can't determine absolute path for $top_item (No such file or directory)\n";
  624.             next Proc_Top_Item;
  625.             }
  626.         }
  627.  
  628.         }
  629.         else {
  630.         if (substr($top_item,0,1) eq '/') {
  631.             $abs_dir = $top_item;
  632.         }
  633.         elsif ($top_item eq $File::Find::current_dir) {
  634.             $abs_dir = $cwd;
  635.         }
  636.         else {  # care about any  ../
  637.             $abs_dir = contract_name("$cwd/",$top_item);
  638.         }
  639.         }
  640.         $abs_dir= Follow_SymLink($abs_dir);
  641.         unless (defined $abs_dir) {
  642.         if ($dangling_symlinks) {
  643.             if (ref $dangling_symlinks eq 'CODE') {
  644.             $dangling_symlinks->($top_item, $cwd);
  645.             } else {
  646.             warnings::warnif "$top_item is a dangling symbolic link\n";
  647.             }
  648.         }
  649.         next Proc_Top_Item;
  650.         }
  651.  
  652.         if (-d _) {
  653.         _find_dir_symlnk($wanted, $abs_dir, $top_item);
  654.         $Is_Dir= 1;
  655.         }
  656.     }
  657.     else { # no follow
  658.         $topdir = $top_item;
  659.         unless (defined $topnlink) {
  660.         warnings::warnif "Can't stat $top_item: $!\n";
  661.         next Proc_Top_Item;
  662.         }
  663.         if (-d _) {
  664.         $top_item =~ s/\.dir\z// if $Is_VMS;
  665.         _find_dir($wanted, $top_item, $topnlink);
  666.         $Is_Dir= 1;
  667.         }
  668.         else {
  669.         $abs_dir= $top_item;
  670.         }
  671.     }
  672.  
  673.     unless ($Is_Dir) {
  674.         unless (($_,$dir) = File::Basename::fileparse($abs_dir)) {
  675.         if ($Is_MacOS) {
  676.             ($dir,$_) = (':', $top_item); # $File::Find::dir, $_
  677.         }
  678.         else {
  679.             ($dir,$_) = ('./', $top_item);
  680.         }
  681.         }
  682.  
  683.         $abs_dir = $dir;
  684.         if (( $untaint ) && (is_tainted($dir) )) {
  685.         ( $abs_dir ) = $dir =~ m|$untaint_pat|;
  686.         unless (defined $abs_dir) {
  687.             if ($untaint_skip == 0) {
  688.             die "directory $dir is still tainted";
  689.             }
  690.             else {
  691.             next Proc_Top_Item;
  692.             }
  693.         }
  694.         }
  695.  
  696.         unless ($no_chdir || chdir $abs_dir) {
  697.         warnings::warnif "Couldn't chdir $abs_dir: $!\n";
  698.         next Proc_Top_Item;
  699.         }
  700.  
  701.         $name = $abs_dir . $_; # $File::Find::name
  702.  
  703.         { $wanted_callback->() }; # protect against wild "next"
  704.  
  705.     }
  706.  
  707.     unless ( $no_chdir ) {
  708.         if ( ($check_t_cwd) && (($untaint) && (is_tainted($cwd) )) ) {
  709.         ( $cwd_untainted ) = $cwd =~ m|$untaint_pat|;
  710.         unless (defined $cwd_untainted) {
  711.             die "insecure cwd in find(depth)";
  712.         }
  713.         $check_t_cwd = 0;
  714.         }
  715.         unless (chdir $cwd_untainted) {
  716.         die "Can't cd to $cwd: $!\n";
  717.         }
  718.     }
  719.     }
  720. }
  721.  
  722. # API:
  723. #  $wanted
  724. #  $p_dir :  "parent directory"
  725. #  $nlink :  what came back from the stat
  726. # preconditions:
  727. #  chdir (if not no_chdir) to dir
  728.  
  729. sub _find_dir($$$) {
  730.     my ($wanted, $p_dir, $nlink) = @_;
  731.     my ($CdLvl,$Level) = (0,0);
  732.     my @Stack;
  733.     my @filenames;
  734.     my ($subcount,$sub_nlink);
  735.     my $SE= [];
  736.     my $dir_name= $p_dir;
  737.     my $dir_pref;
  738.     my $dir_rel = $File::Find::current_dir;
  739.     my $tainted = 0;
  740.     my $no_nlink;
  741.  
  742.     if ($Is_MacOS) {
  743.     $dir_pref= ($p_dir =~ /:$/) ? $p_dir : "$p_dir:"; # preface
  744.     }
  745.     else {
  746.     $dir_pref= ( $p_dir eq '/' ? '/' : "$p_dir/" );
  747.     }
  748.  
  749.     local ($dir, $name, $prune, *DIR);
  750.  
  751.     unless ( $no_chdir || ($p_dir eq $File::Find::current_dir)) {
  752.     my $udir = $p_dir;
  753.     if (( $untaint ) && (is_tainted($p_dir) )) {
  754.         ( $udir ) = $p_dir =~ m|$untaint_pat|;
  755.         unless (defined $udir) {
  756.         if ($untaint_skip == 0) {
  757.             die "directory $p_dir is still tainted";
  758.         }
  759.         else {
  760.             return;
  761.         }
  762.         }
  763.     }
  764.     unless (chdir $udir) {
  765.         warnings::warnif "Can't cd to $udir: $!\n";
  766.         return;
  767.     }
  768.     }
  769.  
  770.     # push the starting directory
  771.     push @Stack,[$CdLvl,$p_dir,$dir_rel,-1]  if  $bydepth;
  772.  
  773.     if ($Is_MacOS) {
  774.     $p_dir = $dir_pref;  # ensure trailing ':'
  775.     }
  776.  
  777.     while (defined $SE) {
  778.     unless ($bydepth) {
  779.         $dir= $p_dir; # $File::Find::dir 
  780.         $name= $dir_name; # $File::Find::name 
  781.         $_= ($no_chdir ? $dir_name : $dir_rel ); # $_
  782.         # prune may happen here
  783.         $prune= 0;
  784.         { $wanted_callback->() };    # protect against wild "next"
  785.         next if $prune;
  786.     }
  787.  
  788.     # change to that directory
  789.     unless ($no_chdir || ($dir_rel eq $File::Find::current_dir)) {
  790.         my $udir= $dir_rel;
  791.         if ( ($untaint) && (($tainted) || ($tainted = is_tainted($dir_rel) )) ) {
  792.         ( $udir ) = $dir_rel =~ m|$untaint_pat|;
  793.         unless (defined $udir) {
  794.             if ($untaint_skip == 0) {
  795.             if ($Is_MacOS) {
  796.                 die "directory ($p_dir) $dir_rel is still tainted";
  797.             }
  798.             else {
  799.                 die "directory (" . ($p_dir ne '/' ? $p_dir : '') . "/) $dir_rel is still tainted";
  800.             }
  801.             } else { # $untaint_skip == 1
  802.             next; 
  803.             }
  804.         }
  805.         }
  806.         unless (chdir $udir) {
  807.         if ($Is_MacOS) {
  808.             warnings::warnif "Can't cd to ($p_dir) $udir: $!\n";
  809.         }
  810.         else {
  811.             warnings::warnif "Can't cd to (" .
  812.             ($p_dir ne '/' ? $p_dir : '') . "/) $udir: $!\n";
  813.         }
  814.         next;
  815.         }
  816.         $CdLvl++;
  817.     }
  818.  
  819.     if ($Is_MacOS) {
  820.         $dir_name = "$dir_name:" unless ($dir_name =~ /:$/);
  821.     }
  822.  
  823.     $dir= $dir_name; # $File::Find::dir 
  824.  
  825.     # Get the list of files in the current directory.
  826.     unless (opendir DIR, ($no_chdir ? $dir_name : $File::Find::current_dir)) {
  827.         warnings::warnif "Can't opendir($dir_name): $!\n";
  828.         next;
  829.     }
  830.     @filenames = readdir DIR;
  831.     closedir(DIR);
  832.     @filenames = $pre_process->(@filenames) if $pre_process;
  833.     push @Stack,[$CdLvl,$dir_name,"",-2]   if $post_process;
  834.  
  835.     # default: use whatever was specifid
  836.         # (if $nlink >= 2, and $avoid_nlink == 0, this will switch back)
  837.         $no_nlink = $avoid_nlink;
  838.         # if dir has wrong nlink count, force switch to slower stat method
  839.         $no_nlink = 1 if ($nlink < 2);
  840.  
  841.     if ($nlink == 2 && !$no_nlink) {
  842.         # This dir has no subdirectories.
  843.         for my $FN (@filenames) {
  844.         next if $FN =~ $File::Find::skip_pattern;
  845.         
  846.         $name = $dir_pref . $FN; # $File::Find::name
  847.         $_ = ($no_chdir ? $name : $FN); # $_
  848.         { $wanted_callback->() }; # protect against wild "next"
  849.         }
  850.  
  851.     }
  852.     else {
  853.         # This dir has subdirectories.
  854.         $subcount = $nlink - 2;
  855.  
  856.         for my $FN (@filenames) {
  857.         next if $FN =~ $File::Find::skip_pattern;
  858.         if ($subcount > 0 || $no_nlink) {
  859.             # Seen all the subdirs?
  860.             # check for directoriness.
  861.             # stat is faster for a file in the current directory
  862.             $sub_nlink = (lstat ($no_chdir ? $dir_pref . $FN : $FN))[3];
  863.  
  864.             if (-d _) {
  865.             --$subcount;
  866.             $FN =~ s/\.dir\z// if $Is_VMS;
  867.             push @Stack,[$CdLvl,$dir_name,$FN,$sub_nlink];
  868.             }
  869.             else {
  870.             $name = $dir_pref . $FN; # $File::Find::name
  871.             $_= ($no_chdir ? $name : $FN); # $_
  872.             { $wanted_callback->() }; # protect against wild "next"
  873.             }
  874.         }
  875.         else {
  876.             $name = $dir_pref . $FN; # $File::Find::name
  877.             $_= ($no_chdir ? $name : $FN); # $_
  878.             { $wanted_callback->() }; # protect against wild "next"
  879.         }
  880.         }
  881.     }
  882.     }
  883.     continue {
  884.     while ( defined ($SE = pop @Stack) ) {
  885.         ($Level, $p_dir, $dir_rel, $nlink) = @$SE;
  886.         if ($CdLvl > $Level && !$no_chdir) {
  887.         my $tmp;
  888.         if ($Is_MacOS) {
  889.             $tmp = (':' x ($CdLvl-$Level)) . ':';
  890.         }
  891.         else {
  892.             $tmp = join('/',('..') x ($CdLvl-$Level));
  893.         }
  894.         die "Can't cd to $dir_name" . $tmp
  895.             unless chdir ($tmp);
  896.         $CdLvl = $Level;
  897.         }
  898.  
  899.         if ($Is_MacOS) {
  900.         # $pdir always has a trailing ':', except for the starting dir,
  901.         # where $dir_rel eq ':'
  902.         $dir_name = "$p_dir$dir_rel";
  903.         $dir_pref = "$dir_name:";
  904.         }
  905.         else {
  906.         $dir_name = ($p_dir eq '/' ? "/$dir_rel" : "$p_dir/$dir_rel");
  907.         $dir_pref = "$dir_name/";
  908.         }
  909.  
  910.         if ( $nlink == -2 ) {
  911.         $name = $dir = $p_dir; # $File::Find::name / dir
  912.                 $_ = $File::Find::current_dir;
  913.         $post_process->();        # End-of-directory processing
  914.         }
  915.         elsif ( $nlink < 0 ) {  # must be finddepth, report dirname now
  916.         $name = $dir_name;
  917.         if ($Is_MacOS) {
  918.             if ($dir_rel eq ':') { # must be the top dir, where we started
  919.             $name =~ s|:$||; # $File::Find::name
  920.             $p_dir = "$p_dir:" unless ($p_dir =~ /:$/);
  921.             }
  922.             $dir = $p_dir; # $File::Find::dir
  923.             $_ = ($no_chdir ? $name : $dir_rel); # $_
  924.         }
  925.         else {
  926.             if ( substr($name,-2) eq '/.' ) {
  927.             substr($name, length($name) == 2 ? -1 : -2) = '';
  928.             }
  929.             $dir = $p_dir;
  930.             $_ = ($no_chdir ? $dir_name : $dir_rel );
  931.             if ( substr($_,-2) eq '/.' ) {
  932.             substr($_, length($_) == 2 ? -1 : -2) = '';
  933.             }
  934.         }
  935.         { $wanted_callback->() }; # protect against wild "next"
  936.          }
  937.          else {
  938.         push @Stack,[$CdLvl,$p_dir,$dir_rel,-1]  if  $bydepth;
  939.         last;
  940.         }
  941.     }
  942.     }
  943. }
  944.  
  945.  
  946. # API:
  947. #  $wanted
  948. #  $dir_loc : absolute location of a dir
  949. #  $p_dir   : "parent directory"
  950. # preconditions:
  951. #  chdir (if not no_chdir) to dir
  952.  
  953. sub _find_dir_symlnk($$$) {
  954.     my ($wanted, $dir_loc, $p_dir) = @_; # $dir_loc is the absolute directory
  955.     my @Stack;
  956.     my @filenames;
  957.     my $new_loc;
  958.     my $updir_loc = $dir_loc; # untainted parent directory
  959.     my $SE = [];
  960.     my $dir_name = $p_dir;
  961.     my $dir_pref;
  962.     my $loc_pref;
  963.     my $dir_rel = $File::Find::current_dir;
  964.     my $byd_flag; # flag for pending stack entry if $bydepth
  965.     my $tainted = 0;
  966.     my $ok = 1;
  967.  
  968.     if ($Is_MacOS) {
  969.     $dir_pref = ($p_dir =~ /:$/) ? "$p_dir" : "$p_dir:";
  970.     $loc_pref = ($dir_loc =~ /:$/) ? "$dir_loc" : "$dir_loc:";
  971.     } else {
  972.     $dir_pref = ( $p_dir   eq '/' ? '/' : "$p_dir/" );
  973.     $loc_pref = ( $dir_loc eq '/' ? '/' : "$dir_loc/" );
  974.     }
  975.  
  976.     local ($dir, $name, $fullname, $prune, *DIR);
  977.  
  978.     unless ($no_chdir) {
  979.     # untaint the topdir
  980.     if (( $untaint ) && (is_tainted($dir_loc) )) {
  981.         ( $updir_loc ) = $dir_loc =~ m|$untaint_pat|; # parent dir, now untainted
  982.          # once untainted, $updir_loc is pushed on the stack (as parent directory);
  983.         # hence, we don't need to untaint the parent directory every time we chdir 
  984.         # to it later 
  985.         unless (defined $updir_loc) {
  986.         if ($untaint_skip == 0) {
  987.             die "directory $dir_loc is still tainted";
  988.         }
  989.         else {
  990.             return;
  991.         }
  992.         }
  993.     }
  994.     $ok = chdir($updir_loc) unless ($p_dir eq $File::Find::current_dir);
  995.     unless ($ok) {
  996.         warnings::warnif "Can't cd to $updir_loc: $!\n";
  997.         return;
  998.     }
  999.     }
  1000.  
  1001.     push @Stack,[$dir_loc,$updir_loc,$p_dir,$dir_rel,-1]  if  $bydepth;
  1002.  
  1003.     if ($Is_MacOS) {
  1004.     $p_dir = $dir_pref; # ensure trailing ':'
  1005.     }
  1006.  
  1007.     while (defined $SE) {
  1008.  
  1009.     unless ($bydepth) {
  1010.         # change (back) to parent directory (always untainted)
  1011.         unless ($no_chdir) {
  1012.         unless (chdir $updir_loc) {
  1013.             warnings::warnif "Can't cd to $updir_loc: $!\n";
  1014.             next;
  1015.         }
  1016.         }
  1017.         $dir= $p_dir; # $File::Find::dir
  1018.         $name= $dir_name; # $File::Find::name
  1019.         $_= ($no_chdir ? $dir_name : $dir_rel ); # $_
  1020.         $fullname= $dir_loc; # $File::Find::fullname
  1021.         # prune may happen here
  1022.         $prune= 0;
  1023.         lstat($_); # make sure  file tests with '_' work
  1024.         { $wanted_callback->() }; # protect against wild "next"
  1025.         next if $prune;
  1026.     }
  1027.  
  1028.     # change to that directory
  1029.     unless ($no_chdir || ($dir_rel eq $File::Find::current_dir)) {
  1030.         $updir_loc = $dir_loc;
  1031.         if ( ($untaint) && (($tainted) || ($tainted = is_tainted($dir_loc) )) ) {
  1032.         # untaint $dir_loc, what will be pushed on the stack as (untainted) parent dir 
  1033.         ( $updir_loc ) = $dir_loc =~ m|$untaint_pat|;
  1034.         unless (defined $updir_loc) {
  1035.             if ($untaint_skip == 0) {
  1036.             die "directory $dir_loc is still tainted";
  1037.             }
  1038.             else {
  1039.             next;
  1040.             }
  1041.         }
  1042.         }
  1043.         unless (chdir $updir_loc) {
  1044.         warnings::warnif "Can't cd to $updir_loc: $!\n";
  1045.         next;
  1046.         }
  1047.     }
  1048.  
  1049.     if ($Is_MacOS) {
  1050.         $dir_name = "$dir_name:" unless ($dir_name =~ /:$/);
  1051.     }
  1052.  
  1053.     $dir = $dir_name; # $File::Find::dir
  1054.  
  1055.     # Get the list of files in the current directory.
  1056.     unless (opendir DIR, ($no_chdir ? $dir_loc : $File::Find::current_dir)) {
  1057.         warnings::warnif "Can't opendir($dir_loc): $!\n";
  1058.         next;
  1059.     }
  1060.     @filenames = readdir DIR;
  1061.     closedir(DIR);
  1062.  
  1063.     for my $FN (@filenames) {
  1064.         next if $FN =~ $File::Find::skip_pattern;
  1065.  
  1066.         # follow symbolic links / do an lstat
  1067.         $new_loc = Follow_SymLink($loc_pref.$FN);
  1068.  
  1069.         # ignore if invalid symlink
  1070.         next unless defined $new_loc;
  1071.  
  1072.         if (-d _) {
  1073.         push @Stack,[$new_loc,$updir_loc,$dir_name,$FN,1];
  1074.         }
  1075.         else {
  1076.         $fullname = $new_loc; # $File::Find::fullname 
  1077.         $name = $dir_pref . $FN; # $File::Find::name
  1078.         $_ = ($no_chdir ? $name : $FN); # $_
  1079.         { $wanted_callback->() }; # protect against wild "next"
  1080.         }
  1081.     }
  1082.  
  1083.     }
  1084.     continue {
  1085.     while (defined($SE = pop @Stack)) {
  1086.         ($dir_loc, $updir_loc, $p_dir, $dir_rel, $byd_flag) = @$SE;
  1087.         if ($Is_MacOS) {
  1088.         # $p_dir always has a trailing ':', except for the starting dir,
  1089.         # where $dir_rel eq ':'
  1090.         $dir_name = "$p_dir$dir_rel";
  1091.         $dir_pref = "$dir_name:";
  1092.         $loc_pref = ($dir_loc =~ /:$/) ? $dir_loc : "$dir_loc:";
  1093.         }
  1094.         else {
  1095.         $dir_name = ($p_dir eq '/' ? "/$dir_rel" : "$p_dir/$dir_rel");
  1096.         $dir_pref = "$dir_name/";
  1097.         $loc_pref = "$dir_loc/";
  1098.         }
  1099.         if ( $byd_flag < 0 ) {  # must be finddepth, report dirname now
  1100.         unless ($no_chdir || ($dir_rel eq $File::Find::current_dir)) {
  1101.             unless (chdir $updir_loc) { # $updir_loc (parent dir) is always untainted 
  1102.             warnings::warnif "Can't cd to $updir_loc: $!\n";
  1103.             next;
  1104.             }
  1105.         }
  1106.         $fullname = $dir_loc; # $File::Find::fullname
  1107.         $name = $dir_name; # $File::Find::name
  1108.         if ($Is_MacOS) {
  1109.             if ($dir_rel eq ':') { # must be the top dir, where we started
  1110.             $name =~ s|:$||; # $File::Find::name
  1111.             $p_dir = "$p_dir:" unless ($p_dir =~ /:$/);
  1112.             }
  1113.             $dir = $p_dir; # $File::Find::dir
  1114.              $_ = ($no_chdir ? $name : $dir_rel); # $_
  1115.         }
  1116.         else {
  1117.             if ( substr($name,-2) eq '/.' ) {
  1118.             substr($name, length($name) == 2 ? -1 : -2) = ''; # $File::Find::name
  1119.             }
  1120.             $dir = $p_dir; # $File::Find::dir
  1121.             $_ = ($no_chdir ? $dir_name : $dir_rel); # $_
  1122.             if ( substr($_,-2) eq '/.' ) {
  1123.             substr($_, length($_) == 2 ? -1 : -2) = '';
  1124.             }
  1125.         }
  1126.  
  1127.         lstat($_); # make sure file tests with '_' work
  1128.         { $wanted_callback->() }; # protect against wild "next"
  1129.         }
  1130.         else {
  1131.         push @Stack,[$dir_loc, $updir_loc, $p_dir, $dir_rel,-1]  if  $bydepth;
  1132.         last;
  1133.         }
  1134.     }
  1135.     }
  1136. }
  1137.  
  1138.  
  1139. sub wrap_wanted {
  1140.     my $wanted = shift;
  1141.     if ( ref($wanted) eq 'HASH' ) {
  1142.     if ( $wanted->{follow} || $wanted->{follow_fast}) {
  1143.         $wanted->{follow_skip} = 1 unless defined $wanted->{follow_skip};
  1144.     }
  1145.     if ( $wanted->{untaint} ) {
  1146.         $wanted->{untaint_pattern} = $File::Find::untaint_pattern  
  1147.         unless defined $wanted->{untaint_pattern};
  1148.         $wanted->{untaint_skip} = 0 unless defined $wanted->{untaint_skip};
  1149.     }
  1150.     return $wanted;
  1151.     }
  1152.     else {
  1153.     return { wanted => $wanted };
  1154.     }
  1155. }
  1156.  
  1157. sub find {
  1158.     my $wanted = shift;
  1159.     _find_opt(wrap_wanted($wanted), @_);
  1160. }
  1161.  
  1162. sub finddepth {
  1163.     my $wanted = wrap_wanted(shift);
  1164.     $wanted->{bydepth} = 1;
  1165.     _find_opt($wanted, @_);
  1166. }
  1167.  
  1168. # default
  1169. $File::Find::skip_pattern    = qr/^\.{1,2}\z/;
  1170. $File::Find::untaint_pattern = qr|^([-+@\w./]+)$|;
  1171.  
  1172. # These are hard-coded for now, but may move to hint files.
  1173. if ($^O eq 'VMS') {
  1174.     $Is_VMS = 1;
  1175.     $File::Find::dont_use_nlink  = 1;
  1176. }
  1177. elsif ($^O eq 'MacOS') {
  1178.     $Is_MacOS = 1;
  1179.     $File::Find::dont_use_nlink  = 1;
  1180.     $File::Find::skip_pattern    = qr/^Icon\015\z/;
  1181.     $File::Find::untaint_pattern = qr|^(.+)$|;
  1182. }
  1183.  
  1184. # this _should_ work properly on all platforms
  1185. # where File::Find can be expected to work
  1186. $File::Find::current_dir = File::Spec->curdir || '.';
  1187.  
  1188. $File::Find::dont_use_nlink = 1
  1189.     if $^O eq 'os2' || $^O eq 'dos' || $^O eq 'amigaos' || $^O eq 'MSWin32' ||
  1190.        $^O eq 'cygwin' || $^O eq 'epoc' || $^O eq 'qnx' ||
  1191.        $^O eq 'nto';
  1192.  
  1193. # Set dont_use_nlink in your hint file if your system's stat doesn't
  1194. # report the number of links in a directory as an indication
  1195. # of the number of files.
  1196. # See, e.g. hints/machten.sh for MachTen 2.2.
  1197. unless ($File::Find::dont_use_nlink) {
  1198.     require Config;
  1199.     $File::Find::dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});
  1200. }
  1201.  
  1202. # We need a function that checks if a scalar is tainted. Either use the 
  1203. # Scalar::Util module's tainted() function or our (slower) pure Perl 
  1204. # fallback is_tainted_pp()
  1205. {
  1206.     local $@;
  1207.     eval { require Scalar::Util };
  1208.     *is_tainted = $@ ? \&is_tainted_pp : \&Scalar::Util::tainted;
  1209. }
  1210.  
  1211. 1;
  1212.