X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-utils.git;a=blobdiff_plain;f=scripts%2Frandom-word;h=649af152a3c074a55be6e3b3ef33e081ec03881f;hp=427e4d08795710586fb6f02229f78cadb98b0ac4;hb=c46b09429cb3309bfb32fd57c294c9dc4d5cf6e9;hpb=7e4802e541e3528f67089f10f7ac299fad97fe75 diff --git a/scripts/random-word b/scripts/random-word index 427e4d0..649af15 100755 --- a/scripts/random-word +++ b/scripts/random-word @@ -25,6 +25,7 @@ use POSIX; 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: $!"); @@ -37,6 +38,8 @@ while (@ARGV && $ARGV[0] =~ m/^\-/) { $want= $1; } elsif (m/^\-f/ && length > 2) { $filename= $'; #'; + } elsif (m/^\-F(\d+)$/) { + $filemaxlen= $1; } elsif (m/^\-r/ && length > 2) { @randfile= ($'); #'); } elsif (m/^\-D$/) { @@ -66,7 +69,16 @@ $r->autoflush(0); our $w= new IO::File $filename, 'r'; $w or fail("cannot open $filename: $!"); debug("open $filename ok"); -our @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");