rendered paste body#!/usr/bin/perl
use CGI;
#change this to the full pathname to the bottom of the dir structure:
my $dir = "/tmp/";
print "Content-type: text/html\n\n";
my $cgiin = CGI->new;
my $letter = $cgiin->param('letter');
if (($letter eq "") && ($ARGV[0] =~ /[\w|\.]/)) {
$letter = $ARGV[0];
print "Running from the command line, looking for things that start with $letter\n";
}
if ($letter !~ /[\w|\.]/) {
print "<h2>Please specify a valid letter</h2>";
die "Invalid letter given";
}
my @errors;
#my $givendir = $ARGV[0];
#if ($givendir eq "") {
# $givendir = ".";
#}
opendir('FLIST', "$dir");
my @dirs = readdir('FLIST');
closedir('FLIST');
while(@dirs) {
my $workdir = shift(@dirs);
# print "Checking $workdir\n";
while(($workdir eq '.') || ($workdir eq '..')) {
# print "NOT reading $workdir since its a trick!\n";
$workdir = shift(@dirs);
}
my $oldworkdir = $workdir;
$workdir = $dir . $workdir;
# print "Opening $workdir... ";
opendir('CURDIR', "$workdir");
my @prefilelist = readdir('CURDIR');
closedir('CURDIR');
# print "Found this: @prefilelist\n";
my @filelist;
while(@prefilelist) {
my $workline = shift(@prefilelist);
if (($workline ne '.') && ($workline ne '..')) {
push(@filelist, "$workline");
# print "$workline seems cools!\n";
} else {
# print "NOT reading $workline inside $workdir, since its a trap!\n";
}
}
my $numfiles = @filelist;
if ($numfiles != 1) {
push(@errors, "in $workdir: expected 1 file, found $numfiles");
# print "No way... Too many files in $workdir, just gonna ignore it\n";
} else {
my @firstletter = split(//, $oldworkdir);
my $uc = uc($firstletter[0]);
my $lc = lc($firstletter[0]);
# print "uc is $uc, and lc is $lc\n";
if (($uc eq $letter) || ($lc eq $letter)) {
print "<a href=\"/cgi-bin/download.pl?filename=$oldworkdir\">$workdir</a><br />\n";
} else {
# print "Not matching $workdir because it doesn't start with a $letter<br />\n";
}
if (($letter eq '.') && ($oldworkdir =~ /^\d/)) {
print "<a href=\"/cgi-bin/download.pl?filename=$workdir\">$workdir</a><br />\n";
}
}
}
#my $numerrors = @errors;
#if ($numerrors != 0) {
# print "WARNING: $numerrors reported errors:\n";
#}
#while(@errors) {
# my $errorline = shift(@errors);
# print "$errorline\n";
#}