Subject: | _makerandom() fails on HPUX; retry sysread(urandom) |
This is perl, v5.8.7 built for PA-RISC2.0-thread-multi-LP64
_makerandom attempts to sysread from urandom. HP-UX only supports
reading up to 256 bytes at a time, so the test suite fails.
Working code follows:
sub _makerandom {
my $size = shift;
my $bytes = int($size / 8) + ($size % 8 ? 1 : 0);
my $rand = '';
if (-e "/dev/urandom") {
my $fh;
open($fh, '/dev/urandom')
or die "Couldn't open /dev/urandom";
while ( ( my $len = length($rand) ) < $bytes ) {
my $got = sysread $fh, $rand, $bytes - $len, $len;
die "Didn't read all bytes from urandom" unless $got;
}
close $fh;
} else {
for (1..$bytes) {
$rand .= chr(int(rand(256)));
}
}
my $bits = unpack("b*", $rand);
die unless length($bits) >= $size;
Math::BigInt->new('0b' . substr($bits, 0, $size));
}