Guest User

Untitled

a guest
Oct 16th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <sys/time.h>
  9. #include <sys/resource.h>
  10.  
  11. int
  12. main ()
  13. {
  14. char x;
  15. int ret = 0;
  16. long fails = 0;
  17. int fd = 0;
  18. int flags = 0;
  19. unsigned long dataprocessed = 0;
  20.  
  21. fd = fileno (stdin);
  22. flags = fcntl (fd, F_GETFL, 0);
  23. flags |= O_NONBLOCK;
  24. fcntl (fd, F_SETFL, flags);
  25. while (1)
  26. {
  27. ret = fread (&x, sizeof (char), 1, stdin);
  28. if (ret <= 0 && errno == EAGAIN)
  29. {
  30. /* No data to recieve */
  31. printf ("Error read file: %s\n", strerror (errno));
  32.  
  33. continue;
  34. }
  35.  
  36. if (errno == EWOULDBLOCK && ret == 0)
  37. {
  38. //printf("BLOCKING \n\r");
  39. fails++;
  40. }
  41. else
  42. {
  43. if (ret > 0)
  44. {
  45. /* data flows */
  46. fwrite (&x, sizeof (x), 1, stdout);
  47. fails = 0;
  48. //printf("ret: %d\n\r",sizeof(x));
  49. dataprocessed++;
  50. }
  51. }
  52.  
  53. if (fails > 500000)
  54. {
  55. printf ("%d FAILURES DETECTED %ld bytes Processed\n\r", fails,
  56. dataprocessed);
  57. exit (0);
  58. }
  59. }
  60. return 0;
  61. }
Add Comment
Please, Sign In to add comment