Advertisement
el3

Untitled

el3
Jun 4th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. static int
  2. read_printer_data()
  3. {
  4. struct pollfd fd[1];
  5.  
  6. /* Open device file for printer gadget. */
  7. fd[0].fd = open(PRINTER_FILE, O_RDWR);
  8. if (fd[0].fd < 0) {
  9. printf("Error %d opening %s\n", fd[0].fd, PRINTER_FILE);
  10. close(fd[0].fd);
  11. return(-1);
  12. }
  13.  
  14. fd[0].events = POLLIN | POLLRDNORM;
  15.  
  16. while (1) {
  17. static char buf[BUF_SIZE];
  18. int bytes_read;
  19. int retval;
  20.  
  21. /* Wait for up to 1 second for data. */
  22. retval = poll(fd, 1, 1000);
  23.  
  24. if (retval && (fd[0].revents & POLLRDNORM)) {
  25.  
  26. /* Read data from printer gadget driver. */
  27. bytes_read = read(fd[0].fd, buf, BUF_SIZE);
  28.  
  29. if (bytes_read < 0) {
  30. printf("Error %d reading from %s\n",
  31. fd[0].fd, PRINTER_FILE);
  32. close(fd[0].fd);
  33. return(-1);
  34. } else if (bytes_read > 0) {
  35. /* Write data to standard OUTPUT (stdout). */
  36. fwrite(buf, 1, bytes_read, stdout);
  37. fflush(stdout);
  38. }
  39.  
  40. }
  41.  
  42. }
  43.  
  44. /* Close the device file. */
  45. close(fd[0].fd);
  46.  
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement