Fix another giant batch of resource leaks. (Mostly memory, but there's
[u/mdw/putty] / unix / uxgen.c
1 /*
2 * uxgen.c: Unix implementation of get_heavy_noise() from cmdgen.c.
3 */
4
5 #include <stdio.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8
9 #include "putty.h"
10
11 char *get_random_data(int len)
12 {
13 char *buf = snewn(len, char);
14 int fd;
15 int ngot, ret;
16
17 fd = open("/dev/random", O_RDONLY);
18 if (fd < 0) {
19 sfree(buf);
20 perror("puttygen: unable to open /dev/random");
21 return NULL;
22 }
23
24 ngot = 0;
25 while (ngot < len) {
26 ret = read(fd, buf+ngot, len-ngot);
27 if (ret < 0) {
28 close(fd);
29 sfree(buf);
30 perror("puttygen: unable to read /dev/random");
31 return NULL;
32 }
33 ngot += ret;
34 }
35
36 close(fd);
37
38 return buf;
39 }