#!/usr/local/bin/perl ########################## ## Just an illustration of file locking. ## This is much safer than just writing. ## However, this example is not perfect. ## The print statement could fail, or ## the original flock might not return. ## A more robust program would take ## this into account. ## use Fcntl; # defines constants used by flock my $file = '/homes/home8/adabyron/cgi-bin/output'; $ENV{PATH} = ''; $ENV{ENV} = ''; print "Content-type: text/plain\n\n"; ## Don't let the output file grow arbitrarily. if ( -s $file > 4000) { print "Sorry, file is too big\n"; unlink $file; exit; } else { ## Print accurate error messages when possible unless (open (F, ">>$file") ) { print "Sorry, can't open $file\n"; exit; } } flock(F, LOCK_EX); # get exclusive lock print F `/usr/bin/date`; # write to file flock(F, LOCK_UN); # release lock close F; print "File has been written\n";