Sys::Hostname fails under Solaris 2.5 when setuid
[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
68=cut
69
a0d0e21e 70@ISA = qw(Exporter);
6280b799 71@EXPORT = qw(find finddepth);
72
a0d0e21e 73
74sub find {
75 my $wanted = shift;
5a13742d 76 my $cwd = Cwd::cwd();
28312d68 77 # Localize these rather than lexicalizing them for backwards
78 # compatibility.
79 local($topdir,$topdev,$topino,$topmode,$topnlink);
a0d0e21e 80 foreach $topdir (@_) {
d0e28714 81 (($topdev,$topino,$topmode,$topnlink) =
82 ($Is_VMS ? stat($topdir) : lstat($topdir)))
a0d0e21e 83 || (warn("Can't stat $topdir: $!\n"), next);
84 if (-d _) {
85 if (chdir($topdir)) {
86 ($dir,$_) = ($topdir,'.');
87 $name = $topdir;
d0e28714 88 $prune = 0;
a0d0e21e 89 &$wanted;
d0e28714 90 if (!$prune) {
91 my $fixtopdir = $topdir;
92 $fixtopdir =~ s,/$,, ;
93 $fixtopdir =~ s/\.dir$// if $Is_VMS;
94 $fixtopdir =~ s/\\dir$// if $Is_NT;
95 &finddir($wanted,$fixtopdir,$topnlink);
96 }
a0d0e21e 97 }
98 else {
99 warn "Can't cd to $topdir: $!\n";
100 }
101 }
102 else {
9f637d3d 103 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
a0d0e21e 104 ($dir,$_) = ('.', $topdir);
105 }
106 $name = $topdir;
107 chdir $dir && &$wanted;
108 }
109 chdir $cwd;
110 }
111}
112
113sub finddir {
6280b799 114 my($wanted, $nlink);
115 local($dir, $name);
116 ($wanted, $dir, $nlink) = @_;
a0d0e21e 117
6280b799 118 my($dev, $ino, $mode, $subcount);
a0d0e21e 119
6280b799 120 # Get the list of files in the current directory.
a0d0e21e 121 opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
6280b799 122 my(@filenames) = readdir(DIR);
a0d0e21e 123 closedir(DIR);
124
125 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
126 for (@filenames) {
127 next if $_ eq '.';
128 next if $_ eq '..';
129 $name = "$dir/$_";
130 $nlink = 0;
131 &$wanted;
132 }
133 }
134 else { # This dir has subdirectories.
135 $subcount = $nlink - 2;
136 for (@filenames) {
137 next if $_ eq '.';
138 next if $_ eq '..';
139 $nlink = $prune = 0;
140 $name = "$dir/$_";
141 &$wanted;
142 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
143
144 # Get link count and check for directoriness.
145
10eba763 146 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
147 # unless ($nlink || $dont_use_nlink);
a0d0e21e 148
149 if (-d _) {
150
151 # It really is a directory, so do it recursively.
152
153 if (!$prune && chdir $_) {
748a9306 154 $name =~ s/\.dir$// if $Is_VMS;
9f637d3d 155 $name =~ s/\\dir$// if $Is_NT;
a0d0e21e 156 &finddir($wanted,$name,$nlink);
157 chdir '..';
158 }
159 --$subcount;
160 }
161 }
162 }
163 }
164}
165
a0d0e21e 166
167sub finddepth {
168 my $wanted = shift;
6280b799 169
170 $cwd = Cwd::fastcwd();;
171
28312d68 172 # Localize these rather than lexicalizing them for backwards
173 # compatibility.
174 local($topdir, $topdev, $topino, $topmode, $topnlink);
a0d0e21e 175 foreach $topdir (@_) {
d0e28714 176 (($topdev,$topino,$topmode,$topnlink) =
177 ($Is_VMS ? stat($topdir) : lstat($topdir)))
a0d0e21e 178 || (warn("Can't stat $topdir: $!\n"), next);
179 if (-d _) {
180 if (chdir($topdir)) {
6280b799 181 my $fixtopdir = $topdir;
182 $fixtopdir =~ s,/$,, ;
748a9306 183 $fixtopdir =~ s/\.dir$// if $Is_VMS;
9f637d3d 184 $fixtopdir =~ s/\\dir$// if $Is_NT;
a0d0e21e 185 &finddepthdir($wanted,$fixtopdir,$topnlink);
186 ($dir,$_) = ($fixtopdir,'.');
187 $name = $fixtopdir;
188 &$wanted;
189 }
190 else {
191 warn "Can't cd to $topdir: $!\n";
192 }
193 }
194 else {
9f637d3d 195 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
a0d0e21e 196 ($dir,$_) = ('.', $topdir);
197 }
d0e28714 198 $name = $topdir;
a0d0e21e 199 chdir $dir && &$wanted;
200 }
201 chdir $cwd;
202 }
203}
204
205sub finddepthdir {
6280b799 206 my($wanted, $nlink);
207 local($dir, $name);
208 ($wanted,$dir,$nlink) = @_;
209 my($dev, $ino, $mode, $subcount);
a0d0e21e 210
211 # Get the list of files in the current directory.
a0d0e21e 212 opendir(DIR,'.') || warn "Can't open $dir: $!\n";
213 my(@filenames) = readdir(DIR);
214 closedir(DIR);
215
748a9306 216 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
a0d0e21e 217 for (@filenames) {
218 next if $_ eq '.';
219 next if $_ eq '..';
220 $name = "$dir/$_";
221 $nlink = 0;
222 &$wanted;
223 }
224 }
225 else { # This dir has subdirectories.
226 $subcount = $nlink - 2;
227 for (@filenames) {
228 next if $_ eq '.';
229 next if $_ eq '..';
6280b799 230 $nlink = 0;
a0d0e21e 231 $name = "$dir/$_";
748a9306 232 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
a0d0e21e 233
234 # Get link count and check for directoriness.
235
748a9306 236 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
a0d0e21e 237
238 if (-d _) {
239
240 # It really is a directory, so do it recursively.
241
6280b799 242 if (chdir $_) {
748a9306 243 $name =~ s/\.dir$// if $Is_VMS;
9f637d3d 244 $name =~ s/\\dir$// if $Is_NT;
a0d0e21e 245 &finddepthdir($wanted,$name,$nlink);
246 chdir '..';
247 }
248 --$subcount;
249 }
250 }
251 &$wanted;
252 }
253 }
254}
255
6280b799 256# Set dont_use_nlink in your hint file if your system's stat doesn't
257# report the number of links in a directory as an indication
258# of the number of files.
259# See, e.g. hints/machten.sh for MachTen 2.2.
260$dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});
261
262# These are hard-coded for now, but may move to hint files.
10eba763 263if ($^O eq 'VMS') {
748a9306 264 $Is_VMS = 1;
265 $dont_use_nlink = 1;
266}
9f637d3d 267if ($^O =~ m:^mswin32:i) {
268 $Is_NT = 1;
269 $dont_use_nlink = 1;
270}
748a9306 271
55497cff 272$dont_use_nlink = 1
273 if $^O eq 'os2' || $^O eq 'msdos' || $^O eq 'amigaos';
6280b799 274
a0d0e21e 2751;
276