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