ptg10805159
Section 21.5 Source Code 807
The command used to submit a print job is shown next. The C source file isprint.c.
1/*
2*The client command for printing documents. Opens the file
3*and sends it to the printer spooling daemon. Usage:
4 * print [-t] filename
5*/
6#include "apue.h"
7#include "print.h"
8#include <fcntl.h>
9#include <pwd.h>
10 /*
11 * Needed for logging funtions.
12 */
13 int log_to_stderr = 1;
14 void submit_file(int, int, const char *, size_t, int);
15 int
16 main(int argc, char *argv[])
17 {
18 int fd, sfd, err, text, c;
19 struct stat sbuf;
20 char *host;
21 struct addrinfo *ailist, *aip;
22 err=0;
23 text=0;
24 while ((c = getopt(argc, argv, "t")) != -1) {
25 switch (c) {
26 case ’t’:
27 text=1;
28 break;
29 case ’?’:
30 err =1;
31 break;
32 }
33 }
[1 – 14] We need to define an integer calledlog_to_stderrto be able to use the log
functions in our library.Ifthis integer is set to a nonzerovalue, error messages
will be sent to the standarderror stream instead of to a log file. Although we
don’t use any logging functions in print.c, we do link util.o with
print.oto build the executableprint command, andutil.c contains
functions for both user commands and daemons.
[15 – 33] We support one option,-t,toforce the file to be printed as text (instead of as a
PostScript program, for example).We use thegetoptfunction (introduced in
Section 17.6) to process the command options.