urandom_read.c 419 B

12345678910111213141516171819202122232425262728
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <stdlib.h>
  7. #define BUF_SIZE 256
  8. int main(int argc, char *argv[])
  9. {
  10. int fd = open("/dev/urandom", O_RDONLY);
  11. int i;
  12. char buf[BUF_SIZE];
  13. int count = 4;
  14. if (fd < 0)
  15. return 1;
  16. if (argc == 2)
  17. count = atoi(argv[1]);
  18. for (i = 0; i < count; ++i)
  19. read(fd, buf, BUF_SIZE);
  20. close(fd);
  21. return 0;
  22. }