home *** CD-ROM | disk | FTP | other *** search
- @rem = '-*- Perl -*-';
- @rem = '
- @echo off
- perl %0.cmd %1 %2 %3 %4 %5 %6 %7 %8 %9
- goto endofperl
- ';
- #
- # A clone of an ls -CF
- # no options at present
- #
-
- $dodirs = 1; # expand directories by default
-
- #
- # if no arguments, do current directory
- #
- if (scalar(@ARGV) == 0) {
- opendir (D, ".") || die "Can't open directory .:$!\n";
- @ARGV = sort readdir(D);
- closedir(D);
- $dodirs = 0; # don't expand directories
- }
- elsif (scalar(@ARGV) == 1) {
- local($dir) = $ARGV[0];
- if (-d $dir) {
- opendir (D, $dir) || die "Can't open directory $dir:$!\n";
- @ARGV = sort readdir(D);
- closedir(D);
- chdir($dir);
- }
- $dodirs = 0; # don't expand directories
- }
-
- #
- # weed out non-existent files
- #
- @bad = grep (! -e && print ("ls: $_: no such file or directory\n"), @ARGV);
- if (scalar(@bad)) {
- @ARGV = grep (-e, @ARGV);
- print "\n";
- }
-
-
- #
- # get rid of . and .., then lower-case the filenames
- #
- @ARGV = grep(!/^[.]/ && !/^[.][.]/ && (tr/A-Z/a-z/ || 1), @ARGV);
-
- #
- # if we're expanding directorys, pull the dirs out of ARGV
- #
- if ($dodirs) {
- @dirs = grep(-d, @ARGV);
- @ARGV = grep (!-d, @ARGV);
- }
- else {
- grep(-d && s/^(.*)$/\1\\/ && 0, @ARGV);
- }
-
- &column_out(@ARGV); # now print them
- &expand_dirs(@dirs) if $dodirs;
- exit(0);
-
- sub expand_dirs {
- local(@dirs) = @_;
- local(@filelist);
-
- foreach $dir (@dirs) {
- opendir(D, $dir) || die "Can't open $dir: $!\n";
- @filelist =
- sort grep(!/^[.]/ && !/^[.][.]/ && (tr/A-Z/a-z/ || 1), readdir(D));
- closedir(D);
- grep(-d && s/^(.*)$/\1\\/ && 0, @filelist);
- print "\n$dir:\n";
- &column_out(@filelist);
- }
- }
-
- #
- # This routine stolen from Kazumasa Utashiro <utashiro@sran230.sra.co.jp>
- # (coombs.anu.edu.au archive file lsC2.pl)
- #
- # Thanks!
- #
-
- sub column_out {
- local(@item) = @_;
- local($[, $i, $maxlen, $w, $c, $l) = (0, 0, 8);
-
- return unless @item;
- for (@item) { $w = (length() + 1 + 7) & ~7 if length() > $w - 1; } # width
- $c = $w >= 80 ? 1 : int(80 / $w); # column
- $l = int((@item + $c - 1) / $c); # line
- $c-- while $l * $c - @item + 1 > $l;
-
- for (@item[sort {$a % $l <=> $b % $l || $a <=> $b} $[ .. $#item]) {
- print;
- print $i == $#item || ++$i % $c == 0 ? "\n" : ' ' x ($w - length);
- }
- }
-
- __END__
- :endofperl
-