# subroutine name: appenddata # Appends passed string and newline char ("\n") to specified file # If error occurs, makes CGI process exit with error # # Global variables used: # $no_flock - disable flock or not sub appenddata { my($datafile,$row) = @_; # If OS (like Win95) desn't support flock, we have to # go unreliable and slow way and create "lock" file GetFileLock("$datafile.lock"); my $existed = -e $datafile; local *DATA; if (open(DATA,">>$datafile")) { unless ($no_flock) { # Exclusive locking flock(DATA, 2) ; # In case someone appended while we # were waiting, seek to EOF seek(DATA, 0, 2); } print DATA "$row\n"; # unlock file flock(DATA, 8) unless ($no_flock); close(DATA); chmod(0666, $datafile) unless ($existed); ReleaseFileLock("$datafile.lock"); } else { ReleaseFileLock("$datafile.lock"); die("Error in subroutine appenddata: Can't open $datafile. Reason: $!"); } }