- tbytes = 0;
- while ((nbytes = read(fp, buffer, sizeof(buffer))) > 0)
- {
- /*
- * Write the print data to the printer...
- */
-
- tbytes += nbytes;
- bufptr = buffer;
-
- while (nbytes > 0)
- {
- /*
- * See if we are ready to read or write...
- */
-
- do
- {
- FD_ZERO(&input);
- if (usebc)
- FD_SET(fd, &input);
-
- FD_ZERO(&output);
- FD_SET(fd, &output);
- }
- while (select(fd + 1, &input, &output, NULL, NULL) < 0);
-
- if (FD_ISSET(fd, &input))
- {
- /*
- * Read backchannel data...
- */
-
- if ((rbytes = read(fd, backbuf, sizeof(backbuf))) > 0)
- {
- fprintf(stderr, "DEBUG: Received %d bytes of back-channel data!\n",
- rbytes);
- cupsBackChannelWrite(backbuf, rbytes, 1.0);
- }
- }
-
- if (FD_ISSET(fd, &output))
- {
- /*
- * Write print data...
- */
-
- if ((wbytes = write(fd, bufptr, nbytes)) < 0)
- if (errno == ENOTTY)
- wbytes = write(fd, bufptr, nbytes);
-
- if (wbytes < 0)
- {
- /*
- * Check for retryable errors...
- */
-
- if (errno == ENOSPC)
- {
- paperout = 1;
- fputs("ERROR: Out of paper!\n", stderr);
- fputs("STATUS: +media-tray-empty-error\n", stderr);
- }
- else if (errno != EAGAIN && errno != EINTR)
- {
- perror("ERROR: Unable to send print file to printer");
- break;
- }
- }
- else
- {
- /*
- * Update count and pointer...
- */
-
- if (paperout)
- {
- fputs("STATUS: -media-tray-empty-error\n", stderr);
- paperout = 0;
- }
-
- nbytes -= wbytes;
- bufptr += wbytes;
- }
- }
- }
-
- if (wbytes < 0)
- break;