1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 *
6 * %sccs.include.redist.c%
7 */
8
9 #ifndef lint
10 static char copyright[] =
11 "@(#) Copyright (c) 1983, 1993\n\
12 The Regents of the University of California. All rights reserved.\n";
13 #endif /* not lint */
14
15 #ifndef lint
16 static char sccsid[] = "@(#)lpq.c 8.3 (Berkeley) 05/10/95";
17 #endif /* not lint */
18
19 /*
20 * Spool Queue examination program
21 *
22 * lpq [-a] [-l] [-Pprinter] [user...] [job...]
23 *
24 * -a show all non-null queues on the local machine
25 * -l long output
26 * -P used to identify printer as per lpr/lprm
27 */
28
29 #include <sys/param.h>
30
31 #include <syslog.h>
32 #include <dirent.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <ctype.h>
37 #include "lp.h"
38 #include "lp.local.h"
39 #include "pathnames.h"
40
41 int requ[MAXREQUESTS]; /* job number of spool entries */
42 int requests; /* # of spool requests */
43 char *user[MAXUSERS]; /* users to process */
44 int users; /* # of users in user array */
45
46 static int ckqueue __P((char *));
47 void usage __P((void));
48
49 int
main(argc,argv)50 main(argc, argv)
51 register int argc;
52 register char **argv;
53 {
54 extern char *optarg;
55 extern int optind;
56 int ch, aflag, lflag;
57 char *buf, *cp;
58
59 name = *argv;
60 if (gethostname(host, sizeof(host))) {
61 perror("lpq: gethostname");
62 exit(1);
63 }
64 openlog("lpd", 0, LOG_LPR);
65
66 aflag = lflag = 0;
67 while ((ch = getopt(argc, argv, "alP:")) != EOF)
68 switch((char)ch) {
69 case 'a':
70 ++aflag;
71 break;
72 case 'l': /* long output */
73 ++lflag;
74 break;
75 case 'P': /* printer name */
76 printer = optarg;
77 break;
78 case '?':
79 default:
80 usage();
81 }
82
83 if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
84 printer = DEFLP;
85
86 for (argc -= optind, argv += optind; argc; --argc, ++argv)
87 if (isdigit(argv[0][0])) {
88 if (requests >= MAXREQUESTS)
89 fatal("too many requests");
90 requ[requests++] = atoi(*argv);
91 }
92 else {
93 if (users >= MAXUSERS)
94 fatal("too many users");
95 user[users++] = *argv;
96 }
97
98 if (aflag) {
99 while (cgetnext(&buf, printcapdb) > 0) {
100 if (ckqueue(buf) <= 0) {
101 free(buf);
102 continue; /* no jobs */
103 }
104 for (cp = buf; *cp; cp++)
105 if (*cp == '|' || *cp == ':') {
106 *cp = '\0';
107 break;
108 }
109 printer = buf;
110 printf("%s:\n", printer);
111 displayq(lflag);
112 free(buf);
113 printf("\n");
114 }
115 } else
116 displayq(lflag);
117 exit(0);
118 }
119
120 static int
ckqueue(cap)121 ckqueue(cap)
122 char *cap;
123 {
124 register struct dirent *d;
125 DIR *dirp;
126 char *spooldir;
127
128 if (cgetstr(cap, "sd", &spooldir) == -1)
129 spooldir = _PATH_DEFSPOOL;
130 if ((dirp = opendir(spooldir)) == NULL)
131 return (-1);
132 while ((d = readdir(dirp)) != NULL) {
133 if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
134 continue; /* daemon control files only */
135 closedir(dirp);
136 return (1); /* found something */
137 }
138 closedir(dirp);
139 return (0);
140 }
141
142 void
usage()143 usage()
144 {
145 puts("usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]");
146 exit(1);
147 }
148