-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeturand16.c
39 lines (33 loc) · 896 Bytes
/
geturand16.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <inttypes.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
#define MAXBUFFERSIZE 4096
/* This program opens /dev/urandom, and prints 2-byte hexies
* until it gets the decimal number 26985.
* */
int main(int argc, char *argv[]) {
int fd = open("/dev/urandom", O_RDONLY);
uint16_t rand_2bytes = 0;
if (read(fd, &rand_2bytes, 2) == -1) {
close(fd);
errExit("read");
}
while (rand_2bytes != 26985) {
if (read(fd, &rand_2bytes, 2) == -1) {
close(fd);
errExit("read");
}
printf("rand_2bytes: %04x\n", rand_2bytes);
}
if (close(fd) == -1) {
errExit("close");
}
return EXIT_SUCCESS;
}