diff options
Diffstat (limited to 'ffd/util.c')
-rw-r--r-- | ffd/util.c | 36 |
1 files changed, 36 insertions, 0 deletions
@@ -24,9 +24,16 @@ */ +#define _GNU_SOURCE + #include "util.h" +#include <errno.h> +#include <fcntl.h> #include <stdio.h> +#include <unistd.h> + +#include <sys/stat.h> bool file_readv(const char *file, const char *format, va_list ap) { @@ -40,3 +47,32 @@ bool file_readv(const char *file, const char *format, va_list ap) { return (ret > 0); } + + + +void random_bytes(void *buffer, size_t len) { + size_t read_bytes = 0; + + int fd = open("/dev/urandom", O_RDONLY); + + if (fd < 0) { + fprintf(stderr, "unable to open random device: %m\n"); + exit(1); + } + + while (read_bytes < len) { + ssize_t ret = read(fd, ((char*)buffer)+read_bytes, len-read_bytes); + + if (ret < 0) { + if (errno == EINTR) + continue; + + fprintf(stderr, "unable to read from random device: %m\n"); + exit(1); + } + + read_bytes += ret; + } + + close(fd); +} |