uxgen.c 616 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * uxgen.c: Unix implementation of get_heavy_noise() from cmdgen.c.
  3. */
  4. #include <stdio.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include "putty.h"
  8. char *get_random_data(int len)
  9. {
  10. char *buf = snewn(len, char);
  11. int fd;
  12. int ngot, ret;
  13. fd = open("/dev/random", O_RDONLY);
  14. if (fd < 0) {
  15. sfree(buf);
  16. perror("puttygen: unable to open /dev/random");
  17. return NULL;
  18. }
  19. ngot = 0;
  20. while (ngot < len) {
  21. ret = read(fd, buf+ngot, len-ngot);
  22. if (ret < 0) {
  23. close(fd);
  24. perror("puttygen: unable to read /dev/random");
  25. return NULL;
  26. }
  27. ngot += ret;
  28. }
  29. return buf;
  30. }