| 23 | */ |
| 24 | |
| 25 | int /* O - 0 on success, -1 on error */ |
| 26 | backendDrainOutput(int print_fd, /* I - Print file descriptor */ |
| 27 | int device_fd) /* I - Device file descriptor */ |
| 28 | { |
| 29 | int nfds; /* Maximum file descriptor value + 1 */ |
| 30 | fd_set input; /* Input set for reading */ |
| 31 | ssize_t print_bytes, /* Print bytes read */ |
| 32 | bytes; /* Bytes written */ |
| 33 | char print_buffer[8192], /* Print data buffer */ |
| 34 | *print_ptr; /* Pointer into print data buffer */ |
| 35 | struct timeval timeout; /* Timeout for read... */ |
| 36 | |
| 37 | |
| 38 | fprintf(stderr, "DEBUG: backendDrainOutput(print_fd=%d, device_fd=%d)\n", |
| 39 | print_fd, device_fd); |
| 40 | |
| 41 | /* |
| 42 | * Figure out the maximum file descriptor value to use with select()... |
| 43 | */ |
| 44 | |
| 45 | nfds = (print_fd > device_fd ? print_fd : device_fd) + 1; |
| 46 | |
| 47 | /* |
| 48 | * Now loop until we are out of data from print_fd... |
| 49 | */ |
| 50 | |
| 51 | for (;;) |
| 52 | { |
| 53 | /* |
| 54 | * Use select() to determine whether we have data to copy around... |
| 55 | */ |
| 56 | |
| 57 | FD_ZERO(&input); |
| 58 | FD_SET(print_fd, &input); |
| 59 | |
| 60 | timeout.tv_sec = 0; |
| 61 | timeout.tv_usec = 0; |
| 62 | |
| 63 | if (select(nfds, &input, NULL, NULL, &timeout) < 0) |
| 64 | return (-1); |
| 65 | |
| 66 | if (!FD_ISSET(print_fd, &input)) |
| 67 | return (0); |
| 68 | |
| 69 | if ((print_bytes = read(print_fd, print_buffer, |
| 70 | sizeof(print_buffer))) < 0) |
| 71 | { |
| 72 | /* |
| 73 | * Read error - bail if we don't see EAGAIN or EINTR... |
| 74 | */ |
| 75 | |
| 76 | if (errno != EAGAIN && errno != EINTR) |
| 77 | { |
| 78 | fprintf(stderr, "DEBUG: Read failed: %s\n", strerror(errno)); |
| 79 | _cupsLangPrintFilter(stderr, "ERROR", _("Unable to read print data.")); |
| 80 | return (-1); |
| 81 | } |
| 82 |
no test coverage detected