Conman Laboratories

Better living through software …

Nerd Trivia (the question)

Mark Grosberg

<script language="php"> function get_next_record($fd, $kind) { for($once = false;; $once = true) { $last_ch = "\n"; while (!feof($fd)) { $ch = fgetc($fd); if (($last_ch == "\n") && ($ch == "~")) { if (feof($fd)) die("Invalid tag specifier."); $next_ch = fgetc($fd); if ($next_ch == $kind) return; $ch = $next_ch; } $last_ch = $ch; } // Whoops, hit EOF, back up. if ($once) die("Internal error."); rewind($fd); } } function read_until($fd, $term) { $result = ""; while ($s = fgets($fd, 4096)) { if ($s == ($term . "\n")) break; $result = $result . $s; } return ($result); } // Location of the database file. $trivia_file = "/app/httpd/run/sites/www.conman.org/htdocs/projects" . "/nerd_trivia/trivia.dat"; // Initialize the random number generator. srand((double )microtime() * 1000.66 + getmypid()); // Open the trivia file. $fd = fopen($trivia_file, "r"); if (!$fd) die("Can't open trivial file."); // Now, seek to a random spot in the file. $pos_frac = (double )rand() / (double )getrandmax(); $pos = round($pos_frac * (double )filesize($trivia_file)); if (fseek($fd, $pos) != 0) die("Seek error."); // Align to a record boundary. get_next_record($fd, 'Q'); $trivia_loc = ftell($fd) ^ 0x31ee4f; $trivia = read_until($fd, "~A"); $answer = ftell($fd) ^ 0xf0a5b1; fclose($fd); </script>

Question:

<?php echo("$trivia"); ?>

<?php echo("<input type=\"hidden\" name=\"answer_key\" value=\"$answer\">"); echo("\n"); echo("<input type=\"hidden\" name=\"trivia\" value=\"$trivia_loc\">"); echo("\n"); ?>