xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/finddepth.pl (revision 0:68f95e015346)
1*0Sstevel@tonic-gate# Usage:
2*0Sstevel@tonic-gate#	require "finddepth.pl";
3*0Sstevel@tonic-gate#
4*0Sstevel@tonic-gate#	&finddepth('/foo','/bar');
5*0Sstevel@tonic-gate#
6*0Sstevel@tonic-gate#	sub wanted { ... }
7*0Sstevel@tonic-gate#		where wanted does whatever you want.  $dir contains the
8*0Sstevel@tonic-gate#		current directory name, and $_ the current filename within
9*0Sstevel@tonic-gate#		that directory.  $name contains "$dir/$_".  You are cd'ed
10*0Sstevel@tonic-gate#		to $dir when the function is called.  The function may
11*0Sstevel@tonic-gate#		set $prune to prune the tree.
12*0Sstevel@tonic-gate#
13*0Sstevel@tonic-gate# This library is primarily for find2perl, which, when fed
14*0Sstevel@tonic-gate#
15*0Sstevel@tonic-gate#   find2perl / -name .nfs\* -mtime +7 -exec rm -f {} \; -o -fstype nfs -prune
16*0Sstevel@tonic-gate#
17*0Sstevel@tonic-gate# spits out something like this
18*0Sstevel@tonic-gate#
19*0Sstevel@tonic-gate#	sub wanted {
20*0Sstevel@tonic-gate#	    /^\.nfs.*$/ &&
21*0Sstevel@tonic-gate#	    (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
22*0Sstevel@tonic-gate#	    int(-M _) > 7 &&
23*0Sstevel@tonic-gate#	    unlink($_)
24*0Sstevel@tonic-gate#	    ||
25*0Sstevel@tonic-gate#	    ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
26*0Sstevel@tonic-gate#	    $dev < 0 &&
27*0Sstevel@tonic-gate#	    ($prune = 1);
28*0Sstevel@tonic-gate#	}
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gateuse File::Find ();
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate*name		= *File::Find::name;
34*0Sstevel@tonic-gate*prune		= *File::Find::prune;
35*0Sstevel@tonic-gate*dir		= *File::Find::dir;
36*0Sstevel@tonic-gate*topdir		= *File::Find::topdir;
37*0Sstevel@tonic-gate*topdev		= *File::Find::topdev;
38*0Sstevel@tonic-gate*topino		= *File::Find::topino;
39*0Sstevel@tonic-gate*topmode	= *File::Find::topmode;
40*0Sstevel@tonic-gate*topnlink	= *File::Find::topnlink;
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gatesub finddepth {
43*0Sstevel@tonic-gate    &File::Find::finddepth(\&wanted, @_);
44*0Sstevel@tonic-gate}
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate1;
47