chiark / gitweb /
Merge remote-tracking branch 'remotes/dgit/dgit/sid'
[chiark-utils.git] / scripts / random-word
index 427e4d08795710586fb6f02229f78cadb98b0ac4..649af152a3c074a55be6e3b3ef33e081ec03881f 100755 (executable)
@@ -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");