lpq.c revision 1.20
11.20Sandvar/*	$NetBSD: lpq.c,v 1.20 2022/04/08 10:17:55 andvar 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.18Slukem__COPYRIGHT("@(#) Copyright (c) 1983, 1993\
361.18Slukem The Regents of the University of California.  All rights reserved.");
371.7Smrg#if 0
381.6Smrgstatic char sccsid[] = "@(#)lpq.c	8.3 (Berkeley) 5/10/95";
391.7Smrg#else
401.20Sandvar__RCSID("$NetBSD: lpq.c,v 1.20 2022/04/08 10:17:55 andvar 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.17Sperrystatic void usage(void) __dead;
751.1Scgd
761.3Scgdint
771.12Swizmain(int argc, char *argv[])
781.1Scgd{
791.6Smrg	int	ch, aflag, lflag;
801.6Smrg	char	*buf, *cp;
811.1Scgd
821.16Schristos	setprogname(*argv);
831.4Shpeyerl	euid = geteuid();
841.4Shpeyerl	uid = getuid();
851.4Shpeyerl	seteuid(uid);
861.7Smrg	if (gethostname(host, sizeof(host)))
871.7Smrg		err(1, "lpq: gethostname");
881.8Smrg	host[sizeof(host) - 1] = '\0';
891.1Scgd	openlog("lpd", 0, LOG_LPR);
901.1Scgd
911.6Smrg	aflag = lflag = 0;
921.9Smrg	while ((ch = getopt(argc, argv, "alP:w:")) != -1)
931.1Scgd		switch((char)ch) {
941.6Smrg		case 'a':
951.6Smrg			++aflag;
961.6Smrg			break;
971.1Scgd		case 'l':			/* long output */
981.1Scgd			++lflag;
991.1Scgd			break;
1001.1Scgd		case 'P':		/* printer name */
1011.1Scgd			printer = optarg;
1021.9Smrg			break;
1031.9Smrg		case 'w':
1041.9Smrg			wait_time = atoi(optarg);
1051.9Smrg			if (wait_time < 0)
1061.20Sandvar				errx(1, "wait time must be positive: %s",
1071.9Smrg				    optarg);
1081.9Smrg			if (wait_time < 30)
1091.9Smrg			    warnx("warning: wait time less than 30 seconds");
1101.1Scgd			break;
1111.1Scgd		case '?':
1121.1Scgd		default:
1131.1Scgd			usage();
1141.1Scgd		}
1151.1Scgd
1161.6Smrg	if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
1171.1Scgd		printer = DEFLP;
1181.1Scgd
1191.1Scgd	for (argc -= optind, argv += optind; argc; --argc, ++argv)
1201.15Sdsl		if (isdigit((unsigned char)argv[0][0])) {
1211.1Scgd			if (requests >= MAXREQUESTS)
1221.1Scgd				fatal("too many requests");
1231.1Scgd			requ[requests++] = atoi(*argv);
1241.1Scgd		}
1251.1Scgd		else {
1261.1Scgd			if (users >= MAXUSERS)
1271.1Scgd				fatal("too many users");
1281.1Scgd			user[users++] = *argv;
1291.1Scgd		}
1301.1Scgd
1311.6Smrg	if (aflag) {
1321.6Smrg		while (cgetnext(&buf, printcapdb) > 0) {
1331.6Smrg			if (ckqueue(buf) <= 0) {
1341.6Smrg				free(buf);
1351.6Smrg				continue;	/* no jobs */
1361.6Smrg			}
1371.6Smrg			for (cp = buf; *cp; cp++)
1381.6Smrg				if (*cp == '|' || *cp == ':') {
1391.6Smrg					*cp = '\0';
1401.6Smrg					break;
1411.6Smrg				}
1421.6Smrg			printer = buf;
1431.6Smrg			printf("%s:\n", printer);
1441.6Smrg			displayq(lflag);
1451.6Smrg			free(buf);
1461.6Smrg			printf("\n");
1471.6Smrg		}
1481.6Smrg	} else
1491.6Smrg		displayq(lflag);
1501.16Schristos	return 0;
1511.6Smrg}
1521.6Smrg
1531.7Smrgstatic void
1541.12Swizusage(void)
1551.1Scgd{
1561.19Smishka	(void)fprintf(stderr, "Usage: %s [-a] [-l] [-Pprinter] [-w maxwait] [user ...] [job ...]\n", getprogname());
1571.1Scgd	exit(1);
1581.1Scgd}
159