diff options
author | Mattias Andrée <maandree@kth.se> | 2024-09-17 19:32:05 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-09-17 19:32:09 +0200 |
commit | 7df27c110a0f0a97a940283e20fc52b267f6fdab (patch) | |
tree | daa477fbd9c15592a868f6d28f84d12641a47ac4 /deadshred.c | |
parent | Add README and deadshred.1 (diff) | |
download | deadshred-7df27c110a0f0a97a940283e20fc52b267f6fdab.tar.gz deadshred-7df27c110a0f0a97a940283e20fc52b267f6fdab.tar.bz2 deadshred-7df27c110a0f0a97a940283e20fc52b267f6fdab.tar.xz |
Use rand(3) when stdin is a TTY
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | deadshred.c | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/deadshred.c b/deadshred.c index 5323d15..5f8f36b 100644 --- a/deadshred.c +++ b/deadshred.c @@ -4,7 +4,7 @@ #include <libsimple-arg.h> -USAGE("file"); +USAGE("device [< random-source]"); struct span { @@ -23,6 +23,7 @@ static off_t total_size = 0; static char reservoir[128U << 10]; static size_t reservoir_off = sizeof(reservoir); +static int use_stdin; static char total_size_1000[256]; static char total_size_1024[256]; @@ -60,6 +61,12 @@ ensure_random(size_t needed) if (sizeof(reservoir) - reservoir_off >= needed) return; + if (!use_stdin) { + libsimple_random_bytes(&libsimple_random_bits, NULL, reservoir, reservoir_off); + reservoir_off = 0; + return; + } + for (off = 0; off < reservoir_off;) { r = read(STDIN_FILENO, &reservoir[off], reservoir_off - off); if (r <= 0) { @@ -206,6 +213,11 @@ main(int argc, char *argv[]) if (fd < 0) eprintf("open %s O_WRONLY:", argv[0]); + use_stdin = !isatty(STDIN_FILENO); + if (!use_stdin) { + libsimple_srand(); + } + spans = emalloc(sizeof(*spans)); spans[0].start = 0; spans[0].end = total_size = filesize(fd, argv[0]); |