lpq.c revision 1.9
11.9Smrg/*	$NetBSD: lpq.c,v 1.9 1999/12/07 14:54:47 mrg 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.9Smrg__RCSID("$NetBSD: lpq.c,v 1.9 1999/12/07 14:54:47 mrg 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.1Scgd	extern char	*optarg;
881.1Scgd	extern int	optind;
891.6Smrg	int	ch, aflag, lflag;
901.6Smrg	char	*buf, *cp;
911.1Scgd
921.4Shpeyerl	euid = geteuid();
931.4Shpeyerl	uid = getuid();
941.4Shpeyerl	seteuid(uid);
951.1Scgd	name = *argv;
961.7Smrg	if (gethostname(host, sizeof(host)))
971.7Smrg		err(1, "lpq: gethostname");
981.8Smrg	host[sizeof(host) - 1] = '\0';
991.1Scgd	openlog("lpd", 0, LOG_LPR);
1001.1Scgd
1011.6Smrg	aflag = lflag = 0;
1021.9Smrg	while ((ch = getopt(argc, argv, "alP:w:")) != -1)
1031.1Scgd		switch((char)ch) {
1041.6Smrg		case 'a':
1051.6Smrg			++aflag;
1061.6Smrg			break;
1071.1Scgd		case 'l':			/* long output */
1081.1Scgd			++lflag;
1091.1Scgd			break;
1101.1Scgd		case 'P':		/* printer name */
1111.1Scgd			printer = optarg;
1121.9Smrg			break;
1131.9Smrg		case 'w':
1141.9Smrg			wait_time = atoi(optarg);
1151.9Smrg			if (wait_time < 0)
1161.9Smrg				errx(1, "wait time must be postive: %s",
1171.9Smrg				    optarg);
1181.9Smrg			if (wait_time < 30)
1191.9Smrg			    warnx("warning: wait time less than 30 seconds");
1201.1Scgd			break;
1211.1Scgd		case '?':
1221.1Scgd		default:
1231.1Scgd			usage();
1241.1Scgd		}
1251.1Scgd
1261.6Smrg	if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
1271.1Scgd		printer = DEFLP;
1281.1Scgd
1291.1Scgd	for (argc -= optind, argv += optind; argc; --argc, ++argv)
1301.1Scgd		if (isdigit(argv[0][0])) {
1311.1Scgd			if (requests >= MAXREQUESTS)
1321.1Scgd				fatal("too many requests");
1331.1Scgd			requ[requests++] = atoi(*argv);
1341.1Scgd		}
1351.1Scgd		else {
1361.1Scgd			if (users >= MAXUSERS)
1371.1Scgd				fatal("too many users");
1381.1Scgd			user[users++] = *argv;
1391.1Scgd		}
1401.1Scgd
1411.6Smrg	if (aflag) {
1421.6Smrg		while (cgetnext(&buf, printcapdb) > 0) {
1431.6Smrg			if (ckqueue(buf) <= 0) {
1441.6Smrg				free(buf);
1451.6Smrg				continue;	/* no jobs */
1461.6Smrg			}
1471.6Smrg			for (cp = buf; *cp; cp++)
1481.6Smrg				if (*cp == '|' || *cp == ':') {
1491.6Smrg					*cp = '\0';
1501.6Smrg					break;
1511.6Smrg				}
1521.6Smrg			printer = buf;
1531.6Smrg			printf("%s:\n", printer);
1541.6Smrg			displayq(lflag);
1551.6Smrg			free(buf);
1561.6Smrg			printf("\n");
1571.6Smrg		}
1581.6Smrg	} else
1591.6Smrg		displayq(lflag);
1601.1Scgd	exit(0);
1611.1Scgd}
1621.1Scgd
1631.6Smrgstatic int
1641.6Smrgckqueue(cap)
1651.6Smrg	char *cap;
1661.6Smrg{
1671.7Smrg	struct dirent *d;
1681.6Smrg	DIR *dirp;
1691.6Smrg	char *spooldir;
1701.6Smrg
1711.6Smrg	if (cgetstr(cap, "sd", &spooldir) == -1)
1721.6Smrg		spooldir = _PATH_DEFSPOOL;
1731.6Smrg	if ((dirp = opendir(spooldir)) == NULL)
1741.6Smrg		return (-1);
1751.6Smrg	while ((d = readdir(dirp)) != NULL) {
1761.6Smrg		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
1771.6Smrg			continue;	/* daemon control files only */
1781.6Smrg		closedir(dirp);
1791.6Smrg		return (1);		/* found something */
1801.6Smrg	}
1811.6Smrg	closedir(dirp);
1821.6Smrg	return (0);
1831.6Smrg}
1841.6Smrg
1851.7Smrgstatic void
1861.1Scgdusage()
1871.1Scgd{
1881.6Smrg	puts("usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]");
1891.1Scgd	exit(1);
1901.1Scgd}
191