# subroutine name: getcounter # Reads content of a counter file and returns an integer. # If counter file doesn't exist, returns "0". # If error occurs, makes CGI process exit with error # # Global variables used: # $no_flock - disable flock or not sub getcounter { my($datafile) = @_; my $count=0; if (-f $datafile) { # If OS (like Win95) desn't support flock, we have to # go unreliable and slow way and create "lock" file GetFileLock("$datafile.lock"); if (open (COUNTER, "<$datafile")) { # shared locking flock(COUNTER, 1) unless ($no_flock); $count = int || 0; # unlock file flock(COUNTER, 8) unless ($no_flock); close(COUNTER); ReleaseFileLock("$datafile.lock"); } else { ReleaseFileLock("$datafile.lock"); die("Error in subroutine getcounter: Can't open $datafile. Reason: $!"); } }; return $count; }