forked from larryli/PuTTY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uxgen.c
46 lines (37 loc) · 826 Bytes
/
uxgen.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
40
41
42
43
44
45
46
/*
* uxgen.c: Unix implementation of get_heavy_noise() from cmdgen.c.
*/
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "putty.h"
char *get_random_data(int len, const char *device)
{
char *buf = snewn(len, char);
int fd;
int ngot, ret;
if (!device)
device = "/dev/random";
fd = open(device, O_RDONLY);
if (fd < 0) {
sfree(buf);
fprintf(stderr, "puttygen: %s: open: %s\n",
device, strerror(errno));
return NULL;
}
ngot = 0;
while (ngot < len) {
ret = read(fd, buf+ngot, len-ngot);
if (ret < 0) {
close(fd);
sfree(buf);
fprintf(stderr, "puttygen: %s: read: %s\n",
device, strerror(errno));
return NULL;
}
ngot += ret;
}
close(fd);
return buf;
}