lpq.c revision 1.15
11.15Sdsl/*	$NetBSD: lpq.c,v 1.15 2004/10/30 08:44:26 dsl 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.13Sagc * 3. Neither the name of the University nor the names of its contributors
171.1Scgd *    may be used to endorse or promote products derived from this software
181.1Scgd *    without specific prior written permission.
191.1Scgd *
201.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301.1Scgd * SUCH DAMAGE.
311.1Scgd */
321.1Scgd
331.7Smrg#include <sys/cdefs.h>
341.1Scgd#ifndef lint
351.7Smrg__COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
361.7Smrg	The Regents of the University of California.  All rights reserved.\n");
371.7Smrg#if 0
381.6Smrgstatic char sccsid[] = "@(#)lpq.c	8.3 (Berkeley) 5/10/95";
391.7Smrg#else
401.15Sdsl__RCSID("$NetBSD: lpq.c,v 1.15 2004/10/30 08:44:26 dsl Exp $");
411.7Smrg#endif
421.1Scgd#endif /* not lint */
431.1Scgd
441.1Scgd/*
451.1Scgd * Spool Queue examination program
461.1Scgd *
471.6Smrg * lpq [-a] [-l] [-Pprinter] [user...] [job...]
481.1Scgd *
491.6Smrg * -a show all non-null queues on the local machine
501.1Scgd * -l long output
511.1Scgd * -P used to identify printer as per lpr/lprm
521.1Scgd */
531.1Scgd
541.3Scgd#include <sys/param.h>
551.3Scgd
561.3Scgd#include <syslog.h>
571.3Scgd#include <dirent.h>
581.3Scgd#include <unistd.h>
591.3Scgd#include <stdlib.h>
601.3Scgd#include <stdio.h>
611.3Scgd#include <ctype.h>
621.7Smrg#include <err.h>
631.7Smrg
641.1Scgd#include "lp.h"
651.3Scgd#include "lp.local.h"
661.6Smrg#include "pathnames.h"
671.1Scgd
681.3Scgdint	 requ[MAXREQUESTS];	/* job number of spool entries */
691.3Scgdint	 requests;		/* # of spool requests */
701.1Scgdchar	*user[MAXUSERS];	/* users to process */
711.3Scgdint	 users;			/* # of users in user array */
721.4Shpeyerluid_t	uid, euid;
731.4Shpeyerl
741.12Swizstatic int ckqueue(char *);
751.12Swizstatic void usage(void);
761.12Swizint main(int, char *[]);
771.1Scgd
781.3Scgdint
791.12Swizmain(int argc, char *argv[])
801.1Scgd{
811.6Smrg	int	ch, aflag, lflag;
821.6Smrg	char	*buf, *cp;
831.1Scgd
841.4Shpeyerl	euid = geteuid();
851.4Shpeyerl	uid = getuid();
861.4Shpeyerl	seteuid(uid);
871.1Scgd	name = *argv;
881.7Smrg	if (gethostname(host, sizeof(host)))
891.7Smrg		err(1, "lpq: gethostname");
901.8Smrg	host[sizeof(host) - 1] = '\0';
911.1Scgd	openlog("lpd", 0, LOG_LPR);
921.1Scgd
931.6Smrg	aflag = lflag = 0;
941.9Smrg	while ((ch = getopt(argc, argv, "alP:w:")) != -1)
951.1Scgd		switch((char)ch) {
961.6Smrg		case 'a':
971.6Smrg			++aflag;
981.6Smrg			break;
991.1Scgd		case 'l':			/* long output */
1001.1Scgd			++lflag;
1011.1Scgd			break;
1021.1Scgd		case 'P':		/* printer name */
1031.1Scgd			printer = optarg;
1041.9Smrg			break;
1051.9Smrg		case 'w':
1061.9Smrg			wait_time = atoi(optarg);
1071.9Smrg			if (wait_time < 0)
1081.9Smrg				errx(1, "wait time must be postive: %s",
1091.9Smrg				    optarg);
1101.9Smrg			if (wait_time < 30)
1111.9Smrg			    warnx("warning: wait time less than 30 seconds");
1121.1Scgd			break;
1131.1Scgd		case '?':
1141.1Scgd		default:
1151.1Scgd			usage();
1161.1Scgd		}
1171.1Scgd
1181.6Smrg	if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
1191.1Scgd		printer = DEFLP;
1201.1Scgd
1211.1Scgd	for (argc -= optind, argv += optind; argc; --argc, ++argv)
1221.15Sdsl		if (isdigit((unsigned char)argv[0][0])) {
1231.1Scgd			if (requests >= MAXREQUESTS)
1241.1Scgd				fatal("too many requests");
1251.1Scgd			requ[requests++] = atoi(*argv);
1261.1Scgd		}
1271.1Scgd		else {
1281.1Scgd			if (users >= MAXUSERS)
1291.1Scgd				fatal("too many users");
1301.1Scgd			user[users++] = *argv;
1311.1Scgd		}
1321.1Scgd
1331.6Smrg	if (aflag) {
1341.6Smrg		while (cgetnext(&buf, printcapdb) > 0) {
1351.6Smrg			if (ckqueue(buf) <= 0) {
1361.6Smrg				free(buf);
1371.6Smrg				continue;	/* no jobs */
1381.6Smrg			}
1391.6Smrg			for (cp = buf; *cp; cp++)
1401.6Smrg				if (*cp == '|' || *cp == ':') {
1411.6Smrg					*cp = '\0';
1421.6Smrg					break;
1431.6Smrg				}
1441.6Smrg			printer = buf;
1451.6Smrg			printf("%s:\n", printer);
1461.6Smrg			displayq(lflag);
1471.6Smrg			free(buf);
1481.6Smrg			printf("\n");
1491.6Smrg		}
1501.6Smrg	} else
1511.6Smrg		displayq(lflag);
1521.1Scgd	exit(0);
1531.1Scgd}
1541.1Scgd
1551.6Smrgstatic int
1561.12Swizckqueue(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.14Sitojun	if ((dirp = opendir(spooldir)) == NULL) {
1651.14Sitojun		if (spooldir != _PATH_DEFSPOOL)
1661.14Sitojun			free(spooldir);
1671.6Smrg		return (-1);
1681.14Sitojun	}
1691.6Smrg	while ((d = readdir(dirp)) != NULL) {
1701.6Smrg		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
1711.6Smrg			continue;	/* daemon control files only */
1721.6Smrg		closedir(dirp);
1731.14Sitojun		if (spooldir != _PATH_DEFSPOOL)
1741.14Sitojun			free(spooldir);
1751.6Smrg		return (1);		/* found something */
1761.6Smrg	}
1771.6Smrg	closedir(dirp);
1781.14Sitojun	if (spooldir != _PATH_DEFSPOOL)
1791.14Sitojun		free(spooldir);
1801.6Smrg	return (0);
1811.6Smrg}
1821.6Smrg
1831.7Smrgstatic void
1841.12Swizusage(void)
1851.1Scgd{
1861.11Swiz	puts("usage: lpq [-a] [-l] [-Pprinter] [-w maxwait] [user ...] [job ...]");
1871.1Scgd	exit(1);
1881.1Scgd}
189