add notice about where devurandom.c is from
[detour.git] / devurandom.c
1 // copied from NaCl (the crypto lib)
2
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7
8 /* it's really stupid that there isn't a syscall for this */
9
10 static int fd = -1;
11
12 void randombytes(void *x_,unsigned long long xlen)
13 {
14   unsigned char *x = x_;
15   int i;
16
17   if (fd == -1) {
18     for (;;) {
19       fd = open("/dev/urandom",O_RDONLY);
20       if (fd != -1) break;
21       sleep(1);
22     }
23   }
24
25   while (xlen > 0) {
26     if (xlen < 1048576) i = xlen; else i = 1048576;
27
28     i = read(fd,x,i);
29     if (i < 1) {
30       sleep(1);
31       continue;
32     }
33
34     x += i;
35     xlen -= i;
36   }
37 }