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