<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package File::Find;

use 5.006;
use strict;
use warnings;
use warnings::register;

use Exporter 'import';
require Cwd;
require File::Basename;
require File::Spec;

our $VERSION = '1.44';
our @EXPORT = qw(find finddepth);

my $Is_VMS = $^O eq 'VMS';
my $Is_Win32 = $^O eq 'MSWin32';


# Should ideally be my() not our() but local() currently
# refuses to operate on lexicals

our %SLnkSeen;
our ($wanted_callback, $avoid_nlink, $bydepth, $no_chdir, $follow,
    $follow_skip, $full_check, $untaint, $untaint_skip, $untaint_pat,
    $pre_process, $post_process, $dangling_symlinks);

sub contract_name {
    my ($cdir,$fn) = @_;

    return substr($cdir,0,rindex($cdir,'/')) if $fn eq $File::Find::current_dir;

    $cdir = substr($cdir,0,rindex($cdir,'/')+1);

    $fn =~ s|^\./||;

    my $abs_name= $cdir . $fn;

    if (substr($fn,0,3) eq '../') {
       1 while $abs_name =~ s!/[^/]*/\.\./+!/!;
    }

    return $abs_name;
}

sub _is_absolute {
    return $_[0] =~ m|^(?:[A-Za-z]:)?/| if $Is_Win32;
    return substr($_[0], 0, 1) eq '/';
}

sub _is_root {
    return $_[0] =~ m|^(?:[A-Za-z]:)?/\z| if $Is_Win32;
    return $_[0] eq '/';
}

sub PathCombine($$) {
    my ($Base,$Name) = @_;
    my $AbsName;

    if (_is_absolute($Name)) {
        $AbsName= $Name;
    }
    else {
        $AbsName= contract_name($Base,$Name);
    }

    # (simple) check for recursion
    my $newlen= length($AbsName);
    if ($newlen &lt;= length($Base)) {
        if (($newlen == length($Base) || substr($Base,$newlen,1) eq '/')
            &amp;&amp; $AbsName eq substr($Base,0,$newlen))
        {
            return undef;
        }
    }
    return $AbsName;
}

sub Follow_SymLink($) {
    my ($AbsName) = @_;

    my ($NewName,$DEV, $INO);
    ($DEV, $INO)= lstat $AbsName;

    while (-l _) {
        if ($SLnkSeen{$DEV, $INO}++) {
            if ($follow_skip &lt; 2) {
                die "$AbsName is encountered a second time";
            }
            else {
                return undef;
            }
        }
        my $Link = readlink($AbsName);
        # canonicalize directory separators
        $Link =~ s|\\|/|g if $Is_Win32;
        $NewName= PathCombine($AbsName, $Link);
        unless(defined $NewName) {
            if ($follow_skip &lt; 2) {
                die "$AbsName is a recursive symbolic link";
            }
            else {
                return undef;
            }
        }
        else {
            $AbsName= $NewName;
        }
        ($DEV, $INO) = lstat($AbsName);
        return undef unless defined $DEV;  #  dangling symbolic link
    }

    if ($full_check &amp;&amp; defined $DEV &amp;&amp; $SLnkSeen{$DEV, $INO}++) {
        if ( ($follow_skip &lt; 1) || ((-d _) &amp;&amp; ($follow_skip &lt; 2)) ) {
            die "$AbsName encountered a second time";
        }
        else {
            return undef;
        }
    }

    return $AbsName;
}

our($dir, $name, $fullname, $prune);
sub _find_dir_symlnk($$$);
sub _find_dir($$$);

# check whether or not a scalar variable is tainted
# (code straight from the Camel, 3rd ed., page 561)
sub is_tainted_pp {
    my $arg = shift;
    my $nada = substr($arg, 0, 0); # zero-length
    local $@;
    eval { eval "# $nada" };
    return length($@) != 0;
}


sub _find_opt {
    my $wanted = shift;
    return unless @_;
    die "invalid top directory" unless defined $_[0];

    # This function must local()ize everything because callbacks may
    # call find() or finddepth()

    local %SLnkSeen;
    local ($wanted_callback, $avoid_nlink, $bydepth, $no_chdir, $follow,
        $follow_skip, $full_check, $untaint, $untaint_skip, $untaint_pat,
        $pre_process, $post_process, $dangling_symlinks);
    local($dir, $name, $fullname, $prune);
    local *_ = \my $a;

    my $cwd            = $wanted-&gt;{bydepth} ? Cwd::fastcwd() : Cwd::getcwd();
    if ($Is_VMS) {
        # VMS returns this by default in VMS format which just doesn't
        # work for the rest of this module.
        $cwd = VMS::Filespec::unixpath($cwd);

        # Apparently this is not expected to have a trailing space.
        # To attempt to make VMS/UNIX conversions mostly reversible,
        # a trailing slash is needed.  The run-time functions ignore the
        # resulting double slash, but it causes the perl tests to fail.
        $cwd =~ s#/\z##;

        # This comes up in upper case now, but should be lower.
        # In the future this could be exact case, no need to change.
    }
    my $cwd_untainted  = $cwd;
    my $check_t_cwd    = 1;
    $wanted_callback   = $wanted-&gt;{wanted};
    $bydepth           = $wanted-&gt;{bydepth};
    $pre_process       = $wanted-&gt;{preprocess};
    $post_process      = $wanted-&gt;{postprocess};
    $no_chdir          = $wanted-&gt;{no_chdir};
    $full_check        = $wanted-&gt;{follow};
    $follow            = $full_check || $wanted-&gt;{follow_fast};
    $follow_skip       = $wanted-&gt;{follow_skip};
    $untaint           = $wanted-&gt;{untaint};
    $untaint_pat       = $wanted-&gt;{untaint_pattern};
    $untaint_skip      = $wanted-&gt;{untaint_skip};
    $dangling_symlinks = $wanted-&gt;{dangling_symlinks};

    # for compatibility reasons (find.pl, find2perl)
    local our ($topdir, $topdev, $topino, $topmode, $topnlink);

    # a symbolic link to a directory doesn't increase the link count
    $avoid_nlink      = $follow || $File::Find::dont_use_nlink;

    my ($abs_dir, $Is_Dir);

    Proc_Top_Item:
    foreach my $TOP (@_) {
        my $top_item = $TOP;
        $top_item = VMS::Filespec::unixify($top_item) if $Is_VMS;

        ($topdev,$topino,$topmode,$topnlink) = $follow ? stat $top_item : lstat $top_item;

        # canonicalize directory separators
        $top_item =~ s|[/\\]|/|g if $Is_Win32;

        # no trailing / unless path is root
        $top_item =~ s|/\z|| unless _is_root($top_item);

        $Is_Dir= 0;

        if ($follow) {

            if (_is_absolute($top_item)) {
                $abs_dir = $top_item;
            }
            elsif ($top_item eq $File::Find::current_dir) {
                $abs_dir = $cwd;
            }
            else {  # care about any  ../
                $top_item =~ s/\.dir\z//i if $Is_VMS;
                $abs_dir = contract_name("$cwd/",$top_item);
            }
            $abs_dir= Follow_SymLink($abs_dir);
            unless (defined $abs_dir) {
                if ($dangling_symlinks) {
                    if (ref $dangling_symlinks eq 'CODE') {
                        $dangling_symlinks-&gt;($top_item, $cwd);
                    } else {
                        warnings::warnif "$top_item is a dangling symbolic link\n";
                    }
                }
                next Proc_Top_Item;
            }

            if (-d _) {
                $top_item =~ s/\.dir\z//i if $Is_VMS;
                _find_dir_symlnk($wanted, $abs_dir, $top_item);
                $Is_Dir= 1;
            }
        }
        else { # no follow
            $topdir = $top_item;
            unless (defined $topnlink) {
                warnings::warnif "Can't stat $top_item: $!\n";
                next Proc_Top_Item;
            }
            if (-d _) {
                $top_item =~ s/\.dir\z//i if $Is_VMS;
                _find_dir($wanted, $top_item, $topnlink);
                $Is_Dir= 1;
            }
            else {
                $abs_dir= $top_item;
            }
        }

        unless ($Is_Dir) {
            unless (($_,$dir) = File::Basename::fileparse($abs_dir)) {
                ($dir,$_) = ('./', $top_item);
            }

            $abs_dir = $dir;
            if (( $untaint ) &amp;&amp; (is_tainted($dir) )) {
                ( $abs_dir ) = $dir =~ m|$untaint_pat|;
                unless (defined $abs_dir) {
                    if ($untaint_skip == 0) {
                        die "directory $dir is still tainted";
                    }
                    else {
                        next Proc_Top_Item;
                    }
                }
            }

            unless ($no_chdir || chdir $abs_dir) {
                warnings::warnif "Couldn't chdir $abs_dir: $!\n";
                next Proc_Top_Item;
            }

            $name = $abs_dir . $_; # $File::Find::name
            $_ = $name if $no_chdir;

            { $wanted_callback-&gt;() }; # protect against wild "next"

        }

        unless ( $no_chdir ) {
            if ( ($check_t_cwd) &amp;&amp; (($untaint) &amp;&amp; (is_tainted($cwd) )) ) {
                ( $cwd_untainted ) = $cwd =~ m|$untaint_pat|;
                unless (defined $cwd_untainted) {
                    die "insecure cwd in find(depth)";
                }
                $check_t_cwd = 0;
            }
            unless (chdir $cwd_untainted) {
                die "Can't cd to $cwd: $!\n";
            }
        }
    }
}

# API:
#  $wanted
#  $p_dir :  "parent directory"
#  $nlink :  what came back from the stat
# preconditions:
#  chdir (if not no_chdir) to dir

sub _find_dir($$$) {
    my ($wanted, $p_dir, $nlink) = @_;
    my ($CdLvl,$Level) = (0,0);
    my @Stack;
    my @filenames;
    my ($subcount,$sub_nlink);
    my $SE= [];
    my $dir_name= $p_dir;
    my $dir_pref;
    my $dir_rel = $File::Find::current_dir;
    my $tainted = 0;
    my $no_nlink;

    if ($Is_VMS) {
        # VMS is returning trailing .dir on directories
        # and trailing . on files and symbolic links
        # in UNIX syntax.
        #

        $p_dir =~ s/\.(dir)?$//i unless $p_dir eq '.';

        $dir_pref = ($p_dir =~ m/[\]&gt;]+$/ ? $p_dir : "$p_dir/" );
    }
    else {
        $dir_pref = _is_root($p_dir) ? $p_dir : "$p_dir/";
    }

    local ($dir, $name, $prune);

    unless ( $no_chdir || ($p_dir eq $File::Find::current_dir)) {
        my $udir = $p_dir;
        if (( $untaint ) &amp;&amp; (is_tainted($p_dir) )) {
            ( $udir ) = $p_dir =~ m|$untaint_pat|;
            unless (defined $udir) {
                if ($untaint_skip == 0) {
                    die "directory $p_dir is still tainted";
                }
                else {
                    return;
                }
            }
        }
        unless (chdir ($Is_VMS &amp;&amp; $udir !~ /[\/\[&lt;]+/ ? "./$udir" : $udir)) {
            warnings::warnif "Can't cd to $udir: $!\n";
            return;
        }
    }

    # push the starting directory
    push @Stack,[$CdLvl,$p_dir,$dir_rel,-1]  if  $bydepth;

    while (defined $SE) {
        unless ($bydepth) {
            $dir= $p_dir; # $File::Find::dir
            $name= $dir_name; # $File::Find::name
            $_= ($no_chdir ? $dir_name : $dir_rel ); # $_
            # prune may happen here
            $prune= 0;
            { $wanted_callback-&gt;() };   # protect against wild "next"
            next if $prune;
        }

        # change to that directory
        unless ($no_chdir || ($dir_rel eq $File::Find::current_dir)) {
            my $udir= $dir_rel;
            if ( ($untaint) &amp;&amp; (($tainted) || ($tainted = is_tainted($dir_rel) )) ) {
                ( $udir ) = $dir_rel =~ m|$untaint_pat|;
                unless (defined $udir) {
                    if ($untaint_skip == 0) {
                        die "directory (" . ($p_dir ne '/' ? $p_dir : '') . "/) $dir_rel is still tainted";
                    } else { # $untaint_skip == 1
                        next;
                    }
                }
            }
            unless (chdir ($Is_VMS &amp;&amp; $udir !~ /[\/\[&lt;]+/ ? "./$udir" : $udir)) {
                warnings::warnif "Can't cd to (" .
                    ($p_dir ne '/' ? $p_dir : '') . "/) $udir: $!\n";
                next;
            }
            $CdLvl++;
        }

        $dir= $dir_name; # $File::Find::dir

        # Get the list of files in the current directory.
        my $dh;
        unless (opendir $dh, ($no_chdir ? $dir_name : $File::Find::current_dir)) {
            warnings::warnif "Can't opendir($dir_name): $!\n";
            next;
        }
        @filenames = readdir $dh;
        closedir($dh);
        @filenames = $pre_process-&gt;(@filenames) if $pre_process;
        push @Stack,[$CdLvl,$dir_name,"",-2]   if $post_process;

        # default: use whatever was specified
        # (if $nlink &gt;= 2, and $avoid_nlink == 0, this will switch back)
        $no_nlink = $avoid_nlink;
        # if dir has wrong nlink count, force switch to slower stat method
        $no_nlink = 1 if ($nlink &lt; 2);

        if ($nlink == 2 &amp;&amp; !$no_nlink) {
            # This dir has no subdirectories.
            for my $FN (@filenames) {
                if ($Is_VMS) {
                    # Big hammer here - Compensate for VMS trailing . and .dir
                    # No win situation until this is changed, but this
                    # will handle the majority of the cases with breaking the fewest

                    $FN =~ s/\.dir\z//i;
                    $FN =~ s#\.$## if ($FN ne '.');
                }
                next if $FN =~ $File::Find::skip_pattern;

                $name = $dir_pref . $FN; # $File::Find::name
                $_ = ($no_chdir ? $name : $FN); # $_
                { $wanted_callback-&gt;() }; # protect against wild "next"
            }

        }
        else {
            # This dir has subdirectories.
            $subcount = $nlink - 2;

            # HACK: insert directories at this position, so as to preserve
            # the user pre-processed ordering of files (thus ensuring
            # directory traversal is in user sorted order, not at random).
            my $stack_top = @Stack;

            for my $FN (@filenames) {
                next if $FN =~ $File::Find::skip_pattern;
                if ($subcount &gt; 0 || $no_nlink) {
                    # Seen all the subdirs?
                    # check for directoriness.
                    # stat is faster for a file in the current directory
                    $sub_nlink = (lstat ($no_chdir ? $dir_pref . $FN : $FN))[3];

                    if (-d _) {
                        --$subcount;
                        $FN =~ s/\.dir\z//i if $Is_VMS;
                        # HACK: replace push to preserve dir traversal order
                        #push @Stack,[$CdLvl,$dir_name,$FN,$sub_nlink];
                        splice @Stack, $stack_top, 0,
                                 [$CdLvl,$dir_name,$FN,$sub_nlink];
                    }
                    else {
                        $name = $dir_pref . $FN; # $File::Find::name
                        $_= ($no_chdir ? $name : $FN); # $_
                        { $wanted_callback-&gt;() }; # protect against wild "next"
                    }
                }
                else {
                    $name = $dir_pref . $FN; # $File::Find::name
                    $_= ($no_chdir ? $name : $FN); # $_
                    { $wanted_callback-&gt;() }; # protect against wild "next"
                }
            }
        }
    }
    continue {
        while ( defined ($SE = pop @Stack) ) {
            ($Level, $p_dir, $dir_rel, $nlink) = @$SE;
            if ($CdLvl &gt; $Level &amp;&amp; !$no_chdir) {
                my $tmp;
                if ($Is_VMS) {
                    $tmp = '[' . ('-' x ($CdLvl-$Level)) . ']';
                }
                else {
                    $tmp = join('/',('..') x ($CdLvl-$Level));
                }
                die "Can't cd to $tmp from $dir_name: $!"
                    unless chdir ($tmp);
                $CdLvl = $Level;
            }

            if ($^O eq 'VMS') {
                if ($p_dir =~ m/[\]&gt;]+$/) {
                    $dir_name = $p_dir;
                    $dir_name =~ s/([\]&gt;]+)$/.$dir_rel$1/;
                    $dir_pref = $dir_name;
                }
                else {
                    $dir_name = "$p_dir/$dir_rel";
                    $dir_pref = "$dir_name/";
                }
            }
            else {
                $dir_name = _is_root($p_dir) ? "$p_dir$dir_rel" : "$p_dir/$dir_rel";
                $dir_pref = "$dir_name/";
            }

            if ( $nlink == -2 ) {
                $name = $dir = $p_dir; # $File::Find::name / dir
                $_ = $File::Find::current_dir;
                $post_process-&gt;();              # End-of-directory processing
            }
            elsif ( $nlink &lt; 0 ) {  # must be finddepth, report dirname now
                $name = $dir_name;
                if ( substr($name,-2) eq '/.' ) {
                    substr($name, length($name) == 2 ? -1 : -2) = '';
                }
                $dir = $p_dir;
                $_ = ($no_chdir ? $dir_name : $dir_rel );
                if ( substr($_,-2) eq '/.' ) {
                    substr($_, length($_) == 2 ? -1 : -2) = '';
                }
                { $wanted_callback-&gt;() }; # protect against wild "next"
             }
             else {
                push @Stack,[$CdLvl,$p_dir,$dir_rel,-1]  if  $bydepth;
                last;
            }
        }
    }
}


# API:
#  $wanted
#  $dir_loc : absolute location of a dir
#  $p_dir   : "parent directory"
# preconditions:
#  chdir (if not no_chdir) to dir

sub _find_dir_symlnk($$$) {
    my ($wanted, $dir_loc, $p_dir) = @_; # $dir_loc is the absolute directory
    my @Stack;
    my @filenames;
    my $new_loc;
    my $updir_loc = $dir_loc; # untainted parent directory
    my $SE = [];
    my $dir_name = $p_dir;
    my $dir_pref;
    my $loc_pref;
    my $dir_rel = $File::Find::current_dir;
    my $byd_flag; # flag for pending stack entry if $bydepth
    my $tainted = 0;
    my $ok = 1;

    $dir_pref = _is_root($p_dir) ? $p_dir : "$p_dir/";
    $loc_pref = _is_root($dir_loc) ? $dir_loc : "$dir_loc/";

    local ($dir, $name, $fullname, $prune);

    unless ($no_chdir) {
        # untaint the topdir
        if (( $untaint ) &amp;&amp; (is_tainted($dir_loc) )) {
            ( $updir_loc ) = $dir_loc =~ m|$untaint_pat|; # parent dir, now untainted
            # once untainted, $updir_loc is pushed on the stack (as parent directory);
            # hence, we don't need to untaint the parent directory every time we chdir
            # to it later
            unless (defined $updir_loc) {
                if ($untaint_skip == 0) {
                    die "directory $dir_loc is still tainted";
                }
                else {
                    return;
                }
            }
        }
        $ok = chdir($updir_loc) unless ($p_dir eq $File::Find::current_dir);
        unless ($ok) {
            warnings::warnif "Can't cd to $updir_loc: $!\n";
            return;
        }
    }

    push @Stack,[$dir_loc,$updir_loc,$p_dir,$dir_rel,-1]  if  $bydepth;

    while (defined $SE) {

        unless ($bydepth) {
            # change (back) to parent directory (always untainted)
            unless ($no_chdir) {
                unless (chdir $updir_loc) {
                    warnings::warnif "Can't cd to $updir_loc: $!\n";
                    next;
                }
            }
            $dir= $p_dir; # $File::Find::dir
            $name= $dir_name; # $File::Find::name
            $_= ($no_chdir ? $dir_name : $dir_rel ); # $_
            $fullname= $dir_loc; # $File::Find::fullname
            # prune may happen here
            $prune= 0;
            lstat($_); # make sure  file tests with '_' work
            { $wanted_callback-&gt;() }; # protect against wild "next"
            next if $prune;
        }

        # change to that directory
        unless ($no_chdir || ($dir_rel eq $File::Find::current_dir)) {
            $updir_loc = $dir_loc;
            if ( ($untaint) &amp;&amp; (($tainted) || ($tainted = is_tainted($dir_loc) )) ) {
                # untaint $dir_loc, what will be pushed on the stack as (untainted) parent dir
                ( $updir_loc ) = $dir_loc =~ m|$untaint_pat|;
                unless (defined $updir_loc) {
                    if ($untaint_skip == 0) {
                        die "directory $dir_loc is still tainted";
                    }
                    else {
                        next;
                    }
                }
            }
            unless (chdir $updir_loc) {
                warnings::warnif "Can't cd to $updir_loc: $!\n";
                next;
            }
        }

        $dir = $dir_name; # $File::Find::dir

        # Get the list of files in the current directory.
        my $dh;
        unless (opendir $dh, ($no_chdir ? $dir_loc : $File::Find::current_dir)) {
            warnings::warnif "Can't opendir($dir_loc): $!\n";
            next;
        }
        @filenames = readdir $dh;
        closedir($dh);

        for my $FN (@filenames) {
            if ($Is_VMS) {
                # Big hammer here - Compensate for VMS trailing . and .dir
                # No win situation until this is changed, but this
                # will handle the majority of the cases with breaking the fewest.

                $FN =~ s/\.dir\z//i;
                $FN =~ s#\.$## if ($FN ne '.');
            }
            next if $FN =~ $File::Find::skip_pattern;

            # follow symbolic links / do an lstat
            $new_loc = Follow_SymLink($loc_pref.$FN);

            # ignore if invalid symlink
            unless (defined $new_loc) {
                if (!defined -l _ &amp;&amp; $dangling_symlinks) {
                $fullname = undef;
                    if (ref $dangling_symlinks eq 'CODE') {
                        $dangling_symlinks-&gt;($FN, $dir_pref);
                    } else {
                        warnings::warnif "$dir_pref$FN is a dangling symbolic link\n";
                    }
                }
            else {
                $fullname = $loc_pref . $FN;
            }
                $name = $dir_pref . $FN;
                $_ = ($no_chdir ? $name : $FN);
                { $wanted_callback-&gt;() };
                next;
            }

            if (-d _) {
                if ($Is_VMS) {
                    $FN =~ s/\.dir\z//i;
                    $FN =~ s#\.$## if ($FN ne '.');
                    $new_loc =~ s/\.dir\z//i;
                    $new_loc =~ s#\.$## if ($new_loc ne '.');
                }
                push @Stack,[$new_loc,$updir_loc,$dir_name,$FN,1];
            }
            else {
                $fullname = $new_loc; # $File::Find::fullname
                $name = $dir_pref . $FN; # $File::Find::name
                $_ = ($no_chdir ? $name : $FN); # $_
                { $wanted_callback-&gt;() }; # protect against wild "next"
            }
        }

    }
    continue {
        while (defined($SE = pop @Stack)) {
            ($dir_loc, $updir_loc, $p_dir, $dir_rel, $byd_flag) = @$SE;
            $dir_name = _is_root($p_dir) ? "$p_dir$dir_rel" : "$p_dir/$dir_rel";
            $dir_pref = "$dir_name/";
            $loc_pref = "$dir_loc/";
            if ( $byd_flag &lt; 0 ) {  # must be finddepth, report dirname now
                unless ($no_chdir || ($dir_rel eq $File::Find::current_dir)) {
                    unless (chdir $updir_loc) { # $updir_loc (parent dir) is always untainted
                        warnings::warnif "Can't cd to $updir_loc: $!\n";
                        next;
                    }
                }
                $fullname = $dir_loc; # $File::Find::fullname
                $name = $dir_name; # $File::Find::name
                if ( substr($name,-2) eq '/.' ) {
                    substr($name, length($name) == 2 ? -1 : -2) = ''; # $File::Find::name
                }
                $dir = $p_dir; # $File::Find::dir
                $_ = ($no_chdir ? $dir_name : $dir_rel); # $_
                if ( substr($_,-2) eq '/.' ) {
                    substr($_, length($_) == 2 ? -1 : -2) = '';
                }

                lstat($_); # make sure file tests with '_' work
                { $wanted_callback-&gt;() }; # protect against wild "next"
            }
            else {
                push @Stack,[$dir_loc, $updir_loc, $p_dir, $dir_rel,-1]  if  $bydepth;
                last;
            }
        }
    }
}


sub wrap_wanted {
    my $wanted = shift;
    if ( ref($wanted) eq 'HASH' ) {
        # RT #122547
        my %valid_options = map {$_ =&gt; 1} qw(
            wanted
            bydepth
            preprocess
            postprocess
            follow
            follow_fast
            follow_skip
            dangling_symlinks
            no_chdir
            untaint
            untaint_pattern
            untaint_skip
        );
        my @invalid_options = ();
        for my $v (keys %{$wanted}) {
            push @invalid_options, $v unless exists $valid_options{$v};
        }
        warn "Invalid option(s): @invalid_options" if @invalid_options;

        unless( exists $wanted-&gt;{wanted} and ref( $wanted-&gt;{wanted} ) eq 'CODE' ) {
            die 'no &amp;wanted subroutine given';
        }
        if ( $wanted-&gt;{follow} || $wanted-&gt;{follow_fast}) {
            $wanted-&gt;{follow_skip} = 1 unless defined $wanted-&gt;{follow_skip};
        }
        if ( $wanted-&gt;{untaint} ) {
            $wanted-&gt;{untaint_pattern} = $File::Find::untaint_pattern
            unless defined $wanted-&gt;{untaint_pattern};
            $wanted-&gt;{untaint_skip} = 0 unless defined $wanted-&gt;{untaint_skip};
        }
        return $wanted;
    }
    elsif( ref( $wanted ) eq 'CODE' ) {
        return { wanted =&gt; $wanted };
    }
    else {
       die 'no &amp;wanted subroutine given';
    }
}

sub find {
    my $wanted = shift;
    _find_opt(wrap_wanted($wanted), @_);
}

sub finddepth {
    my $wanted = wrap_wanted(shift);
    $wanted-&gt;{bydepth} = 1;
    _find_opt($wanted, @_);
}

# default
$File::Find::skip_pattern    = qr/^\.{1,2}\z/;
$File::Find::untaint_pattern = qr|^([-+@\w./]+)$|;

# this _should_ work properly on all platforms
# where File::Find can be expected to work
$File::Find::current_dir = File::Spec-&gt;curdir || '.';

$File::Find::dont_use_nlink = 1;

# We need a function that checks if a scalar is tainted. Either use the
# Scalar::Util module's tainted() function or our (slower) pure Perl
# fallback is_tainted_pp()
{
    local $@;
    eval { require Scalar::Util };
    *is_tainted = $@ ? \&amp;is_tainted_pp : \&amp;Scalar::Util::tainted;
}

1;

__END__

=head1 NAME

File::Find - Traverse a directory tree.

=head1 SYNOPSIS

    use File::Find;
    find(\&amp;wanted, @directories_to_search);
    sub wanted { ... }

    use File::Find;
    finddepth(\&amp;wanted, @directories_to_search);
    sub wanted { ... }

    use File::Find;
    find({ wanted =&gt; \&amp;process, follow =&gt; 1 }, '.');

=head1 DESCRIPTION

These are functions for searching through directory trees doing work
on each file found similar to the Unix L&lt;find(1)&gt; command.  C&lt;File::Find&gt;
exports two functions, C&lt;find&gt; and C&lt;finddepth&gt;.  They work similarly
but have subtle differences.

=over 4

=item B&lt;find&gt;

  find(\&amp;wanted,  @directories);
  find(\%options, @directories);

C&lt;find()&gt; does a depth-first search over the given C&lt;@directories&gt; in
the order they are given.  For each file or directory found, it calls
the C&lt;&amp;wanted&gt; subroutine.  (See below for details on how to use the
C&lt;&amp;wanted&gt; function).  Additionally, for each directory found, it will
C&lt;chdir()&gt; into that directory and continue the search, invoking the
C&lt;&amp;wanted&gt; function on each file or subdirectory in the directory.

=item B&lt;finddepth&gt;

  finddepth(\&amp;wanted,  @directories);
  finddepth(\%options, @directories);

C&lt;finddepth()&gt; works just like C&lt;find()&gt; except that it invokes the
C&lt;&amp;wanted&gt; function for a directory I&lt;after&gt; invoking it for the
directory's contents.  It does a postorder traversal instead of a
preorder traversal, working from the bottom of the directory tree up
where C&lt;find()&gt; works from the top of the tree down.

=back

Despite the name of the C&lt;finddepth()&gt; function, both C&lt;find()&gt; and
C&lt;finddepth()&gt; perform a depth-first search of the directory hierarchy.

=head2 C&lt;%options&gt;

The first argument to C&lt;find()&gt; is either a code reference to your
C&lt;&amp;wanted&gt; function, or a hash reference describing the operations
to be performed for each file.  The
code reference is described in L&lt;/The wanted function&gt; below.

Here are the possible B&lt;keys&gt; for the hash:

=over 4

=item C&lt;wanted&gt;

The value should be a code reference.  This code reference is
described in L&lt;/The wanted function&gt; below. The C&lt;&amp;wanted&gt; subroutine is
mandatory.

=item C&lt;bydepth&gt;

Reports the name of a directory only AFTER all its entries
have been reported.  Entry point C&lt;finddepth()&gt; is a shortcut for
specifying C&lt;&lt; { bydepth =&gt; 1 } &gt;&gt; in the first argument of C&lt;find()&gt;.

=item C&lt;preprocess&gt;

The value should be a code reference. This code reference is used to
preprocess the current directory. The name of the currently processed
directory is in C&lt;$File::Find::dir&gt;. Your preprocessing function is
called after C&lt;readdir()&gt;, but before the loop that calls the C&lt;wanted()&gt;
function. It is called with a list of strings (actually file/directory
names) and is expected to return a list of strings. The code can be
used to sort the file/directory names alphabetically, numerically,
or to filter out directory entries based on their name alone. When
C&lt;follow&gt; or C&lt;follow_fast&gt; are in effect, C&lt;preprocess&gt; is a no-op.

=item C&lt;postprocess&gt;

The value should be a code reference. It is invoked just before leaving
the currently processed directory. It is called in void context with no
arguments. The name of the current directory is in C&lt;$File::Find::dir&gt;. This
hook is handy for summarizing a directory, such as calculating its disk
usage. When C&lt;follow&gt; or C&lt;follow_fast&gt; are in effect, C&lt;postprocess&gt; is a
no-op.

=item C&lt;follow&gt;

Causes symbolic links to be followed. Since directory trees with symbolic
links (followed) may contain files more than once and may even have
cycles, a hash has to be built up with an entry for each file.
This might be expensive both in space and time for a large
directory tree. See L&lt;/follow_fast&gt; and L&lt;/follow_skip&gt; below.
If either C&lt;follow&gt; or C&lt;follow_fast&gt; is in effect:

=over 4

=item *

It is guaranteed that an C&lt;lstat&gt; has been called before the user's
C&lt;wanted()&gt; function is called. This enables fast file checks involving C&lt;_&gt;.
Note that this guarantee no longer holds if C&lt;follow&gt; or C&lt;follow_fast&gt;
are not set.

=item *

There is a variable C&lt;$File::Find::fullname&gt; which holds the absolute
pathname of the file with all symbolic links resolved.  If the link is
a dangling symbolic link, then fullname will be set to C&lt;undef&gt;.

=back

=item C&lt;follow_fast&gt;

This is similar to C&lt;follow&gt; except that it may report some files more
than once.  It does detect cycles, however.  Since only symbolic links
have to be hashed, this is much cheaper both in space and time.  If
processing a file more than once (by the user's C&lt;wanted()&gt; function)
is worse than just taking time, the option C&lt;follow&gt; should be used.

=item C&lt;follow_skip&gt;

C&lt;follow_skip==1&gt;, which is the default, causes all files which are
neither directories nor symbolic links to be ignored if they are about
to be processed a second time. If a directory or a symbolic link
are about to be processed a second time, C&lt;File::Find&gt; dies.

C&lt;follow_skip==0&gt; causes C&lt;File::Find&gt; to die if any file is about to be
processed a second time.

C&lt;follow_skip==2&gt; causes C&lt;File::Find&gt; to ignore any duplicate files and
directories but to proceed normally otherwise.

=item C&lt;dangling_symlinks&gt;

Specifies what to do with symbolic links whose target doesn't exist.
If true and a code reference, will be called with the symbolic link
name and the directory it lives in as arguments.  Otherwise, if true
and warnings are on, a warning of the form C&lt;"symbolic_link_name is a dangling
symbolic link\n"&gt; will be issued.  If false, the dangling symbolic link
will be silently ignored.

=item C&lt;no_chdir&gt;

Does not C&lt;chdir()&gt; to each directory as it recurses. The C&lt;wanted()&gt;
function will need to be aware of this, of course. In this case,
C&lt;$_&gt; will be the same as C&lt;$File::Find::name&gt;.

=item C&lt;untaint&gt;

If find is used in L&lt;taint-mode|perlsec/Taint mode&gt; (C&lt;-T&gt; command line
switch or C&lt;if EUID != UID&gt; or C&lt;if EGID != GID&gt;), then internally
directory names have to be untainted before they can be C&lt;chdir&gt;'d to.
Therefore they are checked against a regular expression C&lt;untaint_pattern&gt;.
Note that all names passed to the user's C&lt;wanted()&gt; function are still
tainted. If this option is used while not in taint-mode, C&lt;untaint&gt;
is a no-op.

=item C&lt;untaint_pattern&gt;

See above. This should be set using the C&lt;qr&gt; quoting operator.
The default is set to C&lt;qr|^([-+@\w./]+)$|&gt;.
Note that the parentheses are vital.

=item C&lt;untaint_skip&gt;

If set, a directory which fails the C&lt;untaint_pattern&gt; is skipped,
including all its sub-directories. The default is to C&lt;die&gt; in such a case.

=back

=head2 The wanted function

The C&lt;wanted()&gt; function does whatever verifications you want on
each file and directory.  Note that despite its name, the C&lt;wanted()&gt;
function is a generic callback function, and does B&lt;not&gt; tell
C&lt;File::Find&gt; if a file is "wanted" or not.  In fact, its return value
is ignored.

The C&lt;wanted&gt; function takes no arguments but rather does its work
through a collection of variables.

=over 4

=item C&lt;$File::Find::dir&gt; is the current directory name,

=item C&lt;$_&gt; is the current filename within that directory

=item C&lt;$File::Find::name&gt; is the complete pathname to the file.

=back

The above variables have all been localized and may be changed without
affecting data outside of the wanted function.

For example, when examining the file F&lt;/some/path/foo.ext&gt; you will have:

    $File::Find::dir  = /some/path/
    $_                = foo.ext
    $File::Find::name = /some/path/foo.ext

You are C&lt;chdir()&gt;'d to C&lt;$File::Find::dir&gt; when the function is called,
unless C&lt;no_chdir&gt; was specified. Note that when changing to
directories is in effect, the root directory (F&lt;/&gt;) is a somewhat
special case inasmuch as the concatenation of C&lt;$File::Find::dir&gt;,
C&lt;'/'&gt; and C&lt;$_&gt; is not literally equal to C&lt;$File::Find::name&gt;. The
table below summarizes all variants:

              $File::Find::name  $File::Find::dir  $_
 default      /                  /                 .
 no_chdir=&gt;0  /etc               /                 etc
              /etc/x             /etc              x

 no_chdir=&gt;1  /                  /                 /
              /etc               /                 /etc
              /etc/x             /etc              /etc/x


When C&lt;follow&gt; or C&lt;follow_fast&gt; are in effect, there is
also a C&lt;$File::Find::fullname&gt;.  The function may set
C&lt;$File::Find::prune&gt; to prune the tree unless C&lt;bydepth&gt; was
specified.  Unless C&lt;follow&gt; or C&lt;follow_fast&gt; is specified, for
compatibility reasons (C&lt;find.pl&gt;, L&lt;find2perl&gt;) there are
in addition the following globals available: C&lt;$File::Find::topdir&gt;,
C&lt;$File::Find::topdev&gt;, C&lt;$File::Find::topino&gt;,
C&lt;$File::Find::topmode&gt; and C&lt;$File::Find::topnlink&gt;.

This library is useful for the C&lt;find2perl&gt; tool (distributed with the
L&lt;App::find2perl&gt; CPAN module), which when fed:

  find2perl / -name .nfs\* -mtime +7 \
    -exec rm -f {} \; -o -fstype nfs -prune

produces something like:

 sub wanted {
    /^\.nfs.*\z/s &amp;&amp;
    (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_)) &amp;&amp;
    int(-M _) &gt; 7 &amp;&amp;
    unlink($_)
    ||
    ($nlink || (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_))) &amp;&amp;
    $dev &lt; 0 &amp;&amp;
    ($File::Find::prune = 1);
 }

Notice the C&lt;_&gt; in the above C&lt;int(-M _)&gt;: the C&lt;_&gt; is a magical
filehandle that caches the information from the preceding
C&lt;stat()&gt;, C&lt;lstat()&gt;, or filetest.

Here's another interesting wanted function.  It will find all symbolic
links that don't resolve:

    sub wanted {
         -l &amp;&amp; !-e &amp;&amp; print "bogus link: $File::Find::name\n";
    }

Note that you may mix directories and (non-directory) files in the list of 
directories to be searched by the C&lt;wanted()&gt; function.

    find(\&amp;wanted, "./foo", "./bar", "./baz/epsilon");

In the example above, no file in F&lt;./baz/&gt; other than F&lt;./baz/epsilon&gt; will be
evaluated by C&lt;wanted()&gt;.

See also the script C&lt;pfind&gt; on CPAN for a nice application of this
module.

=head1 WARNINGS

If you run your program with the C&lt;-w&gt; switch, or if you use the
C&lt;warnings&gt; pragma, File::Find will report warnings for several weird
situations. You can disable these warnings by putting the statement

    no warnings 'File::Find';

in the appropriate scope. See L&lt;warnings&gt; for more info about lexical
warnings.

=head1 BUGS AND CAVEATS

=over 4

=item C&lt;$dont_use_nlink&gt;

You can set the variable C&lt;$File::Find::dont_use_nlink&gt; to C&lt;0&gt; if you
are sure the filesystem you are scanning reflects the number of
subdirectories in the parent directory's C&lt;nlink&gt; count.

If you do set C&lt;$File::Find::dont_use_nlink&gt; to 0, you may notice an
improvement in speed at the risk of not recursing into subdirectories
if a filesystem doesn't populate C&lt;nlink&gt; as expected.

C&lt;$File::Find::dont_use_nlink&gt; now defaults to 1 on all platforms.

=item Symlinks

Be aware that the option to follow symbolic links can be dangerous.
Depending on the structure of the directory tree (including symbolic
links to directories) you might traverse a given (physical) directory
more than once (only if C&lt;follow_fast&gt; is in effect).
Furthermore, deleting or changing files in a symbolically linked directory
might cause very unpleasant surprises, since you delete or change files
in an unknown directory.

=back

=head1 HISTORY

C&lt;File::Find&gt; used to produce incorrect results if called recursively.
During the development of perl 5.8 this bug was fixed.
The first fixed version of C&lt;File::Find&gt; was 1.01.

=head1 SEE ALSO

L&lt;find(1)&gt;, L&lt;find2perl&gt;

=cut
</pre></body></html>