File: random-word

package info (click to toggle)
chiark-utils 8.0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,084 kB
  • sloc: ansic: 4,640; perl: 4,281; sh: 671; python: 465; makefile: 286; tcl: 228
file content (104 lines) | stat: -rwxr-xr-x 2,761 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/perl -w

# Copyright 2004 Ian Jackson <ian@chiark.greenend.org.uk>
#
# This script and its documentation (if any) are free software; you
# can redistribute it and/or modify them under the terms of the GNU
# General Public License as published by the Free Software Foundation;
# either version 3, or (at your option) any later version.
# 
# chiark-named-conf and its manpage are distributed in the hope that
# it will be useful, but WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# 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;

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: $!");

while (@ARGV && $ARGV[0] =~ m/^\-/) {
    $_= shift @ARGV;
    if (m/^\-\-?$/) {
	last;
    } elsif (m/^\-n(\d+)$/) {
	$want= $1;
    } elsif (m/^\-f/ && length > 2) {
	$filename= $'; #';
    } elsif (m/^\-F(\d+)$/) {
	$filemaxlen= $1;
    } elsif (m/^\-r/ && length > 2) {
	@randfile= ($'); #');
    } elsif (m/^\-D$/) {
	open D, ">&STDERR" or fail("dup stderr for debug: $!");
    } else {
	fail("unknown option \`$_'");
    }
}

sub debug ($) {
    print D "random-word: debug: $_[0]\n"
	or fail("write debug: $!");
}

our $randfile;
our $r;

for $randfile (@randfile) {
    $r= new IO::File "$randfile", 'r';
    debug("open $randfile ".($r ? "ok" : "failed $!"));
    last if $r;
    $!==&ENOENT or fail("cannot open $randfile: $!");
}
$r or fail("could not open any random device: $!\n (tried @randfile)");
$r->autoflush(0);

our $w= new IO::File $filename, 'r';
$w or fail("cannot open $filename: $!");
debug("open $filename ok");
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");

our @out;
while (@out < $want) {
    my $rbytes;
    $!=0; read $r,$rbytes,4;
    length $rbytes==4 or fail("cannot read $randfile: $!");
    my $wordno= unpack 'L',$rbytes;
    $wordno &= ~0x80000000;
    $wordno %= @words;
    $_= $words[$wordno];
    chomp;
    debug("picked $wordno \`$_'");
    next unless m/^([a-z][-a-z]+)$/;
    push @out, $1;
    debug("good, now ".scalar @out);
}

debug("enough");

print join(' ',@out), "\n"
    or fail("cannot write output: $!");