-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathexample.c
39 lines (35 loc) · 838 Bytes
/
example.c
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
#if HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h> /* For free(3). */
#include "system.h"
int
main(void)
{
FILE *fp;
char *buf;
unsigned int random;
if ((fp = fopen("/dev/urandom", "r")) == NULL &&
(fp = fopen("/dev/random", "r")) == NULL) {
perror("Cannot open random device");
return 1;
}
if (fread(&random, sizeof(random), 1, fp) != 1) {
(void)fputs("Cannot read random device.", stderr);
return 1;
}
if (fclose(fp) != 0) {
perror("Cannot close random device");
return 1;
}
/* We never heard of printf(3), so we use asprintf(3)/puts(3) :-) */
if (asprintf(&buf, "Random %zu-bit integer: %#.*x",
sizeof(random) * 8, (int)sizeof(random) * 2, random) < 0) {
perror("asprintf(3) failed");
return 1;
}
(void)puts(buf);
free(buf);
return 0;
}