4 # &find('/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($_))) &&
30 # Set the variable $dont_use_nlink if you're using AFS, since AFS cheats.
34 foreach $topdir (@_) {
35 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
36 || (warn("Can't stat $topdir: $!\n"), next);
39 ($dir,$_) = ($topdir,'.');
42 ($fixtopdir = $topdir) =~ s,/$,, ;
43 &finddir($fixtopdir,$topnlink);
46 warn "Can't cd to $topdir: $!\n";
50 unless (($dir,$_) = $topdir =~ m#^(.*/)(.*)$#) {
51 ($dir,$_) = ('.', $topdir);
54 chdir $dir && &wanted;
61 local($dir,$nlink) = @_;
62 local($dev,$ino,$mode,$subcount);
65 # Get the list of files in the current directory.
67 opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
68 local(@filenames) = readdir(DIR);
71 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
80 else { # This dir has subdirectories.
81 $subcount = $nlink - 2;
88 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
90 # Get link count and check for directoriness.
92 ($dev,$ino,$mode,$nlink) = lstat($_) unless $nlink;
96 # It really is a directory, so do it recursively.
98 if (!$prune && chdir $_) {
99 &finddir($name,$nlink);