chiark
/
gitweb
/
~ian
/
chiark-utils.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
finalise 4.4.2
[chiark-utils.git]
/
scripts
/
random-word
diff --git
a/scripts/random-word
b/scripts/random-word
index 66d09a27027afc28589ca4c65e18e08ce2007bbc..649af152a3c074a55be6e3b3ef33e081ec03881f 100755
(executable)
--- a/
scripts/random-word
+++ b/
scripts/random-word
@@
-1,4
+1,4
@@
-#!/usr/bin/perl
+#!/usr/bin/perl
-w
# Copyright 2004 Ian Jackson <ian@chiark.greenend.org.uk>
#
# Copyright 2004 Ian Jackson <ian@chiark.greenend.org.uk>
#
@@
-16,27
+16,32
@@
# with this program; if not, consult the Free Software Foundation's
# website at www.fsf.org, or the GNU Project website at www.gnu.org.
# with this program; if not, consult the Free Software Foundation's
# website at www.fsf.org, or the GNU Project website at www.gnu.org.
+use strict;
+
use IO::Handle;
use IO::File;
use POSIX;
use IO::Handle;
use IO::File;
use POSIX;
-$want= 1;
-$filename= "/usr/share/dict/words";
-@randfile= ("/dev/urandom", "/dev/random");
+our $want= 1;
+our $filename= "/usr/share/dict/words";
+our @randfile= ("/dev/urandom", "/dev/random");
+our $filemaxlen;
sub fail ($) { die "random-word: $_[0]\n"; }
open D, ">/dev/null" or fail("open /dev/null: $!");
sub fail ($) { die "random-word: $_[0]\n"; }
open D, ">/dev/null" or fail("open /dev/null: $!");
-while ($ARGV[0] =~ m/^\-/) {
+while (
@ARGV &&
$ARGV[0] =~ m/^\-/) {
$_= shift @ARGV;
if (m/^\-\-?$/) {
last;
} elsif (m/^\-n(\d+)$/) {
$want= $1;
} elsif (m/^\-f/ && length > 2) {
$_= shift @ARGV;
if (m/^\-\-?$/) {
last;
} elsif (m/^\-n(\d+)$/) {
$want= $1;
} elsif (m/^\-f/ && length > 2) {
- $filename= $';
+ $filename= $'; #';
+ } elsif (m/^\-F(\d+)$/) {
+ $filemaxlen= $1;
} elsif (m/^\-r/ && length > 2) {
} elsif (m/^\-r/ && length > 2) {
- @randfile= ($');
+ @randfile= ($');
#');
} elsif (m/^\-D$/) {
open D, ">&STDERR" or fail("dup stderr for debug: $!");
} else {
} elsif (m/^\-D$/) {
open D, ">&STDERR" or fail("dup stderr for debug: $!");
} else {
@@
-49,6
+54,9
@@
sub debug ($) {
or fail("write debug: $!");
}
or fail("write debug: $!");
}
+our $randfile;
+our $r;
+
for $randfile (@randfile) {
$r= new IO::File "$randfile", 'r';
debug("open $randfile ".($r ? "ok" : "failed $!"));
for $randfile (@randfile) {
$r= new IO::File "$randfile", 'r';
debug("open $randfile ".($r ? "ok" : "failed $!"));
@@
-58,17
+66,28
@@
for $randfile (@randfile) {
$r or fail("could not open any random device: $!\n (tried @randfile)");
$r->autoflush(0);
$r or fail("could not open any random device: $!\n (tried @randfile)");
$r->autoflush(0);
-$w= new IO::File $filename, 'r';
+
our
$w= new IO::File $filename, 'r';
$w or fail("cannot open $filename: $!");
debug("open $filename ok");
$w or fail("cannot open $filename: $!");
debug("open $filename ok");
-@words= <$w>;
+our @words;
+if (defined $filemaxlen) {
+ while (@words < $filemaxlen) {
+ my $l = <$w>;
+ last unless defined $l;
+ push @words, $l;
+ }
+} else {
+ @words= <$w>;
+}
$w->error and fail("cannot read $filename: $!");
debug("read $filename ok");
$w->error and fail("cannot read $filename: $!");
debug("read $filename ok");
+our @out;
while (@out < $want) {
while (@out < $want) {
+ my $rbytes;
$!=0; read $r,$rbytes,4;
length $rbytes==4 or fail("cannot read $randfile: $!");
$!=0; read $r,$rbytes,4;
length $rbytes==4 or fail("cannot read $randfile: $!");
- $wordno= unpack 'L',$rbytes;
+
my
$wordno= unpack 'L',$rbytes;
$wordno &= ~0x80000000;
$wordno %= @words;
$_= $words[$wordno];
$wordno &= ~0x80000000;
$wordno %= @words;
$_= $words[$wordno];