Major changes to the DOS/djgpp port (including threading):
[p5sagit/p5-mst-13.2.git] / lib / File / Find.pm
CommitLineData
a0d0e21e 1package File::Find;
2require 5.000;
3require Exporter;
10eba763 4use Config;
6280b799 5require Cwd;
6require File::Basename;
7
a0d0e21e 8
f06db76b 9=head1 NAME
10
11find - traverse a file tree
12
13finddepth - traverse a directory structure depth-first
14
15=head1 SYNOPSIS
16
17 use File::Find;
18 find(\&wanted, '/foo','/bar');
19 sub wanted { ... }
20
21 use File::Find;
22 finddepth(\&wanted, '/foo','/bar');
23 sub wanted { ... }
24
25=head1 DESCRIPTION
26
6280b799 27The wanted() function does whatever verifications you want.
28$File::Find::dir contains the current directory name, and $_ the
29current filename within that directory. $File::Find::name contains
30C<"$File::Find::dir/$_">. You are chdir()'d to $File::Find::dir when
31the function is called. The function may set $File::Find::prune to
32prune the tree.
f06db76b 33
47a735e8 34File::Find assumes that you don't alter the $_ variable. If you do then
35make sure you return it to its original value before exiting your function.
36
f06db76b 37This library is primarily for the C<find2perl> tool, which when fed,
38
39 find2perl / -name .nfs\* -mtime +7 \
40 -exec rm -f {} \; -o -fstype nfs -prune
41
42produces something like:
43
44 sub wanted {
45 /^\.nfs.*$/ &&
46 (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
47 int(-M _) > 7 &&
48 unlink($_)
49 ||
50 ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
51 $dev < 0 &&
6280b799 52 ($File::Find::prune = 1);
f06db76b 53 }
54
6280b799 55Set the variable $File::Find::dont_use_nlink if you're using AFS,
56since AFS cheats.
f06db76b 57
58C<finddepth> is just like C<find>, except that it does a depth-first
59search.
60
61Here's another interesting wanted function. It will find all symlinks
62that don't resolve:
63
64 sub wanted {
6280b799 65 -l && !-e && print "bogus link: $File::Find::name\n";
f06db76b 66 }
67
0530a6c4 68=head1 BUGS
69
70There is no way to make find or finddepth follow symlinks.
71
f06db76b 72=cut
73
a0d0e21e 74@ISA = qw(Exporter);
6280b799 75@EXPORT = qw(find finddepth);
76
a0d0e21e 77
78sub find {
79 my $wanted = shift;
5a13742d 80 my $cwd = Cwd::cwd();
28312d68 81 # Localize these rather than lexicalizing them for backwards
82 # compatibility.
83 local($topdir,$topdev,$topino,$topmode,$topnlink);
a0d0e21e 84 foreach $topdir (@_) {
d0e28714 85 (($topdev,$topino,$topmode,$topnlink) =
86 ($Is_VMS ? stat($topdir) : lstat($topdir)))
a0d0e21e 87 || (warn("Can't stat $topdir: $!\n"), next);
88 if (-d _) {
89 if (chdir($topdir)) {
90 ($dir,$_) = ($topdir,'.');
91 $name = $topdir;
d0e28714 92 $prune = 0;
a0d0e21e 93 &$wanted;
d0e28714 94 if (!$prune) {
95 my $fixtopdir = $topdir;
96 $fixtopdir =~ s,/$,, ;
97 $fixtopdir =~ s/\.dir$// if $Is_VMS;
98 $fixtopdir =~ s/\\dir$// if $Is_NT;
99 &finddir($wanted,$fixtopdir,$topnlink);
100 }
a0d0e21e 101 }
102 else {
103 warn "Can't cd to $topdir: $!\n";
104 }
105 }
106 else {
9f637d3d 107 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
a0d0e21e 108 ($dir,$_) = ('.', $topdir);
109 }
110 $name = $topdir;
111 chdir $dir && &$wanted;
112 }
113 chdir $cwd;
114 }
115}
116
117sub finddir {
6280b799 118 my($wanted, $nlink);
119 local($dir, $name);
120 ($wanted, $dir, $nlink) = @_;
a0d0e21e 121
6280b799 122 my($dev, $ino, $mode, $subcount);
a0d0e21e 123
6280b799 124 # Get the list of files in the current directory.
a0d0e21e 125 opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
6280b799 126 my(@filenames) = readdir(DIR);
a0d0e21e 127 closedir(DIR);
128
129 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
130 for (@filenames) {
131 next if $_ eq '.';
132 next if $_ eq '..';
133 $name = "$dir/$_";
134 $nlink = 0;
135 &$wanted;
136 }
137 }
138 else { # This dir has subdirectories.
139 $subcount = $nlink - 2;
140 for (@filenames) {
141 next if $_ eq '.';
142 next if $_ eq '..';
143 $nlink = $prune = 0;
144 $name = "$dir/$_";
145 &$wanted;
146 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
147
148 # Get link count and check for directoriness.
149
10eba763 150 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
151 # unless ($nlink || $dont_use_nlink);
a0d0e21e 152
153 if (-d _) {
154
155 # It really is a directory, so do it recursively.
156
157 if (!$prune && chdir $_) {
748a9306 158 $name =~ s/\.dir$// if $Is_VMS;
9f637d3d 159 $name =~ s/\\dir$// if $Is_NT;
a0d0e21e 160 &finddir($wanted,$name,$nlink);
161 chdir '..';
162 }
163 --$subcount;
164 }
165 }
166 }
167 }
168}
169
a0d0e21e 170
171sub finddepth {
172 my $wanted = shift;
6280b799 173
174 $cwd = Cwd::fastcwd();;
175
28312d68 176 # Localize these rather than lexicalizing them for backwards
177 # compatibility.
178 local($topdir, $topdev, $topino, $topmode, $topnlink);
a0d0e21e 179 foreach $topdir (@_) {
d0e28714 180 (($topdev,$topino,$topmode,$topnlink) =
181 ($Is_VMS ? stat($topdir) : lstat($topdir)))
a0d0e21e 182 || (warn("Can't stat $topdir: $!\n"), next);
183 if (-d _) {
184 if (chdir($topdir)) {
6280b799 185 my $fixtopdir = $topdir;
186 $fixtopdir =~ s,/$,, ;
748a9306 187 $fixtopdir =~ s/\.dir$// if $Is_VMS;
9f637d3d 188 $fixtopdir =~ s/\\dir$// if $Is_NT;
a0d0e21e 189 &finddepthdir($wanted,$fixtopdir,$topnlink);
190 ($dir,$_) = ($fixtopdir,'.');
191 $name = $fixtopdir;
192 &$wanted;
193 }
194 else {
195 warn "Can't cd to $topdir: $!\n";
196 }
197 }
198 else {
9f637d3d 199 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
a0d0e21e 200 ($dir,$_) = ('.', $topdir);
201 }
d0e28714 202 $name = $topdir;
a0d0e21e 203 chdir $dir && &$wanted;
204 }
205 chdir $cwd;
206 }
207}
208
209sub finddepthdir {
6280b799 210 my($wanted, $nlink);
211 local($dir, $name);
212 ($wanted,$dir,$nlink) = @_;
213 my($dev, $ino, $mode, $subcount);
a0d0e21e 214
215 # Get the list of files in the current directory.
a0d0e21e 216 opendir(DIR,'.') || warn "Can't open $dir: $!\n";
217 my(@filenames) = readdir(DIR);
218 closedir(DIR);
219
748a9306 220 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
a0d0e21e 221 for (@filenames) {
222 next if $_ eq '.';
223 next if $_ eq '..';
224 $name = "$dir/$_";
225 $nlink = 0;
226 &$wanted;
227 }
228 }
229 else { # This dir has subdirectories.
230 $subcount = $nlink - 2;
231 for (@filenames) {
232 next if $_ eq '.';
233 next if $_ eq '..';
6280b799 234 $nlink = 0;
a0d0e21e 235 $name = "$dir/$_";
748a9306 236 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
a0d0e21e 237
238 # Get link count and check for directoriness.
239
748a9306 240 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
a0d0e21e 241
242 if (-d _) {
243
244 # It really is a directory, so do it recursively.
245
6280b799 246 if (chdir $_) {
748a9306 247 $name =~ s/\.dir$// if $Is_VMS;
9f637d3d 248 $name =~ s/\\dir$// if $Is_NT;
a0d0e21e 249 &finddepthdir($wanted,$name,$nlink);
250 chdir '..';
251 }
252 --$subcount;
253 }
254 }
255 &$wanted;
256 }
257 }
258}
259
6280b799 260# Set dont_use_nlink in your hint file if your system's stat doesn't
261# report the number of links in a directory as an indication
262# of the number of files.
263# See, e.g. hints/machten.sh for MachTen 2.2.
264$dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});
265
266# These are hard-coded for now, but may move to hint files.
10eba763 267if ($^O eq 'VMS') {
748a9306 268 $Is_VMS = 1;
269 $dont_use_nlink = 1;
270}
9f637d3d 271if ($^O =~ m:^mswin32:i) {
272 $Is_NT = 1;
273 $dont_use_nlink = 1;
274}
748a9306 275
55497cff 276$dont_use_nlink = 1
39e571d4 277 if $^O eq 'os2' || $^O eq 'dos' || $^O eq 'amigaos';
6280b799 278
a0d0e21e 2791;
280