lpq.c revision 1.11
11.11Swiz/*	$NetBSD: lpq.c,v 1.11 2002/07/08 14:05:38 wiz Exp $	*/
21.7Smrg
31.1Scgd/*
41.3Scgd * Copyright (c) 1983, 1993
51.3Scgd *	The Regents of the University of California.  All rights reserved.
61.3Scgd *
71.1Scgd *
81.1Scgd * Redistribution and use in source and binary forms, with or without
91.1Scgd * modification, are permitted provided that the following conditions
101.1Scgd * are met:
111.1Scgd * 1. Redistributions of source code must retain the above copyright
121.1Scgd *    notice, this list of conditions and the following disclaimer.
131.1Scgd * 2. Redistributions in binary form must reproduce the above copyright
141.1Scgd *    notice, this list of conditions and the following disclaimer in the
151.1Scgd *    documentation and/or other materials provided with the distribution.
161.1Scgd * 3. All advertising materials mentioning features or use of this software
171.1Scgd *    must display the following acknowledgement:
181.1Scgd *	This product includes software developed by the University of
191.1Scgd *	California, Berkeley and its contributors.
201.1Scgd * 4. Neither the name of the University nor the names of its contributors
211.1Scgd *    may be used to endorse or promote products derived from this software
221.1Scgd *    without specific prior written permission.
231.1Scgd *
241.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341.1Scgd * SUCH DAMAGE.
351.1Scgd */
361.1Scgd
371.7Smrg#include <sys/cdefs.h>
381.1Scgd#ifndef lint
391.7Smrg__COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
401.7Smrg	The Regents of the University of California.  All rights reserved.\n");
411.7Smrg#if 0
421.6Smrgstatic char sccsid[] = "@(#)lpq.c	8.3 (Berkeley) 5/10/95";
431.7Smrg#else
441.11Swiz__RCSID("$NetBSD: lpq.c,v 1.11 2002/07/08 14:05:38 wiz Exp $");
451.7Smrg#endif
461.1Scgd#endif /* not lint */
471.1Scgd
481.1Scgd/*
491.1Scgd * Spool Queue examination program
501.1Scgd *
511.6Smrg * lpq [-a] [-l] [-Pprinter] [user...] [job...]
521.1Scgd *
531.6Smrg * -a show all non-null queues on the local machine
541.1Scgd * -l long output
551.1Scgd * -P used to identify printer as per lpr/lprm
561.1Scgd */
571.1Scgd
581.3Scgd#include <sys/param.h>
591.3Scgd
601.3Scgd#include <syslog.h>
611.3Scgd#include <dirent.h>
621.3Scgd#include <unistd.h>
631.3Scgd#include <stdlib.h>
641.3Scgd#include <stdio.h>
651.3Scgd#include <ctype.h>
661.7Smrg#include <err.h>
671.7Smrg
681.1Scgd#include "lp.h"
691.3Scgd#include "lp.local.h"
701.6Smrg#include "pathnames.h"
711.1Scgd
721.3Scgdint	 requ[MAXREQUESTS];	/* job number of spool entries */
731.3Scgdint	 requests;		/* # of spool requests */
741.1Scgdchar	*user[MAXUSERS];	/* users to process */
751.3Scgdint	 users;			/* # of users in user array */
761.4Shpeyerluid_t	uid, euid;
771.4Shpeyerl
781.6Smrgstatic int ckqueue __P((char *));
791.7Smrgstatic void usage __P((void));
801.7Smrgint main __P((int, char *[]));
811.1Scgd
821.3Scgdint
831.1Scgdmain(argc, argv)
841.7Smrg	int	argc;
851.7Smrg	char	**argv;
861.1Scgd{
871.6Smrg	int	ch, aflag, lflag;
881.6Smrg	char	*buf, *cp;
891.1Scgd
901.4Shpeyerl	euid = geteuid();
911.4Shpeyerl	uid = getuid();
921.4Shpeyerl	seteuid(uid);
931.1Scgd	name = *argv;
941.7Smrg	if (gethostname(host, sizeof(host)))
951.7Smrg		err(1, "lpq: gethostname");
961.8Smrg	host[sizeof(host) - 1] = '\0';
971.1Scgd	openlog("lpd", 0, LOG_LPR);
981.1Scgd
991.6Smrg	aflag = lflag = 0;
1001.9Smrg	while ((ch = getopt(argc, argv, "alP:w:")) != -1)
1011.1Scgd		switch((char)ch) {
1021.6Smrg		case 'a':
1031.6Smrg			++aflag;
1041.6Smrg			break;
1051.1Scgd		case 'l':			/* long output */
1061.1Scgd			++lflag;
1071.1Scgd			break;
1081.1Scgd		case 'P':		/* printer name */
1091.1Scgd			printer = optarg;
1101.9Smrg			break;
1111.9Smrg		case 'w':
1121.9Smrg			wait_time = atoi(optarg);
1131.9Smrg			if (wait_time < 0)
1141.9Smrg				errx(1, "wait time must be postive: %s",
1151.9Smrg				    optarg);
1161.9Smrg			if (wait_time < 30)
1171.9Smrg			    warnx("warning: wait time less than 30 seconds");
1181.1Scgd			break;
1191.1Scgd		case '?':
1201.1Scgd		default:
1211.1Scgd			usage();
1221.1Scgd		}
1231.1Scgd
1241.6Smrg	if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
1251.1Scgd		printer = DEFLP;
1261.1Scgd
1271.1Scgd	for (argc -= optind, argv += optind; argc; --argc, ++argv)
1281.1Scgd		if (isdigit(argv[0][0])) {
1291.1Scgd			if (requests >= MAXREQUESTS)
1301.1Scgd				fatal("too many requests");
1311.1Scgd			requ[requests++] = atoi(*argv);
1321.1Scgd		}
1331.1Scgd		else {
1341.1Scgd			if (users >= MAXUSERS)
1351.1Scgd				fatal("too many users");
1361.1Scgd			user[users++] = *argv;
1371.1Scgd		}
1381.1Scgd
1391.6Smrg	if (aflag) {
1401.6Smrg		while (cgetnext(&buf, printcapdb) > 0) {
1411.6Smrg			if (ckqueue(buf) <= 0) {
1421.6Smrg				free(buf);
1431.6Smrg				continue;	/* no jobs */
1441.6Smrg			}
1451.6Smrg			for (cp = buf; *cp; cp++)
1461.6Smrg				if (*cp == '|' || *cp == ':') {
1471.6Smrg					*cp = '\0';
1481.6Smrg					break;
1491.6Smrg				}
1501.6Smrg			printer = buf;
1511.6Smrg			printf("%s:\n", printer);
1521.6Smrg			displayq(lflag);
1531.6Smrg			free(buf);
1541.6Smrg			printf("\n");
1551.6Smrg		}
1561.6Smrg	} else
1571.6Smrg		displayq(lflag);
1581.1Scgd	exit(0);
1591.1Scgd}
1601.1Scgd
1611.6Smrgstatic int
1621.6Smrgckqueue(cap)
1631.6Smrg	char *cap;
1641.6Smrg{
1651.7Smrg	struct dirent *d;
1661.6Smrg	DIR *dirp;
1671.6Smrg	char *spooldir;
1681.6Smrg
1691.6Smrg	if (cgetstr(cap, "sd", &spooldir) == -1)
1701.6Smrg		spooldir = _PATH_DEFSPOOL;
1711.6Smrg	if ((dirp = opendir(spooldir)) == NULL)
1721.6Smrg		return (-1);
1731.6Smrg	while ((d = readdir(dirp)) != NULL) {
1741.6Smrg		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
1751.6Smrg			continue;	/* daemon control files only */
1761.6Smrg		closedir(dirp);
1771.6Smrg		return (1);		/* found something */
1781.6Smrg	}
1791.6Smrg	closedir(dirp);
1801.6Smrg	return (0);
1811.6Smrg}
1821.6Smrg
1831.7Smrgstatic void
1841.1Scgdusage()
1851.1Scgd{
1861.11Swiz	puts("usage: lpq [-a] [-l] [-Pprinter] [-w maxwait] [user ...] [job ...]");
1871.1Scgd	exit(1);
1881.1Scgd}
189