xref: /freebsd/usr.sbin/lpr/lpq/lpq.c (revision 380a989b3223d455375b4fae70fd0b9bdd43bafb)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef lint
36 static const char copyright[] =
37 "@(#) Copyright (c) 1983, 1993\n\
38 	The Regents of the University of California.  All rights reserved.\n";
39 #endif /* not lint */
40 
41 #ifndef lint
42 /*
43 static char sccsid[] = "@(#)lpq.c	8.3 (Berkeley) 5/10/95";
44 */
45 static const char rcsid[] =
46 	"$Id: lpq.c,v 1.5 1997/12/02 20:46:04 wollman Exp $";
47 #endif /* not lint */
48 
49 /*
50  * Spool Queue examination program
51  *
52  * lpq [-a] [-l] [-Pprinter] [user...] [job...]
53  *
54  * -a show all non-null queues on the local machine
55  * -l long output
56  * -P used to identify printer as per lpr/lprm
57  */
58 
59 #include <sys/param.h>
60 
61 #include <ctype.h>
62 #include <dirent.h>
63 #include <err.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <syslog.h>
67 #include <unistd.h>
68 
69 #include "lp.h"
70 #include "lp.local.h"
71 #include "pathnames.h"
72 
73 int	 requ[MAXREQUESTS];	/* job number of spool entries */
74 int	 requests;		/* # of spool requests */
75 char	*user[MAXUSERS];	/* users to process */
76 int	 users;			/* # of users in user array */
77 
78 uid_t	uid, euid;
79 
80 static int ckqueue __P((const struct printer *));
81 static void usage __P((void));
82 int main __P((int, char **));
83 
84 int
85 main(argc, argv)
86 	int	argc;
87 	char	**argv;
88 {
89 	int ch, aflag, lflag;
90 	char *printer;
91 	struct printer myprinter, *pp = &myprinter;
92 
93 	printer = NULL;
94 	euid = geteuid();
95 	uid = getuid();
96 	seteuid(uid);
97 	name = *argv;
98 	if (gethostname(host, sizeof(host)))
99 		err(1, "gethostname");
100 	openlog("lpd", 0, LOG_LPR);
101 
102 	aflag = lflag = 0;
103 	while ((ch = getopt(argc, argv, "alP:")) != -1)
104 		switch((char)ch) {
105 		case 'a':
106 			++aflag;
107 			break;
108 		case 'l':			/* long output */
109 			++lflag;
110 			break;
111 		case 'P':		/* printer name */
112 			printer = optarg;
113 			break;
114 		case '?':
115 		default:
116 			usage();
117 		}
118 
119 	if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
120 		printer = DEFLP;
121 
122 	for (argc -= optind, argv += optind; argc; --argc, ++argv)
123 		if (isdigit(argv[0][0])) {
124 			if (requests >= MAXREQUESTS)
125 				fatal(0, "too many requests");
126 			requ[requests++] = atoi(*argv);
127 		}
128 		else {
129 			if (users >= MAXUSERS)
130 				fatal(0, "too many users");
131 			user[users++] = *argv;
132 		}
133 
134 	if (aflag) {
135 		int more, status;
136 
137 		more = firstprinter(pp, &status);
138 		if (status)
139 			goto looperr;
140 		while (more) {
141 			if (ckqueue(pp) > 0) {
142 				printf("%s:\n", pp->printer);
143 				displayq(pp, lflag);
144 				printf("\n");
145 			}
146 			do {
147 				more = nextprinter(pp, &status);
148 looperr:
149 				switch (status) {
150 				case PCAPERR_TCOPEN:
151 					printf("warning: %s: unresolved "
152 					       "tc= reference(s) ",
153 					       pp->printer);
154 				case PCAPERR_SUCCESS:
155 					break;
156 				default:
157 					fatal(pp, pcaperr(status));
158 				}
159 			} while (more && status);
160 		}
161 	} else {
162 		int status;
163 
164 		init_printer(pp);
165 		status = getprintcap(printer, pp);
166 		if (status < 0)
167 			fatal(pp, pcaperr(status));
168 
169 		displayq(pp, lflag);
170 	}
171 	exit(0);
172 }
173 
174 static int
175 ckqueue(pp)
176 	const struct printer *pp;
177 {
178 	register struct dirent *d;
179 	DIR *dirp;
180 	char *spooldir;
181 
182 	spooldir = pp->spool_dir;
183 	if ((dirp = opendir(spooldir)) == NULL)
184 		return (-1);
185 	while ((d = readdir(dirp)) != NULL) {
186 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
187 			continue;	/* daemon control files only */
188 		closedir(dirp);
189 		return (1);		/* found something */
190 	}
191 	closedir(dirp);
192 	return (0);
193 }
194 
195 static void
196 usage()
197 {
198 	fprintf(stderr,
199 	"usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]\n");
200 	exit(1);
201 }
202