#!/usr/bin/perl -Tw use strict; ## mrv: most recent visitors (by host) ## constants: my $MAX = 10; # how many visitors to keep ## system stuff $|++; $ENV{PATH} = "/usr/ucb:/bin:/usr/bin"; ## HTML stuff print "Content-type: text/plain\n\n"; ## the main program (in eval so we can trap problems) eval { ## get the CGI data my $uri = $ENV{DOCUMENT_URI}; my $rhost = $ENV{REMOTE_HOST}; ## split the URI up, so we know where the file was my ($dir,$file) = $uri =~ m,(.*/)(.*),s; ## massage the directory to get the containing dir if ($dir =~ m,^/~(\w+)/(.*)$,s) { $dir = "/home/$1/public_html/$2"; # teleport specific } else { die "cannot translate dir"; } ## go there chdir $dir or die "cannot cd to $dir: $!\n"; ## set up mrv file $file =~ /^([a-z0-9.]+)/i or die "Bad filename: $file"; my $mrv = "$1.mrv"; # compute untainted mrv name open MRV, "+>>$mrv" or die "Cannot open $mrv: $!"; flock MRV, 2; # wait for exclusive lock ## from here to the close MRV, we have critical region ## be sure to minimize this time ## get current content seek MRV, 0, 0; # rewind to beginning my @content = ; # get current content ## massage content $rhost .= "\n"; @content = grep $rhost ne $_, @content; # eliminate dups push @content, $rhost; # add new one to end splice(@content,0,@content - $MAX) if @content > $MAX; # save only last $MAX entries ## write new list, and release file seek MRV, 0, 0; # rewind again truncate $mrv, 0; # empty the file print MRV @content; # print the new content ## release file close MRV; ## now prepare the output ## b; a and b; or a1, a2, a3, ... a9, and b chomp @content; my $last = pop @content; print join ", ", @content; print ", " if @content > 1; print " and " if @content; print $last; }; ## if an error, say so: chomp $@, print "[error: $@]" if $@;