common/tools/listdir.pl
changeset 192 d5964b46ccaf
parent 108 d33d43677cdf
equal deleted inserted replaced
191:56e7098e1ace 192:d5964b46ccaf
     1 #!perl -w
     1 #!perl -w
       
     2 # Copyright (c) 2009 Symbian Foundation Ltd
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of the License "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 #
       
     8 # Initial Contributors:
       
     9 # Symbian Foundation Ltd - initial contribution.
       
    10 # 
       
    11 # Contributors:
       
    12 #
       
    13 # Description:
       
    14 # Recursive listing of a directory, outputting lower-cased relative paths with unix dir separators
       
    15 
     2 use strict;
    16 use strict;
     3 
    17 
     4 my $dir      = shift or die "Usage: $0 <dir> \n";   #  provided dir to traverse
    18 my $dir      = shift or die "Usage: $0 <dir> \n";   #  provided dir to traverse
     5 my $filelist = [];
    19 my $filelist = [];
       
    20 my $init = $dir = lc($dir);
       
    21 $init =~ s{\\}{\\\\};
     6 
    22 
     7 # fwd declaration to prevent warning
    23 # fwd declaration to prevent warning
     8 sub recursedir($$);
    24 sub recursedir($$);
     9 
    25 
    10 # run recurse and print
    26 # run recurse and print
    11 recursedir ($dir, $filelist);
    27 recursedir ($dir, $filelist);
       
    28 
    12 print $_, "\n" for(@$filelist);
    29 print $_, "\n" for(@$filelist);
    13 
    30 
    14 sub recursedir($$) {
    31 sub recursedir($$) {
    15 
    32 
    16   my $dir  = shift @_;
    33   my $dir  = shift @_;
    17   my $list = shift @_;
    34   my $list = shift @_;
    18 
    35 
    19   if(opendir(DIR, "$dir")) {
    36   if(opendir(DIR, "$dir")) {
    20     #  list dir
    37     #  list dir
    21     for my $file(grep { !/^\./ } readdir DIR) {
    38     for my $file(grep { !/^\./ } readdir DIR) {
    22       if(-d "$dir\\$file") {
    39       if(-d "$dir/$file") {
    23         #  traverse subdirs
    40         #  traverse subdirs
    24         recursedir("$dir\\$file", $list);
    41         recursedir("$dir/$file", $list);
    25       }
    42       }
    26       elsif(-f "$dir\\$file") {
    43       elsif(-f "$dir/$file") {
    27         #  if file then swap (any present) fwd to bkslash and add to list        
    44         my $formatted = lc($dir)."/".lc($file);
    28         $dir   =~s/\//\\/;
    45         $formatted =~ s!$init/!!;
    29         $file  =~s/\//\\/;
    46         push @$list, $formatted;
    30         push @$list, "$dir\\$file";
       
    31       }
    47       }
    32     }
    48     }
    33     closedir DIR;
    49     closedir DIR;
    34   }
    50   }
    35   else {
    51   else {