lpq.c revision 1.15
1/*	$NetBSD: lpq.c,v 1.15 2004/10/30 08:44:26 dsl 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. 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#include <sys/cdefs.h>
34#ifndef lint
35__COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
36	The Regents of the University of California.  All rights reserved.\n");
37#if 0
38static char sccsid[] = "@(#)lpq.c	8.3 (Berkeley) 5/10/95";
39#else
40__RCSID("$NetBSD: lpq.c,v 1.15 2004/10/30 08:44:26 dsl Exp $");
41#endif
42#endif /* not lint */
43
44/*
45 * Spool Queue examination program
46 *
47 * lpq [-a] [-l] [-Pprinter] [user...] [job...]
48 *
49 * -a show all non-null queues on the local machine
50 * -l long output
51 * -P used to identify printer as per lpr/lprm
52 */
53
54#include <sys/param.h>
55
56#include <syslog.h>
57#include <dirent.h>
58#include <unistd.h>
59#include <stdlib.h>
60#include <stdio.h>
61#include <ctype.h>
62#include <err.h>
63
64#include "lp.h"
65#include "lp.local.h"
66#include "pathnames.h"
67
68int	 requ[MAXREQUESTS];	/* job number of spool entries */
69int	 requests;		/* # of spool requests */
70char	*user[MAXUSERS];	/* users to process */
71int	 users;			/* # of users in user array */
72uid_t	uid, euid;
73
74static int ckqueue(char *);
75static void usage(void);
76int main(int, char *[]);
77
78int
79main(int argc, char *argv[])
80{
81	int	ch, aflag, lflag;
82	char	*buf, *cp;
83
84	euid = geteuid();
85	uid = getuid();
86	seteuid(uid);
87	name = *argv;
88	if (gethostname(host, sizeof(host)))
89		err(1, "lpq: gethostname");
90	host[sizeof(host) - 1] = '\0';
91	openlog("lpd", 0, LOG_LPR);
92
93	aflag = lflag = 0;
94	while ((ch = getopt(argc, argv, "alP:w:")) != -1)
95		switch((char)ch) {
96		case 'a':
97			++aflag;
98			break;
99		case 'l':			/* long output */
100			++lflag;
101			break;
102		case 'P':		/* printer name */
103			printer = optarg;
104			break;
105		case 'w':
106			wait_time = atoi(optarg);
107			if (wait_time < 0)
108				errx(1, "wait time must be postive: %s",
109				    optarg);
110			if (wait_time < 30)
111			    warnx("warning: wait time less than 30 seconds");
112			break;
113		case '?':
114		default:
115			usage();
116		}
117
118	if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
119		printer = DEFLP;
120
121	for (argc -= optind, argv += optind; argc; --argc, ++argv)
122		if (isdigit((unsigned char)argv[0][0])) {
123			if (requests >= MAXREQUESTS)
124				fatal("too many requests");
125			requ[requests++] = atoi(*argv);
126		}
127		else {
128			if (users >= MAXUSERS)
129				fatal("too many users");
130			user[users++] = *argv;
131		}
132
133	if (aflag) {
134		while (cgetnext(&buf, printcapdb) > 0) {
135			if (ckqueue(buf) <= 0) {
136				free(buf);
137				continue;	/* no jobs */
138			}
139			for (cp = buf; *cp; cp++)
140				if (*cp == '|' || *cp == ':') {
141					*cp = '\0';
142					break;
143				}
144			printer = buf;
145			printf("%s:\n", printer);
146			displayq(lflag);
147			free(buf);
148			printf("\n");
149		}
150	} else
151		displayq(lflag);
152	exit(0);
153}
154
155static int
156ckqueue(char *cap)
157{
158	struct dirent *d;
159	DIR *dirp;
160	char *spooldir;
161
162	if (cgetstr(cap, "sd", &spooldir) == -1)
163		spooldir = _PATH_DEFSPOOL;
164	if ((dirp = opendir(spooldir)) == NULL) {
165		if (spooldir != _PATH_DEFSPOOL)
166			free(spooldir);
167		return (-1);
168	}
169	while ((d = readdir(dirp)) != NULL) {
170		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
171			continue;	/* daemon control files only */
172		closedir(dirp);
173		if (spooldir != _PATH_DEFSPOOL)
174			free(spooldir);
175		return (1);		/* found something */
176	}
177	closedir(dirp);
178	if (spooldir != _PATH_DEFSPOOL)
179		free(spooldir);
180	return (0);
181}
182
183static void
184usage(void)
185{
186	puts("usage: lpq [-a] [-l] [-Pprinter] [-w maxwait] [user ...] [job ...]");
187	exit(1);
188}
189