6 @EXPORT = qw(find finddepth);
11 # find(\&wanted, '/foo','/bar');
14 # where wanted does whatever you want. $dir contains the
15 # current directory name, and $_ the current filename within
16 # that directory. $name contains "$dir/$_". You are cd'ed
17 # to $dir when the function is called. The function may
18 # set $prune to prune the tree.
20 # This library is primarily for find2perl, which, when fed
22 # find2perl / -name .nfs\* -mtime +7 -exec rm -f {} \; -o -fstype nfs -prune
24 # spits out something like this
28 # (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
32 # ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
37 # Set the variable $dont_use_nlink if you're using AFS, since AFS cheats.
42 foreach $topdir (@_) {
43 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
44 || (warn("Can't stat $topdir: $!\n"), next);
47 ($dir,$_) = ($topdir,'.');
50 ($fixtopdir = $topdir) =~ s,/$,, ;
51 &finddir($wanted,$fixtopdir,$topnlink);
54 warn "Can't cd to $topdir: $!\n";
58 unless (($dir,$_) = $topdir =~ m#^(.*/)(.*)$#) {
59 ($dir,$_) = ('.', $topdir);
62 chdir $dir && &$wanted;
69 local($wanted,$dir,$nlink) = @_;
70 local($dev,$ino,$mode,$subcount);
73 # Get the list of files in the current directory.
75 opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
76 local(@filenames) = readdir(DIR);
79 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
88 else { # This dir has subdirectories.
89 $subcount = $nlink - 2;
96 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
98 # Get link count and check for directoriness.
100 ($dev,$ino,$mode,$nlink) = lstat($_) unless $nlink;
104 # It really is a directory, so do it recursively.
106 if (!$prune && chdir $_) {
107 &finddir($wanted,$name,$nlink);
120 # finddepth(\&wanted, '/foo','/bar');
123 # where wanted does whatever you want. $dir contains the
124 # current directory name, and $_ the current filename within
125 # that directory. $name contains "$dir/$_". You are cd'ed
126 # to $dir when the function is called. The function may
127 # set $prune to prune the tree.
129 # This library is primarily for find2perl, which, when fed
131 # find2perl / -name .nfs\* -mtime +7 -exec rm -f {} \; -o -fstype nfs -prune
133 # spits out something like this
137 # (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
141 # ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
149 foreach $topdir (@_) {
150 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
151 || (warn("Can't stat $topdir: $!\n"), next);
153 if (chdir($topdir)) {
154 ($fixtopdir = $topdir) =~ s,/$,, ;
155 &finddepthdir($wanted,$fixtopdir,$topnlink);
156 ($dir,$_) = ($fixtopdir,'.');
161 warn "Can't cd to $topdir: $!\n";
165 unless (($dir,$_) = $topdir =~ m#^(.*/)(.*)$#) {
166 ($dir,$_) = ('.', $topdir);
168 chdir $dir && &$wanted;
175 my($wanted,$dir,$nlink) = @_;
176 my($dev,$ino,$mode,$subcount);
179 # Get the list of files in the current directory.
181 opendir(DIR,'.') || warn "Can't open $dir: $!\n";
182 my(@filenames) = readdir(DIR);
185 if ($nlink == 2) { # This dir has no subdirectories.
194 else { # This dir has subdirectories.
195 $subcount = $nlink - 2;
201 if ($subcount > 0) { # Seen all the subdirs?
203 # Get link count and check for directoriness.
205 ($dev,$ino,$mode,$nlink) = lstat($_) unless $nlink;
209 # It really is a directory, so do it recursively.
211 if (!$prune && chdir $_) {
212 &finddepthdir($wanted,$name,$nlink);