yu.dong | c33b307 | 2024-08-21 23:14:49 -0700 | [diff] [blame^] | 1 | #!/usr/bin/perl |
| 2 | # |
| 3 | # Copyright Statement: |
| 4 | # -------------------- |
| 5 | # This software is protected by Copyright and the information contained |
| 6 | # herein is confidential. The software may not be copied and the information |
| 7 | # contained herein may not be used or disclosed except with the written |
| 8 | # permission of MediaTek Inc. (C) 2005 |
| 9 | # |
| 10 | # BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES |
| 11 | # THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE") |
| 12 | # RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON |
| 13 | # AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES, |
| 14 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF |
| 15 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT. |
| 16 | # NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE |
| 17 | # SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR |
| 18 | # SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH |
| 19 | # THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO |
| 20 | # NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S |
| 21 | # SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM. |
| 22 | # |
| 23 | # BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE |
| 24 | # LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE, |
| 25 | # AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE, |
| 26 | # OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO |
| 27 | # MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. |
| 28 | # |
| 29 | # THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE |
| 30 | # WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF |
| 31 | # LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND |
| 32 | # RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER |
| 33 | # THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC). |
| 34 | # |
| 35 | # |
| 36 | #***************************************************************************** |
| 37 | #***************************************************************************** |
| 38 | ##* |
| 39 | ##* Filename: |
| 40 | ##* --------- |
| 41 | ##* find.pl |
| 42 | ##* |
| 43 | ##* Project: |
| 44 | ##* -------- |
| 45 | ##* MOLY_Software |
| 46 | ##* |
| 47 | ##* Description: |
| 48 | ##* ------------ |
| 49 | ##* list directory contents |
| 50 | ##* |
| 51 | ##* Author: |
| 52 | ##* ------- |
| 53 | ##* kk Lin-Wang (mtk04222) |
| 54 | ##* |
| 55 | ##*============================================================================ |
| 56 | use strict; |
| 57 | use warnings; |
| 58 | use File::Find; |
| 59 | |
| 60 | my $option = "all"; |
| 61 | my $root_dir; |
| 62 | my $regex; |
| 63 | my $re_code; |
| 64 | my @all_list; |
| 65 | |
| 66 | # process arguments |
| 67 | if($#ARGV >= 0){ |
| 68 | $option = shift; |
| 69 | if ($option =~ /^\-f\b/) { |
| 70 | $option = "file"; |
| 71 | } elsif ($option =~ /^\-d\b/) { |
| 72 | $option = "folder"; |
| 73 | } else { |
| 74 | unshift @ARGV, $option; |
| 75 | $option = "all"; |
| 76 | } |
| 77 | } |
| 78 | $root_dir = shift || '.'; |
| 79 | $regex = shift || '.'; |
| 80 | &error_handler("$root_dir can Not be found !", __FILE__, __LINE__) if(! -e $root_dir); |
| 81 | |
| 82 | # search recursively |
| 83 | find(\&want ,$root_dir); |
| 84 | |
| 85 | # list the search results by option |
| 86 | $re_code = &output_lists(\@all_list, $regex); |
| 87 | &error_handler("execution failed!", __FILE__, __LINE__) if($re_code != 0); |
| 88 | |
| 89 | #****************************************************************************** |
| 90 | # FUNCTION |
| 91 | # want |
| 92 | # DESCRIPTION |
| 93 | # The function does whatever verifications you want. |
| 94 | #****************************************************************************** |
| 95 | sub want |
| 96 | { |
| 97 | if ($option eq "all") { |
| 98 | push @all_list, $File::Find::name if(-e $_); |
| 99 | } elsif ($option eq "file") { |
| 100 | push @all_list, $File::Find::name if(-f $_); |
| 101 | } elsif ($option eq "folder") { |
| 102 | push @all_list, $File::Find::name if(-d $_); |
| 103 | } else { |
| 104 | &error_handler("No such option valut: $option !", __FILE__, __LINE__) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | #****************************************************************************** |
| 110 | # FUNCTION |
| 111 | # output_lists |
| 112 | # DESCRIPTION |
| 113 | # output the files/folders according to regular expr |
| 114 | # PARAMETER |
| 115 | # @list : output list |
| 116 | # $rule : regular expression |
| 117 | #****************************************************************************** |
| 118 | sub output_lists |
| 119 | { |
| 120 | my $lists = shift; |
| 121 | my $rule = shift; |
| 122 | foreach my $f (@$lists) |
| 123 | { |
| 124 | next if($f eq "."); |
| 125 | next if($f eq ".."); |
| 126 | print $f."\n" if($f =~ /$rule$/); |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | #**************************************************************************** |
| 134 | # FUNCTION |
| 135 | # error_handler |
| 136 | # PARAMETER |
| 137 | # $error_msg: error message |
| 138 | # $file : file |
| 139 | # $line_no : line number |
| 140 | #**************************************************************************** |
| 141 | sub error_handler |
| 142 | { |
| 143 | my ($error_msg, $file, $line_no) = @_; |
| 144 | |
| 145 | my $final_error_msg = "RECURSIVE_DIR Error: $error_msg at $file line $line_no\n"; |
| 146 | print $final_error_msg; |
| 147 | die $final_error_msg; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | #****************************************************************************** |
| 152 | # FUNCTION |
| 153 | # Usage |
| 154 | # DESCRIPTION |
| 155 | # Display the manipulation of this script |
| 156 | #****************************************************************************** |
| 157 | sub Usage |
| 158 | { |
| 159 | warn <<"_END_OF_USAGE"; |
| 160 | Usage: |
| 161 | perl recursive_dir.pl [-f|-d] [path] [regular expression] |
| 162 | Description: |
| 163 | List directory/sub-directory contents. |
| 164 | By default, the path and regular expression is '.' |
| 165 | -f only list the files. |
| 166 | -d only list the folders. |
| 167 | Example: |
| 168 | current path: |
| 169 | -foo (folder) |
| 170 | --foo.pl (file) |
| 171 | -sum.txt (file) |
| 172 | ===================================== |
| 173 | ex1: |
| 174 | Command: |
| 175 | perl recursive_dir.pl |
| 176 | Output: |
| 177 | ./sum.txt |
| 178 | ./foo |
| 179 | ./foo/foo.pl |
| 180 | ------------------------------------ |
| 181 | ex2: |
| 182 | Command: |
| 183 | perl recursive_dir.pl -f |
| 184 | Output: |
| 185 | ./sum.txt |
| 186 | ./foo/foo.pl |
| 187 | ------------------------------------ |
| 188 | ex3: |
| 189 | Command: |
| 190 | perl recursive_dir.pl foo |
| 191 | Output: |
| 192 | foo |
| 193 | foo/foo.pl |
| 194 | ------------------------------------ |
| 195 | ex4: |
| 196 | Command: |
| 197 | perl recursive_dir.pl foo "\\.pl" |
| 198 | Output: |
| 199 | foo/foo.pl |
| 200 | ------------------------------------ |
| 201 | |
| 202 | _END_OF_USAGE |
| 203 | exit 1; |
| 204 | } |
| 205 | |