home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / ntcode / ntperlb / lib / finddep.pl < prev    next >
Encoding:
Perl Script  |  1995-05-19  |  2.5 KB  |  109 lines

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