AnyEvent-FTP
view release on metacpan or search on metacpan
share/ppt/ls.pl view on Meta::CPAN
my $Piece = ""; # piece of entry list
my @SortedEntries = (); # sorted entry list
my $Rows = 0; # output rows for this List()
my $Target = 0; # target element index
my $TotalBlocks = 0; # total directory size in blocks
my $elt = 0; # element index
# ------ get directory entries attributes
$Attributes = pop(@_);
# ------ precompute max entry length and total size
foreach (@_) {
$TotalBlocks +=
(!defined($Attributes->{$_}) ||
($Attributes->{$_}->blocks eq '')) ?
0: $Attributes->{$_}->blocks;
$Mylen = length($_);
if ($Mylen > $Maxlen) {
$Maxlen = $Mylen;
}
}
$Maxlen += 1; # account for spaces
# ------ print directory name if -R
if (exists($Options->{'R'})) {
print "$Name:\n";
}
# ----- print total in blocks if -s or -l
if (exists($Options->{'l'}) || exists($Options->{'s'})) {
print "total $TotalBlocks\n";
}
# ------ sort entry list
@SortedEntries = Order(\%Options, $Attributes, @_);
# ------ user requested 1 entry/line, long, size, or inode
if (defined($Options->{'1'}) ||
exists($Options->{'l'}) ||
exists($Options->{'s'}) ||
exists($Options->{'i'})) {
foreach $Entry (@SortedEntries) {
EntryFormat(\%Options,
$Attributes, $Entry);
}
# ------ multi-column output
} else {
# ------ compute rows, columns, width mask
$Cols = int($WinCols / $Maxlen) || 1;
$Rows = int(($#_+$Cols) / $Cols);
$Mask = sprintf("%%-%ds ", $Maxlen);
for ($elt = 0; $elt < $Rows * $Cols; $elt++) {
$Target = ($elt % $Cols) * $Rows +
int(($elt / $Cols));
$Piece = sprintf($Mask,
$Target < ($#SortedEntries + 1) ?
$SortedEntries[$Target] : "");
# don't blank pad to eol of line
$Piece =~ s/\s+$//
if (($elt+1) % $Cols == 0);
print $Piece;
print "\n" if (($elt+1) % $Cols == 0);
}
print "\n" if (($elt+1) % $Cols == 0);
}
# ------ print blank line if -R
if (exists($Options->{'R'})) {
print "\n";
}
# ------ list subdirectories of this directory
if (!exists($Options{'d'}) &&
($Expand || exists($Options->{'R'}))) {
foreach $Entry (Order(\%Options, $Attributes, @_)) {
next if ($Entry eq "." || $Entry eq "..");
if (defined($Attributes->{$Entry}) &&
$Attributes->{$Entry}->mode & 0040000) {
$Path = File::Spec->canonpath(File::Spec->catdir($Name,$Entry));
@Dirs = DirEntries(\%Options, $Path);
List($Path, \%Options, 0, @Dirs);
}
}
}
}
# ------ sort file list based on %Options
sub Order {
my $Options = shift; # parsed option/flag arguments
my $Attributes = shift; # File::stat attributes hashref
my @Entries = @_; # directory entry names
# ------ sort by size, largest first
if (exists($Options->{'S'})) {
if (exists($Options->{'r'})) {
@Entries = sort
{ $Attributes->{$a}->size <=>
$Attributes->{$b}->size }
@Entries;
} else {
@Entries = sort
{ $Attributes->{$b}->size <=>
$Attributes->{$a}->size }
@Entries;
}
# ------ sort by time, most recent first
} elsif (exists($Options->{'t'}) ||
exists($Options->{'c'}) ||
exists($Options->{'u'})) {
if (exists($Options->{'r'})) {
if (exists($Options->{'u'})) {
@Entries = sort
{ $Attributes->{$a}->atime <=>
$Attributes->{$b}->atime }
@Entries;
} elsif (exists($Options->{'c'})) {
@Entries = sort
( run in 2.284 seconds using v1.01-cache-2.11-cpan-98e64b0badf )