root/juggler/branches/2.2/scripts/RecurseDir.pm
| Revision 5964, 3.2 kB (checked in by patrick, 7 years ago) | |
|---|---|
| |
| Line | |
|---|---|
| 1 | # |
| 2 | # Copyright (c) 1998 Patrick L. Hartling |
| 3 | # |
| 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions |
| 6 | # are met: |
| 7 | # 1. Redistributions of source code must retain the above copyright |
| 8 | # notice, this list of conditions and the following disclaimer. |
| 9 | # 2. Redistributions in binary form must reproduce the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer in the |
| 11 | # documentation and/or other materials provided with the distribution. |
| 12 | # 3. The names of its contributors may not be used to endorse or promote |
| 13 | # products derived from this software without specific prior written |
| 14 | # permission. |
| 15 | # |
| 16 | # THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS |
| 17 | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN |
| 19 | # NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 24 | # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 25 | # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | # |
| 27 | # RecurseDir.pm,v 1.7 2000/10/31 16:20:53 patrick Exp |
| 28 | # |
| 29 | |
| 30 | # ============================================================================= |
| 31 | # This is a silly little module for doing directory recursion. |
| 32 | # ============================================================================= |
| 33 | |
| 34 | package RecurseDir; |
| 35 | |
| 36 | require Exporter; |
| 37 | |
| 38 | @ISA = qw(Exporter); |
| 39 | @EXPORT = qw(recurseDir setRecurseAction); |
| 40 | |
| 41 | my $caller = caller(); |
| 42 | my $rec_func = \&{"${caller}::recurseFunc"}; |
| 43 | |
| 44 | sub setRecurseAction ($) { |
| 45 | $rec_func = shift; |
| 46 | } |
| 47 | |
| 48 | my(@dirstack) = (); |
| 49 | |
| 50 | sub recurseDir ($) { |
| 51 | my $dirname = shift; |
| 52 | |
| 53 | # This if we are just starting (i.e., @dirstack is empty) and the |
| 54 | # directory at which all recusrion is rooted is something other than the |
| 55 | # current directory, add its name to @dirstack. |
| 56 | if ( $#dirstack == -1 && "$dirname" ne "." ) { |
| 57 | push(@dirstack, "$dirname"); |
| 58 | } |
| 59 | |
| 60 | # Don't bother chdir()'ing to the current directory and printing an |
| 61 | # empty directory stack. |
| 62 | if ( $#dirstack != -1 ) { |
| 63 | chdir("$dirname"); |
| 64 | |
| 65 | print("==> ", join("/", @dirstack), "\n") if $print_cur_dir; |
| 66 | } |
| 67 | |
| 68 | opendir(DIR, "."); |
| 69 | my(@files) = sort(readdir(DIR)); |
| 70 | closedir(DIR); |
| 71 | |
| 72 | my $file; |
| 73 | foreach $file ( @files ) { |
| 74 | next if $file =~ /^\.\.?$/; |
| 75 | |
| 76 | next if -l "$file" && $skip_symlinks; |
| 77 | |
| 78 | if ( -d "$file" ) { |
| 79 | push(@dirstack, "$file"); |
| 80 | recurseDir("$file"); |
| 81 | chdir(".."); |
| 82 | print("<== ", join("/", @dirstack), "\n") if $print_cur_dir; |
| 83 | pop(@dirstack); |
| 84 | } else { |
| 85 | # Pass &$rec_func only the current file name. |
| 86 | if ( $pass_rec_func_cur_file ) { |
| 87 | &$rec_func("$file"); |
| 88 | } |
| 89 | # Pass &$rec_func the current file name and the current directory |
| 90 | # stack. |
| 91 | elsif ( $pass_rec_func_cur_file_dir ) { |
| 92 | &$rec_func("$file", join("/", @dirstack)); |
| 93 | } |
| 94 | # Pass &$rec_func nothing. |
| 95 | else { |
| 96 | &$rec_func(); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | 1; |
Note: See TracBrowser for help on using the browser.
