#!/usr/bin/perl use warnings; use strict; ######################################### # dlist.pl for binrev pc #6 # # by: robf # # # # returns directory listing similar to # # ls, with some sorting an display # # options. # # license: GPL # # # # usage: ./dlist.pl [-Options] # # ------------------------------------ # # use -? for help # # # ######################################### my $directory = "/home/robf/src/perl/pc6"; # debug, release will read from $ARGV[]; ### Global vars my %file; my %options; my @output; # my $directory; ### ### Main getargs(); dirlist($directory); sortfiles(); output(); ### ### Argument handling sub getargs { my $definedDir; foreach (@ARGV) { if ( $_ =~ m/^-([adhprs])$/ ) { $options{$1} = "1"; } elsif ( $_ =~ m/^-S([ndts])$/ ) { $options{S} = $1; } elsif ( $_ =~ m/-\?/ ) { displayHelp(); exit; } elsif ( $_ =~ m/^(-.*)$/ ) { die "option $1 is unknown please use -? for help\n"; } } } sub displayHelp { print < example: $0 / -a -St -S [n]ame(default), [d]ate, [t]ype, [s]ize -s display file sizes -a show all in output [size, permissions, date] -p show permissions -d show date modified -r recursive list -h show hidden files ENDHELP } ### ### Directory listing sub dirlist { opendir( DIR, "$_[0]" ) || die "cannot open $_[0]: $!"; my @readdir = readdir(DIR); close DIR; foreach (@readdir) { open( CURFILE, "$_" ); next if ( $_ =~ m/^(\.|\.\.)$/ ); next if ( $_ =~ m/^\..*/ && not( $options{a} || $options{h} ) ); ( $file{$_}{dev}, $file{$_}{ino}, $file{$_}{mode}, $file{$_}{nlink}, $file{$_}{uid}, $file{$_}{gid}, $file{$_}{rdev}, $file{$_}{size}, $file{$_}{atime}, $file{$_}{mtime}, $file{$_}{ctime}, $file{$_}{blksize}, $file{$_}{blocks}, ) = stat CURFILE; $file{$_}{ctimeHuman} = localtime( $file{$_}{ctime} ); # Human Readable Ctime $file{$_}{numPerms} = ( sprintf "%04o", $file{$_}{mode} & 07777 ) ; # Get numeric permissions from Mode if ( -f _ ) # get the file type for each file. { $file{$_}{type} = ""; } elsif ( -d _ ) { $file{$_}{type} = ""; } elsif ( -l _ ) { $file{$_}{type} = "link"; } else { $file{$_}{type} = "file type not known/handled at this time."; } close CURFILE; } } ### ### Output options sub outputopts { my $filename = shift; my $count = shift; my $bytesize; my $metric; my $formattedSize = $file{$filename}{size}; while ( $formattedSize > 1024 ) { $formattedSize /= 1024; $metric++; } if ( $metric == 0 ) { $bytesize = "B"; } elsif ( $metric == 1 ) { $bytesize = "KB"; } elsif ( $metric == 2 ) { $bytesize = "MB"; } elsif ( $metric == 3 ) { $bytesize = "GB"; } elsif ( $metric == 4 ) { $bytesize = "TB"; } $formattedSize = sprintf( "%u", $formattedSize ); if ( $options{a} ) { $output[$count] =~ s/(.*)/$file{$filename}{type}\t$formattedSize$bytesize\t$file{$filename}{numPerms} $file{$filename}{ctimeHuman}\t$1/; return; } } ### ### Sort options sub sortfiles { $options{S} = "n" unless $options{S}; my $filename; my @filelist; my $count = 0; foreach $filename ( keys %file ) # { push @filelist, $filename; } if ( $options{S} eq "d" ) { print "sorting by date is unimplemented.\n"; } elsif ( $options{S} eq "t" ) { print "sorting by type is unimplemented.\n"; } elsif ( $options{S} eq "s" ) { print "sorting by size is unimplemented.\n"; } elsif ( $options{S} eq "n" ) { @output = sort @filelist; } foreach (@output) { outputopts( $_, $count ); $count++; } } ### ### Final Output sub output { foreach (@output) { print $_ . "\n"; } } ###