home *** CD-ROM | disk | FTP | other *** search
/ Freelog 15 / FREELOG 15.ISO / WebMaster / Perl / PERL5106.ZIP / perl5 / Lib / find.pl < prev    next >
Encoding:
Text File  |  1995-12-02  |  2.6 KB  |  111 lines

  1. # Usage:
  2. #    require "find.pl";
  3. #
  4. #    &find('/foo','/bar');
  5. #
  6. #    sub wanted { ... }
  7. #        where wanted does whatever you want.  $dir contains the
  8. #        current directory name, and $_ the current filename within
  9. #        that directory.  $name contains "$dir/$_".  You are cd'ed
  10. #        to $dir when the function is called.  The function may
  11. #        set $prune to prune the tree.
  12. #
  13. # This library is primarily for find2perl, which, when fed
  14. #
  15. #   find2perl / -name .nfs\* -mtime +7 -exec rm -f {} \; -o -fstype nfs -prune
  16. #
  17. # spits out something like this
  18. #
  19. #    sub wanted {
  20. #        /^\.nfs.*$/ &&
  21. #        (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
  22. #        int(-M _) > 7 &&
  23. #        unlink($_)
  24. #        ||
  25. #        ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
  26. #        $dev < 0 &&
  27. #        ($prune = 1);
  28. #    }
  29. #
  30. # Set the variable $dont_use_nlink if you're using AFS, since AFS cheats.
  31.  
  32. use Cwd;
  33.  
  34. sub find {
  35.     chop($cwd = cwd);
  36.     foreach $topdir (@_) {
  37.     (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
  38.       || (warn("Can't stat $topdir: $!\n"), next);
  39.     if (-d _) {
  40.         if (chdir($topdir)) {
  41.         ($dir,$_) = ($topdir,'.');
  42.         $name = $topdir;
  43.         &wanted;
  44.         ($fixtopdir = $topdir) =~ s,/$,, ;
  45.         &finddir($fixtopdir,$topnlink);
  46.         }
  47.         else {
  48.         warn "Can't cd to $topdir: $!\n";
  49.         }
  50.     }
  51.     else {
  52.         unless (($dir,$_) = $topdir =~ m#^(.*/)(.*)$#) {
  53.         ($dir,$_) = ('.', $topdir);
  54.         }
  55.         $name = $topdir;
  56.         chdir $dir && &wanted;
  57.     }
  58.     chdir $cwd;
  59.     }
  60. }
  61.  
  62. sub finddir {
  63.     local($dir,$nlink) = @_;
  64.     local($dev,$ino,$mode,$subcount);
  65.     local($name);
  66.  
  67.     # Get the list of files in the current directory.
  68.  
  69.     opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
  70.     local(@filenames) = readdir(DIR);
  71.     closedir(DIR);
  72.  
  73.     if ($nlink == 2 && !$dont_use_nlink) {  # This dir has no subdirectories.
  74.     for (@filenames) {
  75.         next if $_ eq '.';
  76.         next if $_ eq '..';
  77.         $name = "$dir/$_";
  78.         $nlink = 0;
  79.         &wanted;
  80.     }
  81.     }
  82.     else {                    # This dir has subdirectories.
  83.     $subcount = $nlink - 2;
  84.     for (@filenames) {
  85.         next if $_ eq '.';
  86.         next if $_ eq '..';
  87.         $nlink = $prune = 0;
  88.         $name = "$dir/$_";
  89.         &wanted;
  90.         if ($subcount > 0 || $dont_use_nlink) {    # Seen all the subdirs?
  91.  
  92.         # Get link count and check for directoriness.
  93.  
  94.         ($dev,$ino,$mode,$nlink) = lstat($_) unless $nlink;
  95.         
  96.         if (-d _) {
  97.  
  98.             # It really is a directory, so do it recursively.
  99.  
  100.             if (!$prune && chdir $_) {
  101.             &finddir($name,$nlink);
  102.             chdir '..';
  103.             }
  104.             --$subcount;
  105.         }
  106.         }
  107.     }
  108.     }
  109. }
  110. 1;
  111.