ninenano

Client implementation of the 9P protocol for constrained devices

git clone https://git.8pit.net/ninenano.git

 1#include <stdlib.h>
 2#include <stdint.h>
 3#include <fcntl.h>
 4#include <unistd.h>
 5#include <assert.h>
 6
 7#include <sys/stat.h>
 8#include <sys/types.h>
 9
10#include "9util.h"
11
12void
13initrand(void)
14{
15	uint8_t seed;
16	int fd;
17
18	/* If we can't read from /dev/random we will use a hardcode
19	 * value for seeding which not optimal but sufficient. */
20
21	if ((fd = open("/dev/random", O_RDONLY)) == -1) {
22		seed = 23;
23		goto srand;
24	}
25
26	if (read(fd, &seed, 1) != 1)
27		seed = 42;
28	close(fd);
29
30srand:
31	srand(seed);
32}
33
34uint32_t
35randu32(void)
36{
37	int ret;
38
39	/* From rand(3):
40	 *   The rand() function returns a result in the range of 0 to
41	 *   `RAND_MAX`.
42	 */
43	ret = rand();
44	assert(ret >= 0);
45
46	return (uint32_t)rand();
47}