aboutsummaryrefslogtreecommitdiff
path: root/examples/06-all/rand-bytes.h
blob: b9d38d4a524791647221b6f8ec7b50cc06513f46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef RAND_BYTES_H
#define RAND_BYTES_H

#include <stddef.h> // size_t
#include <sys/random.h> // getrandom()
#include <err.h> // errx()

// Fill `buf` with `len` random bytes using `getrandom()`.
//
// Prints an error and exits with an error code if `len` random bytes
// could not be read.
static void rand_bytes(void * const buf, const size_t len) {
  const ssize_t got = getrandom(buf, len, 0);
  if (got < (ssize_t) len) {
    // print error message, exit with error
    errx(-1, "getrandom() failed");
  }
}

#endif /* RAND_BYTES_H */