11553Srgrimes/*
21553Srgrimes * Copyright (c) 1983, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes *
61553Srgrimes * Redistribution and use in source and binary forms, with or without
71553Srgrimes * modification, are permitted provided that the following conditions
81553Srgrimes * are met:
91553Srgrimes * 1. Redistributions of source code must retain the above copyright
101553Srgrimes *    notice, this list of conditions and the following disclaimer.
111553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
121553Srgrimes *    notice, this list of conditions and the following disclaimer in the
131553Srgrimes *    documentation and/or other materials provided with the distribution.
141553Srgrimes * 4. Neither the name of the University nor the names of its contributors
151553Srgrimes *    may be used to endorse or promote products derived from this software
161553Srgrimes *    without specific prior written permission.
171553Srgrimes *
181553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
191553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
221553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281553Srgrimes * SUCH DAMAGE.
291553Srgrimes */
301553Srgrimes
311553Srgrimes#ifndef lint
3231492Swollmanstatic const char copyright[] =
331553Srgrimes"@(#) Copyright (c) 1983, 1993\n\
341553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
351553Srgrimes#endif /* not lint */
361553Srgrimes
37117623Sgad#if 0
381553Srgrimes#ifndef lint
3915637Sjoergstatic char sccsid[] = "@(#)lpq.c	8.3 (Berkeley) 5/10/95";
401553Srgrimes#endif /* not lint */
41117623Sgad#endif
421553Srgrimes
43117623Sgad#include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
44117623Sgad__FBSDID("$FreeBSD$");
45117623Sgad
461553Srgrimes/*
471553Srgrimes * Spool Queue examination program
481553Srgrimes *
4915637Sjoerg * lpq [-a] [-l] [-Pprinter] [user...] [job...]
501553Srgrimes *
5115637Sjoerg * -a show all non-null queues on the local machine
521553Srgrimes * -l long output
531553Srgrimes * -P used to identify printer as per lpr/lprm
541553Srgrimes */
551553Srgrimes
561553Srgrimes#include <sys/param.h>
571553Srgrimes
5831492Swollman#include <ctype.h>
5931492Swollman#include <dirent.h>
6031492Swollman#include <err.h>
6131492Swollman#include <stdio.h>
6231492Swollman#include <stdlib.h>
631553Srgrimes#include <syslog.h>
641553Srgrimes#include <unistd.h>
6531492Swollman
661553Srgrimes#include "lp.h"
671553Srgrimes#include "lp.local.h"
6815637Sjoerg#include "pathnames.h"
691553Srgrimes
701553Srgrimesint	 requ[MAXREQUESTS];	/* job number of spool entries */
711553Srgrimesint	 requests;		/* # of spool requests */
721553Srgrimeschar	*user[MAXUSERS];	/* users to process */
731553Srgrimesint	 users;			/* # of users in user array */
741553Srgrimes
7527618Simpuid_t	uid, euid;
7627618Simp
7778146Sgadstatic int	 ckqueue(const struct printer *_pp);
7878146Sgadstatic void	 usage(void);
7978146Sgadint 		 main(int _argc, char **_argv);
801553Srgrimes
811553Srgrimesint
8278146Sgadmain(int argc, char **argv)
831553Srgrimes{
8431492Swollman	int ch, aflag, lflag;
8578146Sgad	const char *printer;
8631492Swollman	struct printer myprinter, *pp = &myprinter;
871553Srgrimes
8831492Swollman	printer = NULL;
8927618Simp	euid = geteuid();
9027618Simp	uid = getuid();
91241852Seadler	PRIV_END
9278280Sgad	progname = *argv;
9378300Sgad	if (gethostname(local_host, sizeof(local_host)))
9429780Scharnier		err(1, "gethostname");
951553Srgrimes	openlog("lpd", 0, LOG_LPR);
961553Srgrimes
9715637Sjoerg	aflag = lflag = 0;
9824428Simp	while ((ch = getopt(argc, argv, "alP:")) != -1)
991553Srgrimes		switch((char)ch) {
10015637Sjoerg		case 'a':
10115637Sjoerg			++aflag;
10215637Sjoerg			break;
1031553Srgrimes		case 'l':			/* long output */
1041553Srgrimes			++lflag;
1051553Srgrimes			break;
1061553Srgrimes		case 'P':		/* printer name */
1071553Srgrimes			printer = optarg;
1081553Srgrimes			break;
1091553Srgrimes		case '?':
1101553Srgrimes		default:
1111553Srgrimes			usage();
1121553Srgrimes		}
1131553Srgrimes
11415637Sjoerg	if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
1151553Srgrimes		printer = DEFLP;
1161553Srgrimes
1171553Srgrimes	for (argc -= optind, argv += optind; argc; --argc, ++argv)
1181553Srgrimes		if (isdigit(argv[0][0])) {
1191553Srgrimes			if (requests >= MAXREQUESTS)
12031492Swollman				fatal(0, "too many requests");
1211553Srgrimes			requ[requests++] = atoi(*argv);
1221553Srgrimes		}
1231553Srgrimes		else {
1241553Srgrimes			if (users >= MAXUSERS)
12531492Swollman				fatal(0, "too many users");
1261553Srgrimes			user[users++] = *argv;
1271553Srgrimes		}
1281553Srgrimes
12915637Sjoerg	if (aflag) {
13031492Swollman		int more, status;
13131492Swollman
13231492Swollman		more = firstprinter(pp, &status);
13331492Swollman		if (status)
13431492Swollman			goto looperr;
13531492Swollman		while (more) {
13631492Swollman			if (ckqueue(pp) > 0) {
13731492Swollman				printf("%s:\n", pp->printer);
13831492Swollman				displayq(pp, lflag);
13931492Swollman				printf("\n");
14015637Sjoerg			}
14131492Swollman			do {
14231492Swollman				more = nextprinter(pp, &status);
14331492Swollmanlooperr:
14431492Swollman				switch (status) {
14531492Swollman				case PCAPERR_TCOPEN:
14631492Swollman					printf("warning: %s: unresolved "
14731492Swollman					       "tc= reference(s) ",
14831492Swollman					       pp->printer);
14931492Swollman				case PCAPERR_SUCCESS:
15015637Sjoerg					break;
15131492Swollman				default:
15279739Sgad					fatal(pp, "%s", pcaperr(status));
15315637Sjoerg				}
15431492Swollman			} while (more && status);
15515637Sjoerg		}
15631492Swollman	} else {
15731492Swollman		int status;
15831492Swollman
15931492Swollman		init_printer(pp);
16031492Swollman		status = getprintcap(printer, pp);
16131492Swollman		if (status < 0)
16279739Sgad			fatal(pp, "%s", pcaperr(status));
16331492Swollman
16431492Swollman		displayq(pp, lflag);
16531492Swollman	}
1661553Srgrimes	exit(0);
1671553Srgrimes}
1681553Srgrimes
16915637Sjoergstatic int
17078146Sgadckqueue(const struct printer *pp)
17115637Sjoerg{
17215637Sjoerg	register struct dirent *d;
17315637Sjoerg	DIR *dirp;
17415637Sjoerg	char *spooldir;
17515637Sjoerg
17631492Swollman	spooldir = pp->spool_dir;
17715637Sjoerg	if ((dirp = opendir(spooldir)) == NULL)
17815637Sjoerg		return (-1);
17915637Sjoerg	while ((d = readdir(dirp)) != NULL) {
18015637Sjoerg		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
18115637Sjoerg			continue;	/* daemon control files only */
18215637Sjoerg		closedir(dirp);
18315637Sjoerg		return (1);		/* found something */
18415637Sjoerg	}
18515637Sjoerg	closedir(dirp);
18615637Sjoerg	return (0);
18715637Sjoerg}
18815637Sjoerg
18929780Scharnierstatic void
19078146Sgadusage(void)
1911553Srgrimes{
19229780Scharnier	fprintf(stderr,
19329780Scharnier	"usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]\n");
1941553Srgrimes	exit(1);
1951553Srgrimes}
196