2 # require "finddepth.pl";
4 # &finddepth('/foo','/bar');
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.
13 # This library is primarily for find2perl, which, when fed
15 # find2perl / -name .nfs\* -mtime +7 -exec rm -f {} \; -o -fstype nfs -prune
17 # spits out something like this
21 # (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
25 # ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
32 foreach $topdir (@_) {
33 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
34 || (warn("Can't stat $topdir: $!\n"), next);
37 ($fixtopdir = $topdir) =~ s,/$,, ;
38 &finddepthdir($fixtopdir,$topnlink);
39 ($dir,$_) = ($fixtopdir,'.');
44 warn "Can't cd to $topdir: $!\n";
48 unless (($dir,$_) = $topdir =~ m#^(.*/)(.*)$#) {
49 ($dir,$_) = ('.', $topdir);
51 chdir $dir && &wanted;
58 local($dir,$nlink) = @_;
59 local($dev,$ino,$mode,$subcount);
62 # Get the list of files in the current directory.
64 opendir(DIR,'.') || warn "Can't open $dir: $!\n";
65 local(@filenames) = readdir(DIR);
68 if ($nlink == 2) { # This dir has no subdirectories.
77 else { # This dir has subdirectories.
78 $subcount = $nlink - 2;
84 if ($subcount > 0) { # Seen all the subdirs?
86 # Get link count and check for directoriness.
88 ($dev,$ino,$mode,$nlink) = lstat($_) unless $nlink;
92 # It really is a directory, so do it recursively.
94 if (!$prune && chdir $_) {
95 &finddepthdir($name,$nlink);