ptg10805159
842 Communicating with a Network Printer Chapter 21
923 }
924 }
925 hp =(struct ipp_hdr *)cp;
926 i =ntohs(hp->status);
927 jobid=ntohl(hp->request_id);
928 if (jobid != jp->jobid) {
929 /*
930 * Different jobs. Ignore it.
931 */
932 log_msg("jobid %d status code %d", jobid, i);
933 break;
934 }
935 if (STATCLASS_OK(i))
936 success=1;
937 break;
938 }
939 }
940 out:
941 free(bp);
942 if (nr < 0) {
943 log_msg("jobid %d: error reading printer response: %s",
944 jobid, strerror(errno));
945 }
946 return(success);
947 }
[923 – 927] We get the status and job ID from the IPP header in the message. Both are
stored as integers in network byte order, so weneed to convert them to the
host byte order by callingntohsandntohl,respectively.
[928 – 939] If the job IDs don’t match, then this is not our response, so we log a message
and break out of the outerwhileloop. If the IPP status indicates success,
then we save the return value and break out of the loop.
[940 – 947] Before we return, we free the buffer we used to hold the response message.
We return 1 if the print request was successful and 0 if it failed.
This concludes our look at the extended example in this chapter.The programs in
this chapter weretested with a Xerox Phaser 8560 network-attached PostScript printer.
Unfortunately,this printer doesn’t disable its autosense featurewhen we set the
document format totext/plain.This led us to use a hack to trick the printer so that
it wouldn’t autosense the document format when we wanted to treat a document as
plaintext. An alternative is to print the source to a PostScript program using a utility
such asa2ps( 1 ),which encapsulates the PostScript program beforeprinting.