Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 21.5 Source Code 839


821 success=0;
822 bufsz=IOBUFSZ;
823 if ((bp = malloc(IOBUFSZ)) == NULL)
824 log_sys("printer_status: can’t allocate read buffer");
825 while ((nr = tread(sfd, bp, bufsz, 5)) > 0) {
826 /*
827 * Find the status. Response starts with "HTTP/x.y"
828 * so we can skip the first 8 characters.
829 */
830 cp = bp + 8;
831 datsz=nr;
832 while (isspace((int)*cp))
833 cp++;
834 statcode=cp;
835 while (isdigit((int)*cp))
836 cp++;
837 if (cp == statcode) { /* Bad format; log it and move on */
838 log_msg(bp);
839 } else {
840 *cp++=’\0’;
841 reason =cp;
842 while (*cp != ’\r’ && *cp != ’\n’)
843 cp++;
844 *cp =’\0’;
845 code=atoi(statcode);
846 if (HTTP_INFO(code))
847 continue;
848 if (!HTTP_SUCCESS(code)) { /* probable error: log it */
849 bp[datsz]=’\0’;
850 log_msg("error: %s", reason);
851 break;
852 }

[821 – 838] We allocate a buffer and read from the printer,expecting a response to be
available within about 5 seconds. We skip theHTTP/1.1string and any
white space that starts the message. The numeric status code should follow.
If it doesn’t, we log the contents of the message.
[839 – 844] If we have found a numeric status code in the response, we convert the first
nondigit character following the status code to a null byte (this character
should be some form of white space). The reason string (a text message)
should follow.Wesearch for the terminating carriage return or line feed,
also terminating the text string with a null byte.
[845 – 852] We call theatoifunction to convert the status code string into an integer.If
this is an informational message only, we ignore it and continue the loop to
read more. Weexpect to see either a success message or an error message. If
we get an error message, we log the error and break out of the loop.
Free download pdf