ptg10805159
Section 21.5 Source Code 825
385 req.size=ntohl(req.size);
386 req.flags=ntohl(req.flags);
387 /*
388 * Create the data file.
389 */
390 jobid=get_newjobno();
391 sprintf(name, "%s/%s/%d", SPOOLDIR, DATADIR, jobid);
392 fd=creat(name, FILEPERM);
393 if (fd < 0) {
394 res.jobid=0;
395 res.retcode=htonl(errno);
396 log_msg("client_thread: can’t create %s: %s", name,
397 strerror(res.retcode));
398 strncpy(res.msg, strerror(res.retcode), MSGLEN_MAX);
399 writen(sockfd, &res, sizeof(struct printresp));
400 pthread_exit((void *)1);
401 }
402 /*
403 * Read the file and store it in the spool directory.
404 * Try to figure out if the file is a PostScript file
405 * or a plain text file.
406 */
407 first=1;
408 while ((nr = tread(sockfd, buf, IOBUFSZ, 20)) > 0) {
409 if (first) {
410 first=0;
411 if (strncmp(buf, "%!PS", 4) != 0)
412 req.flags |= PR_TEXT;
413 }
[385 – 401] We convert the integer fields in the request header to host byte order and call
get_newjobnoto reserve the next job ID for this print request. Wecreate
the job data file, named/var/spool/printer/data/jobid,wherejobidis
the request’s job ID.We use permissions that prevent others from being able
read the files (FILEPERMis defined asS_IRUSR|S_IWUSRinprint.h). If
we can’t create the file, we log an error message, send a failureresponse back
to the client, and terminate the thread by callingpthread_exit.
[402 – 413] We read the file contents from the client, with the intention of writing the
contents out to our private copy of the data file. But before we write
anything, we need to check if this is a PostScript file the first time through
the loop. If the file doesn’t begin with the pattern%!PS, we can assume that
the file is plaintext, so we set thePR_TEXTflag in the request header in this
case. (Recall that the client can also set this flag if the-tflag is included
when theprintcommand is executed.) Although PostScript programs are
not required to start with the pattern %!PS,the document formatting
guidelines (Adobe Systems[ 1999 ])strongly recommends that they do.