Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 21.5 Source Code 819


193 /*
194 * Initialize printer information from configuration file.
195 *
196 * LOCKING: none.
197 */
198 void
199 init_printer(void)
200 {
201 printer=get_printaddr();
202 if (printer == NULL)
203 exit(1); /* message already logged */
204 printer_name=printer->ai_canonname;
205 if (printer_name == NULL)
206 printer_name="printer";
207 log_msg("printer is %s", printer_name);
208 }
209 /*
210 * Update the job ID file with the next job number.
211 * Doesn’t handle wrap-around of job number.
212 *
213 * LOCKING: none.
214 */
215 void
216 update_jobno(void)
217 {
218 char buf[32];
219 if (lseek(jobfd, 0, SEEK_SET) == -1)
220 log_sys("can’t seek in job file");
221 sprintf(buf, "%d", nextjob);
222 if (write(jobfd, buf, strlen(buf)) < 0)
223 log_sys("can’t update job file");
224 }

[193 – 208] Theinit_printerfunction is used to set the printer name and address.
We get the printer address by callingget_printaddr(fromutil.c). If
this fails, we exit. Theget_printaddrfunction logs its own message
when it is unable to find the printer’s address. Ifaprinter address is found,
however,weset the printer name to the ai_canonname field in the
addrinfostructure. If this field is null, we set the printer name to a default
value ofprinter.Note that we log the name of the printer we areusing to
aid administrators in diagnosing problems with the spooling system.
[209 – 224] Theupdate_jobnofunction is used to write the next job number to the job
file,/var/spool/printer/jobno.Weseek to the beginning of the file,
convert the integer job number into a string, and write it to the file. On error,
we log a message and exit. The job number increases monotonically;
handling wrap-around is left as an exercise (see Exercise 21.9).
Free download pdf