lpq.c revision 1.7
11.7Smrg/*	$NetBSD: lpq.c,v 1.7 1997/10/05 15:12:18 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.7Smrg__RCSID("$NetBSD: lpq.c,v 1.7 1997/10/05 15:12:18 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.1Scgd	openlog("lpd", 0, LOG_LPR);
991.1Scgd
1001.6Smrg	aflag = lflag = 0;
1011.7Smrg	while ((ch = getopt(argc, argv, "alP:")) != -1)
1021.1Scgd		switch((char)ch) {
1031.6Smrg		case 'a':
1041.6Smrg			++aflag;
1051.6Smrg			break;
1061.1Scgd		case 'l':			/* long output */
1071.1Scgd			++lflag;
1081.1Scgd			break;
1091.1Scgd		case 'P':		/* printer name */
1101.1Scgd			printer = optarg;
1111.1Scgd			break;
1121.1Scgd		case '?':
1131.1Scgd		default:
1141.1Scgd			usage();
1151.1Scgd		}
1161.1Scgd
1171.6Smrg	if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
1181.1Scgd		printer = DEFLP;
1191.1Scgd
1201.1Scgd	for (argc -= optind, argv += optind; argc; --argc, ++argv)
1211.1Scgd		if (isdigit(argv[0][0])) {
1221.1Scgd			if (requests >= MAXREQUESTS)
1231.1Scgd				fatal("too many requests");
1241.1Scgd			requ[requests++] = atoi(*argv);
1251.1Scgd		}
1261.1Scgd		else {
1271.1Scgd			if (users >= MAXUSERS)
1281.1Scgd				fatal("too many users");
1291.1Scgd			user[users++] = *argv;
1301.1Scgd		}
1311.1Scgd
1321.6Smrg	if (aflag) {
1331.6Smrg		while (cgetnext(&buf, printcapdb) > 0) {
1341.6Smrg			if (ckqueue(buf) <= 0) {
1351.6Smrg				free(buf);
1361.6Smrg				continue;	/* no jobs */
1371.6Smrg			}
1381.6Smrg			for (cp = buf; *cp; cp++)
1391.6Smrg				if (*cp == '|' || *cp == ':') {
1401.6Smrg					*cp = '\0';
1411.6Smrg					break;
1421.6Smrg				}
1431.6Smrg			printer = buf;
1441.6Smrg			printf("%s:\n", printer);
1451.6Smrg			displayq(lflag);
1461.6Smrg			free(buf);
1471.6Smrg			printf("\n");
1481.6Smrg		}
1491.6Smrg	} else
1501.6Smrg		displayq(lflag);
1511.1Scgd	exit(0);
1521.1Scgd}
1531.1Scgd
1541.6Smrgstatic int
1551.6Smrgckqueue(cap)
1561.6Smrg	char *cap;
1571.6Smrg{
1581.7Smrg	struct dirent *d;
1591.6Smrg	DIR *dirp;
1601.6Smrg	char *spooldir;
1611.6Smrg
1621.6Smrg	if (cgetstr(cap, "sd", &spooldir) == -1)
1631.6Smrg		spooldir = _PATH_DEFSPOOL;
1641.6Smrg	if ((dirp = opendir(spooldir)) == NULL)
1651.6Smrg		return (-1);
1661.6Smrg	while ((d = readdir(dirp)) != NULL) {
1671.6Smrg		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
1681.6Smrg			continue;	/* daemon control files only */
1691.6Smrg		closedir(dirp);
1701.6Smrg		return (1);		/* found something */
1711.6Smrg	}
1721.6Smrg	closedir(dirp);
1731.6Smrg	return (0);
1741.6Smrg}
1751.6Smrg
1761.7Smrgstatic void
1771.1Scgdusage()
1781.1Scgd{
1791.6Smrg	puts("usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]");
1801.1Scgd	exit(1);
1811.1Scgd}
182