8 $filename= "/usr/share/dict/words";
9 @randfile= ("/dev/urandom", "/dev/random");
11 sub fail ($) { die "random-word: $_[0]\n"; }
12 open D, ">/dev/null" or fail("open /dev/null: $!");
14 while ($ARGV[0] =~ m/^\-/) {
18 } elsif (m/^\-n(\d+)$/) {
20 } elsif (m/^\-f/ && length > 2) {
22 } elsif (m/^\-r/ && length > 2) {
25 open D, ">&STDERR" or fail("dup stderr for debug: $!");
27 fail("unknown option \`$_'");
32 print D "random-word: debug: $_[0]\n"
33 or fail("write debug: $!");
36 for $randfile (@randfile) {
37 $r= new IO::File "$randfile", 'r';
38 debug("open $randfile ".($r ? "ok" : "failed $!"));
40 $!==&ENOENT or fail("cannot open $randfile: $!");
42 $r or fail("could not open any random device: $!\n (tried @randfile)");
45 $w= new IO::File $filename, 'r';
46 $w or fail("cannot open $filename: $!");
47 debug("open $filename ok");
49 $w->error and fail("cannot read $filename: $!");
50 debug("read $filename ok");
52 while (@out < $want) {
53 $!=0; read $r,$rbytes,4;
54 length $rbytes==4 or fail("cannot read $randfile: $!");
55 $wordno= unpack 'L',$rbytes;
56 $wordno &= ~0x80000000;
60 debug("picked $wordno \`$_'");
61 next unless m/^([a-z][-a-z]+)$/;
63 debug("good, now ".scalar @out);
68 print join(' ',@out), "\n"
69 or fail("cannot write output: $!");