lpq.c revision 1.6
11.6Smrg/*	$NetBSD: lpq.c,v 1.6 1997/10/05 11:52:38 mrg Exp $	*/
21.1Scgd/*
31.3Scgd * Copyright (c) 1983, 1993
41.3Scgd *	The Regents of the University of California.  All rights reserved.
51.3Scgd *
61.1Scgd *
71.1Scgd * Redistribution and use in source and binary forms, with or without
81.1Scgd * modification, are permitted provided that the following conditions
91.1Scgd * are met:
101.1Scgd * 1. Redistributions of source code must retain the above copyright
111.1Scgd *    notice, this list of conditions and the following disclaimer.
121.1Scgd * 2. Redistributions in binary form must reproduce the above copyright
131.1Scgd *    notice, this list of conditions and the following disclaimer in the
141.1Scgd *    documentation and/or other materials provided with the distribution.
151.1Scgd * 3. All advertising materials mentioning features or use of this software
161.1Scgd *    must display the following acknowledgement:
171.1Scgd *	This product includes software developed by the University of
181.1Scgd *	California, Berkeley and its contributors.
191.1Scgd * 4. Neither the name of the University nor the names of its contributors
201.1Scgd *    may be used to endorse or promote products derived from this software
211.1Scgd *    without specific prior written permission.
221.1Scgd *
231.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
241.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
251.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
261.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
271.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
281.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
291.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
301.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
311.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
321.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
331.1Scgd * SUCH DAMAGE.
341.1Scgd */
351.1Scgd
361.1Scgd#ifndef lint
371.3Scgdstatic char copyright[] =
381.3Scgd"@(#) Copyright (c) 1983, 1993\n\
391.3Scgd	The Regents of the University of California.  All rights reserved.\n";
401.1Scgd#endif /* not lint */
411.1Scgd
421.1Scgd#ifndef lint
431.6Smrgstatic char sccsid[] = "@(#)lpq.c	8.3 (Berkeley) 5/10/95";
441.1Scgd#endif /* not lint */
451.1Scgd
461.1Scgd/*
471.1Scgd * Spool Queue examination program
481.1Scgd *
491.6Smrg * lpq [-a] [-l] [-Pprinter] [user...] [job...]
501.1Scgd *
511.6Smrg * -a show all non-null queues on the local machine
521.1Scgd * -l long output
531.1Scgd * -P used to identify printer as per lpr/lprm
541.1Scgd */
551.1Scgd
561.3Scgd#include <sys/param.h>
571.3Scgd
581.3Scgd#include <syslog.h>
591.3Scgd#include <dirent.h>
601.3Scgd#include <unistd.h>
611.3Scgd#include <stdlib.h>
621.3Scgd#include <stdio.h>
631.3Scgd#include <ctype.h>
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.6Smrgstatic int ckqueue __P((char *));
751.3Scgdvoid usage __P((void));
761.1Scgd
771.3Scgdint
781.1Scgdmain(argc, argv)
791.1Scgd	register int	argc;
801.1Scgd	register char	**argv;
811.1Scgd{
821.1Scgd	extern char	*optarg;
831.1Scgd	extern int	optind;
841.6Smrg	int	ch, aflag, lflag;
851.6Smrg	char	*buf, *cp;
861.1Scgd
871.4Shpeyerl	euid = geteuid();
881.4Shpeyerl	uid = getuid();
891.4Shpeyerl	seteuid(uid);
901.1Scgd	name = *argv;
911.1Scgd	if (gethostname(host, sizeof(host))) {
921.1Scgd		perror("lpq: gethostname");
931.1Scgd		exit(1);
941.1Scgd	}
951.1Scgd	openlog("lpd", 0, LOG_LPR);
961.1Scgd
971.6Smrg	aflag = lflag = 0;
981.6Smrg	while ((ch = getopt(argc, argv, "alP:")) != EOF)
991.1Scgd		switch((char)ch) {
1001.6Smrg		case 'a':
1011.6Smrg			++aflag;
1021.6Smrg			break;
1031.1Scgd		case 'l':			/* long output */
1041.1Scgd			++lflag;
1051.1Scgd			break;
1061.1Scgd		case 'P':		/* printer name */
1071.1Scgd			printer = optarg;
1081.1Scgd			break;
1091.1Scgd		case '?':
1101.1Scgd		default:
1111.1Scgd			usage();
1121.1Scgd		}
1131.1Scgd
1141.6Smrg	if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
1151.1Scgd		printer = DEFLP;
1161.1Scgd
1171.1Scgd	for (argc -= optind, argv += optind; argc; --argc, ++argv)
1181.1Scgd		if (isdigit(argv[0][0])) {
1191.1Scgd			if (requests >= MAXREQUESTS)
1201.1Scgd				fatal("too many requests");
1211.1Scgd			requ[requests++] = atoi(*argv);
1221.1Scgd		}
1231.1Scgd		else {
1241.1Scgd			if (users >= MAXUSERS)
1251.1Scgd				fatal("too many users");
1261.1Scgd			user[users++] = *argv;
1271.1Scgd		}
1281.1Scgd
1291.6Smrg	if (aflag) {
1301.6Smrg		while (cgetnext(&buf, printcapdb) > 0) {
1311.6Smrg			if (ckqueue(buf) <= 0) {
1321.6Smrg				free(buf);
1331.6Smrg				continue;	/* no jobs */
1341.6Smrg			}
1351.6Smrg			for (cp = buf; *cp; cp++)
1361.6Smrg				if (*cp == '|' || *cp == ':') {
1371.6Smrg					*cp = '\0';
1381.6Smrg					break;
1391.6Smrg				}
1401.6Smrg			printer = buf;
1411.6Smrg			printf("%s:\n", printer);
1421.6Smrg			displayq(lflag);
1431.6Smrg			free(buf);
1441.6Smrg			printf("\n");
1451.6Smrg		}
1461.6Smrg	} else
1471.6Smrg		displayq(lflag);
1481.1Scgd	exit(0);
1491.1Scgd}
1501.1Scgd
1511.6Smrgstatic int
1521.6Smrgckqueue(cap)
1531.6Smrg	char *cap;
1541.6Smrg{
1551.6Smrg	register struct dirent *d;
1561.6Smrg	DIR *dirp;
1571.6Smrg	char *spooldir;
1581.6Smrg
1591.6Smrg	if (cgetstr(cap, "sd", &spooldir) == -1)
1601.6Smrg		spooldir = _PATH_DEFSPOOL;
1611.6Smrg	if ((dirp = opendir(spooldir)) == NULL)
1621.6Smrg		return (-1);
1631.6Smrg	while ((d = readdir(dirp)) != NULL) {
1641.6Smrg		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
1651.6Smrg			continue;	/* daemon control files only */
1661.6Smrg		closedir(dirp);
1671.6Smrg		return (1);		/* found something */
1681.6Smrg	}
1691.6Smrg	closedir(dirp);
1701.6Smrg	return (0);
1711.6Smrg}
1721.6Smrg
1731.3Scgdvoid
1741.1Scgdusage()
1751.1Scgd{
1761.6Smrg	puts("usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]");
1771.1Scgd	exit(1);
1781.1Scgd}
179