Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

838 Communicating with a Network Printer Chapter 21


786 {
787 ssize_t nr;
788 char *bp = *bpp;
789 int bsz=*bszp;
790 if (off >= bsz) {
791 bsz += IOBUFSZ;
792 if ((bp = realloc(*bpp, bsz)) == NULL)
793 log_sys("readmore: can’t allocate bigger read buffer");
794 *bszp=bsz;
795 *bpp=bp;
796 }
797 if ((nr = tread(sockfd, &bp[off], bsz-off, 1)) > 0)
798 return(off+nr);
799 else
800 return(-1);
801 }
802 /*
803 * Read and parse the response from the printer. Return 1
804 * if the request was successful, and 0 otherwise.
805 *
806 * LOCKING: none.
807 */
808 int
809 printer_status(int sfd, struct job *jp)
810 {
811 int i, success, code, len, found, bufsz, datsz;
812 int32_t jobid;
813 ssize_t nr;
814 char *bp, *cp, *statcode, *reason, *contentlen;
815 struct ipp_hdr *hp;
816 /*
817 * Read the HTTP header followed by the IPP response header.
818 * They can be returned in multiple read attempts. Use the
819 * Content-Length specifier to determine how much to read.
820 */

[786 – 801] If we’re at the end of the buffer, we reallocate a bigger buffer and return the
new starting address and size through thebpp andbszp parameters,
respectively.Weread as much as the buffer will hold, starting at the end of
the data already in the buffer,and return the new end-of-data offset in the
buffer.Ifthereadfails or the timeout expires, we return−1.
[802 – 820] Theprinter_statusfunction reads the printer ’s response to a print-job
request. Wedon’t know how the printer will respond; it might send a
response in multiple messages, send the complete response in one message,
or include intermediate acknowledgements, such as HTTP100 Continue
messages. Weneed to handle all these possibilities.
Free download pdf