#!/usr/bin/perl # # tentori.cgi - cgi perl script for Tentori Fortune # #------------------------------------------------------------------------------ # # Last Update: Sat Jul 25 13:41:53 JST 1998 # Maintainer: Gen KUROKI # #------------------------------------------------------------------------------ # # First written by Tetsutaro UEHARA, thanks to SAKAI. # # 12 Jul 1998 Taro NAKANO (use $tmpfile) # 16 Jul 1998 Gen KUROKI (add Lock() and Unlock()) # 21 Jul 1998 Gen KUROKI (change documents) # 25 Jul 1998 Gen KUROKI (use $#lines instead of `wc ...`) # 25 Jul 1998 Gen KUROKI (write $lines on $tmpfile before printing fortune) # 26 Aug 1998 Gen KUROKI (add alternate Lock() commented) # #------------------------------------------------------------------------------ # # Locations # # 1. Get the original archive from # # ftp://ftp.kuis.kyoto-u.ac.jp/misc/tentori.tar.gz # (or http://www.math.tohoku.ac.jp/~kuroki/pub/tentori/tetsu/tentori.tar.gz) # # which contains tentori.data. For more information, see also # # http://grape.c.u-tokyo.ac.jp/~nakano/tentori.html # http://grape.c.u-tokyo.ac.jp/~nakano/tenjoy.html # http://www.kutsuda.kuis.kyoto-u.ac.jp/Members/tetsu/tentori.html # # 2. Get this script from # # http://www.math.tohoku.ac.jp/~kuroki/pub/tentori/tentori.txt # # and rename it tentori.cgi. Outputs of this script can be enjoyed at # # http://www.math.tohoku.ac.jp/~kuroki/tentori.cgi # #------------------------------------------------------------------------------ # # Installation # # 1. Change the '#!' line of this script to use perl version 5. # 2. Change the '$datadir' line to set the working directory of the script. # 3. Open the write permission of $datadir. (chmod a+w $datadir) # 4. Copy tentori.data in tentori.tar.gz to $datadir. # 5. Create $datadir/tentori.tmp by # cd $datadir; cp tentori.data tentori.tmp; chmod a+w tentori.tmp # 6. Invoke directly this script for test. # 7. Access this CGI script via WWW. # #------------------------------------------------------------------------------ # Customization: Change the following line appropriately. # #$datadir = "/home2/wheel/kuroki/public_html/tentori"; $datadir = "/home2/mi-staff/kuroki/public_html/tentori"; #------------------------------------------------------------------------------ $datafile = "$datadir/tentori.data"; $tmpfile = "$datadir/tentori.tmp"; $lockfile = "$datadir/tentori.lock"; print "Content-type: text/html\n"; print "\n"; if (!&Lock()) { print "Busy\n"; print "

Busy

\n"; print "

Temporary file is locked. "; print "Please wait a moment and retry.

\n"; print "\n"; exit 0; } open(TMP, $tmpfile) or die "Cannot read tmpfile: $!\n"; @lines = ; close TMP; if($#lines < 0) { open(ORG, $datafile) or die "Cannot read datafile: $!\n"; @lines = ; close ORG; } srand(time); $word = splice(@lines, rand @lines, 1); $word =~ s/[\n\r]*$//; open(TMP, ">$tmpfile") or die "Cannot write tmpfile: $!\n"; print TMP @lines; close TMP; &Unlock(); print "Tentori Fortune\n"; print "

Tentori Fortune

\n"; print "

$word

\n"; print "

\n"; print "The original data and script are written by Tetsutaro UEHARA.
"; print "The script is modified by Taro NAKANO and Gen KUROKI.\n"; print "

\n"; print "\n"; exit 0; #------------------------------------------------------------------------------ sub Lock { local($retry) = 3; while (!symlink("LOCK", $lockfile)) { if (--$retry <= 0) { return 0; } sleep(2); } return 1; } # Alternate Lock() which does not contain symlink() # #sub Lock { # local($retry) = 3; # while (-e "$lockfile") { # if (--$retry <= 0) { return 0; } # sleep(2); # } # open(LOCK, ">$lockfile"); close(LOCK); # return 1; #} sub Unlock { if (!unlink($lockfile)) { return 0; } return 1; } #------------------------------------------------------------------------------