xref: /freebsd/usr.sbin/lpr/lpq/lpq.c (revision ba7a1ad76a812b38f8bddc730e03a14591d997b8)
10b561052SJoerg Wunsch /*
20b561052SJoerg Wunsch  * Copyright (c) 1983, 1993
30b561052SJoerg Wunsch  *	The Regents of the University of California.  All rights reserved.
40b561052SJoerg Wunsch  *
50b561052SJoerg Wunsch  *
60b561052SJoerg Wunsch  * Redistribution and use in source and binary forms, with or without
70b561052SJoerg Wunsch  * modification, are permitted provided that the following conditions
80b561052SJoerg Wunsch  * are met:
90b561052SJoerg Wunsch  * 1. Redistributions of source code must retain the above copyright
100b561052SJoerg Wunsch  *    notice, this list of conditions and the following disclaimer.
110b561052SJoerg Wunsch  * 2. Redistributions in binary form must reproduce the above copyright
120b561052SJoerg Wunsch  *    notice, this list of conditions and the following disclaimer in the
130b561052SJoerg Wunsch  *    documentation and/or other materials provided with the distribution.
140b561052SJoerg Wunsch  * 3. All advertising materials mentioning features or use of this software
150b561052SJoerg Wunsch  *    must display the following acknowledgement:
160b561052SJoerg Wunsch  *	This product includes software developed by the University of
170b561052SJoerg Wunsch  *	California, Berkeley and its contributors.
180b561052SJoerg Wunsch  * 4. Neither the name of the University nor the names of its contributors
190b561052SJoerg Wunsch  *    may be used to endorse or promote products derived from this software
200b561052SJoerg Wunsch  *    without specific prior written permission.
210b561052SJoerg Wunsch  *
220b561052SJoerg Wunsch  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
230b561052SJoerg Wunsch  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
240b561052SJoerg Wunsch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
250b561052SJoerg Wunsch  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
260b561052SJoerg Wunsch  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
270b561052SJoerg Wunsch  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
280b561052SJoerg Wunsch  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
290b561052SJoerg Wunsch  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
300b561052SJoerg Wunsch  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
310b561052SJoerg Wunsch  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
320b561052SJoerg Wunsch  * SUCH DAMAGE.
330b561052SJoerg Wunsch  */
340b561052SJoerg Wunsch 
350b561052SJoerg Wunsch #ifndef lint
364a1a0dbeSGarrett Wollman static const char copyright[] =
370b561052SJoerg Wunsch "@(#) Copyright (c) 1983, 1993\n\
380b561052SJoerg Wunsch 	The Regents of the University of California.  All rights reserved.\n";
390b561052SJoerg Wunsch #endif /* not lint */
400b561052SJoerg Wunsch 
410b561052SJoerg Wunsch #ifndef lint
424a1a0dbeSGarrett Wollman /*
430b561052SJoerg Wunsch static char sccsid[] = "@(#)lpq.c	8.3 (Berkeley) 5/10/95";
444a1a0dbeSGarrett Wollman */
454a1a0dbeSGarrett Wollman static const char rcsid[] =
4697d92980SPeter Wemm   "$FreeBSD$";
470b561052SJoerg Wunsch #endif /* not lint */
480b561052SJoerg Wunsch 
490b561052SJoerg Wunsch /*
500b561052SJoerg Wunsch  * Spool Queue examination program
510b561052SJoerg Wunsch  *
520b561052SJoerg Wunsch  * lpq [-a] [-l] [-Pprinter] [user...] [job...]
530b561052SJoerg Wunsch  *
540b561052SJoerg Wunsch  * -a show all non-null queues on the local machine
550b561052SJoerg Wunsch  * -l long output
560b561052SJoerg Wunsch  * -P used to identify printer as per lpr/lprm
570b561052SJoerg Wunsch  */
580b561052SJoerg Wunsch 
590b561052SJoerg Wunsch #include <sys/param.h>
600b561052SJoerg Wunsch 
610b561052SJoerg Wunsch #include <ctype.h>
624a1a0dbeSGarrett Wollman #include <dirent.h>
634a1a0dbeSGarrett Wollman #include <err.h>
644a1a0dbeSGarrett Wollman #include <stdio.h>
654a1a0dbeSGarrett Wollman #include <stdlib.h>
664a1a0dbeSGarrett Wollman #include <syslog.h>
674a1a0dbeSGarrett Wollman #include <unistd.h>
684a1a0dbeSGarrett Wollman 
690b561052SJoerg Wunsch #include "lp.h"
700b561052SJoerg Wunsch #include "lp.local.h"
710b561052SJoerg Wunsch #include "pathnames.h"
720b561052SJoerg Wunsch 
730b561052SJoerg Wunsch int	 requ[MAXREQUESTS];	/* job number of spool entries */
740b561052SJoerg Wunsch int	 requests;		/* # of spool requests */
750b561052SJoerg Wunsch char	*user[MAXUSERS];	/* users to process */
760b561052SJoerg Wunsch int	 users;			/* # of users in user array */
770b561052SJoerg Wunsch 
78360d4ad5SWarner Losh uid_t	uid, euid;
79360d4ad5SWarner Losh 
80ba7a1ad7SGarance A Drosehn static int	 ckqueue(const struct printer *_pp);
81ba7a1ad7SGarance A Drosehn static void	 usage(void);
82ba7a1ad7SGarance A Drosehn int 		 main(int _argc, char **_argv);
830b561052SJoerg Wunsch 
840b561052SJoerg Wunsch int
85ba7a1ad7SGarance A Drosehn main(int argc, char **argv)
860b561052SJoerg Wunsch {
870b561052SJoerg Wunsch 	int ch, aflag, lflag;
88ba7a1ad7SGarance A Drosehn 	const char *printer;
894a1a0dbeSGarrett Wollman 	struct printer myprinter, *pp = &myprinter;
900b561052SJoerg Wunsch 
914a1a0dbeSGarrett Wollman 	printer = NULL;
92360d4ad5SWarner Losh 	euid = geteuid();
93360d4ad5SWarner Losh 	uid = getuid();
94360d4ad5SWarner Losh 	seteuid(uid);
950b561052SJoerg Wunsch 	name = *argv;
969b3fe531SPhilippe Charnier 	if (gethostname(host, sizeof(host)))
979b3fe531SPhilippe Charnier 		err(1, "gethostname");
980b561052SJoerg Wunsch 	openlog("lpd", 0, LOG_LPR);
990b561052SJoerg Wunsch 
1000b561052SJoerg Wunsch 	aflag = lflag = 0;
1016c3f552aSWarner Losh 	while ((ch = getopt(argc, argv, "alP:")) != -1)
1020b561052SJoerg Wunsch 		switch((char)ch) {
1030b561052SJoerg Wunsch 		case 'a':
1040b561052SJoerg Wunsch 			++aflag;
1050b561052SJoerg Wunsch 			break;
1060b561052SJoerg Wunsch 		case 'l':			/* long output */
1070b561052SJoerg Wunsch 			++lflag;
1080b561052SJoerg Wunsch 			break;
1090b561052SJoerg Wunsch 		case 'P':		/* printer name */
1100b561052SJoerg Wunsch 			printer = optarg;
1110b561052SJoerg Wunsch 			break;
1120b561052SJoerg Wunsch 		case '?':
1130b561052SJoerg Wunsch 		default:
1140b561052SJoerg Wunsch 			usage();
1150b561052SJoerg Wunsch 		}
1160b561052SJoerg Wunsch 
1170b561052SJoerg Wunsch 	if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
1180b561052SJoerg Wunsch 		printer = DEFLP;
1190b561052SJoerg Wunsch 
1200b561052SJoerg Wunsch 	for (argc -= optind, argv += optind; argc; --argc, ++argv)
1210b561052SJoerg Wunsch 		if (isdigit(argv[0][0])) {
1220b561052SJoerg Wunsch 			if (requests >= MAXREQUESTS)
1234a1a0dbeSGarrett Wollman 				fatal(0, "too many requests");
1240b561052SJoerg Wunsch 			requ[requests++] = atoi(*argv);
1250b561052SJoerg Wunsch 		}
1260b561052SJoerg Wunsch 		else {
1270b561052SJoerg Wunsch 			if (users >= MAXUSERS)
1284a1a0dbeSGarrett Wollman 				fatal(0, "too many users");
1290b561052SJoerg Wunsch 			user[users++] = *argv;
1300b561052SJoerg Wunsch 		}
1310b561052SJoerg Wunsch 
1320b561052SJoerg Wunsch 	if (aflag) {
1334a1a0dbeSGarrett Wollman 		int more, status;
1344a1a0dbeSGarrett Wollman 
1354a1a0dbeSGarrett Wollman 		more = firstprinter(pp, &status);
1364a1a0dbeSGarrett Wollman 		if (status)
1374a1a0dbeSGarrett Wollman 			goto looperr;
1384a1a0dbeSGarrett Wollman 		while (more) {
1394a1a0dbeSGarrett Wollman 			if (ckqueue(pp) > 0) {
1404a1a0dbeSGarrett Wollman 				printf("%s:\n", pp->printer);
1414a1a0dbeSGarrett Wollman 				displayq(pp, lflag);
1420b561052SJoerg Wunsch 				printf("\n");
1430b561052SJoerg Wunsch 			}
1444a1a0dbeSGarrett Wollman 			do {
1454a1a0dbeSGarrett Wollman 				more = nextprinter(pp, &status);
1464a1a0dbeSGarrett Wollman looperr:
1474a1a0dbeSGarrett Wollman 				switch (status) {
1484a1a0dbeSGarrett Wollman 				case PCAPERR_TCOPEN:
1494a1a0dbeSGarrett Wollman 					printf("warning: %s: unresolved "
1504a1a0dbeSGarrett Wollman 					       "tc= reference(s) ",
1514a1a0dbeSGarrett Wollman 					       pp->printer);
1524a1a0dbeSGarrett Wollman 				case PCAPERR_SUCCESS:
1534a1a0dbeSGarrett Wollman 					break;
1544a1a0dbeSGarrett Wollman 				default:
1554a1a0dbeSGarrett Wollman 					fatal(pp, pcaperr(status));
1564a1a0dbeSGarrett Wollman 				}
1574a1a0dbeSGarrett Wollman 			} while (more && status);
1584a1a0dbeSGarrett Wollman 		}
1594a1a0dbeSGarrett Wollman 	} else {
1604a1a0dbeSGarrett Wollman 		int status;
1614a1a0dbeSGarrett Wollman 
1624a1a0dbeSGarrett Wollman 		init_printer(pp);
1634a1a0dbeSGarrett Wollman 		status = getprintcap(printer, pp);
1644a1a0dbeSGarrett Wollman 		if (status < 0)
1654a1a0dbeSGarrett Wollman 			fatal(pp, pcaperr(status));
1664a1a0dbeSGarrett Wollman 
1674a1a0dbeSGarrett Wollman 		displayq(pp, lflag);
1684a1a0dbeSGarrett Wollman 	}
1690b561052SJoerg Wunsch 	exit(0);
1700b561052SJoerg Wunsch }
1710b561052SJoerg Wunsch 
1720b561052SJoerg Wunsch static int
173ba7a1ad7SGarance A Drosehn ckqueue(const struct printer *pp)
1740b561052SJoerg Wunsch {
1750b561052SJoerg Wunsch 	register struct dirent *d;
1760b561052SJoerg Wunsch 	DIR *dirp;
1770b561052SJoerg Wunsch 	char *spooldir;
1780b561052SJoerg Wunsch 
1794a1a0dbeSGarrett Wollman 	spooldir = pp->spool_dir;
1800b561052SJoerg Wunsch 	if ((dirp = opendir(spooldir)) == NULL)
1810b561052SJoerg Wunsch 		return (-1);
1820b561052SJoerg Wunsch 	while ((d = readdir(dirp)) != NULL) {
1830b561052SJoerg Wunsch 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
1840b561052SJoerg Wunsch 			continue;	/* daemon control files only */
1850b561052SJoerg Wunsch 		closedir(dirp);
1860b561052SJoerg Wunsch 		return (1);		/* found something */
1870b561052SJoerg Wunsch 	}
1880b561052SJoerg Wunsch 	closedir(dirp);
1890b561052SJoerg Wunsch 	return (0);
1900b561052SJoerg Wunsch }
1910b561052SJoerg Wunsch 
1929b3fe531SPhilippe Charnier static void
193ba7a1ad7SGarance A Drosehn usage(void)
1940b561052SJoerg Wunsch {
1959b3fe531SPhilippe Charnier 	fprintf(stderr,
1969b3fe531SPhilippe Charnier 	"usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]\n");
1970b561052SJoerg Wunsch 	exit(1);
1980b561052SJoerg Wunsch }
199