xref: /freebsd/usr.sbin/lpr/lpq/lpq.c (revision 360d4ad5ab5fb31d5812ef571e8a346b8752ff9b)
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
360b561052SJoerg Wunsch static 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
420b561052SJoerg Wunsch static char sccsid[] = "@(#)lpq.c	8.3 (Berkeley) 5/10/95";
430b561052SJoerg Wunsch #endif /* not lint */
440b561052SJoerg Wunsch 
450b561052SJoerg Wunsch /*
460b561052SJoerg Wunsch  * Spool Queue examination program
470b561052SJoerg Wunsch  *
480b561052SJoerg Wunsch  * lpq [-a] [-l] [-Pprinter] [user...] [job...]
490b561052SJoerg Wunsch  *
500b561052SJoerg Wunsch  * -a show all non-null queues on the local machine
510b561052SJoerg Wunsch  * -l long output
520b561052SJoerg Wunsch  * -P used to identify printer as per lpr/lprm
530b561052SJoerg Wunsch  */
540b561052SJoerg Wunsch 
550b561052SJoerg Wunsch #include <sys/param.h>
560b561052SJoerg Wunsch 
570b561052SJoerg Wunsch #include <syslog.h>
580b561052SJoerg Wunsch #include <dirent.h>
590b561052SJoerg Wunsch #include <unistd.h>
600b561052SJoerg Wunsch #include <stdlib.h>
610b561052SJoerg Wunsch #include <stdio.h>
620b561052SJoerg Wunsch #include <ctype.h>
630b561052SJoerg Wunsch #include "lp.h"
640b561052SJoerg Wunsch #include "lp.local.h"
650b561052SJoerg Wunsch #include "pathnames.h"
660b561052SJoerg Wunsch 
670b561052SJoerg Wunsch int	 requ[MAXREQUESTS];	/* job number of spool entries */
680b561052SJoerg Wunsch int	 requests;		/* # of spool requests */
690b561052SJoerg Wunsch char	*user[MAXUSERS];	/* users to process */
700b561052SJoerg Wunsch int	 users;			/* # of users in user array */
710b561052SJoerg Wunsch 
72360d4ad5SWarner Losh uid_t	uid, euid;
73360d4ad5SWarner Losh 
740b561052SJoerg Wunsch static int ckqueue __P((char *));
750b561052SJoerg Wunsch void usage __P((void));
760b561052SJoerg Wunsch 
770b561052SJoerg Wunsch int
780b561052SJoerg Wunsch main(argc, argv)
790b561052SJoerg Wunsch 	register int	argc;
800b561052SJoerg Wunsch 	register char	**argv;
810b561052SJoerg Wunsch {
820b561052SJoerg Wunsch 	extern char	*optarg;
830b561052SJoerg Wunsch 	extern int	optind;
840b561052SJoerg Wunsch 	int	ch, aflag, lflag;
850b561052SJoerg Wunsch 	char	*buf, *cp;
860b561052SJoerg Wunsch 
87360d4ad5SWarner Losh 	euid = geteuid();
88360d4ad5SWarner Losh 	uid = getuid();
89360d4ad5SWarner Losh 	seteuid(uid);
900b561052SJoerg Wunsch 	name = *argv;
910b561052SJoerg Wunsch 	if (gethostname(host, sizeof(host))) {
920b561052SJoerg Wunsch 		perror("lpq: gethostname");
930b561052SJoerg Wunsch 		exit(1);
940b561052SJoerg Wunsch 	}
950b561052SJoerg Wunsch 	openlog("lpd", 0, LOG_LPR);
960b561052SJoerg Wunsch 
970b561052SJoerg Wunsch 	aflag = lflag = 0;
986c3f552aSWarner Losh 	while ((ch = getopt(argc, argv, "alP:")) != -1)
990b561052SJoerg Wunsch 		switch((char)ch) {
1000b561052SJoerg Wunsch 		case 'a':
1010b561052SJoerg Wunsch 			++aflag;
1020b561052SJoerg Wunsch 			break;
1030b561052SJoerg Wunsch 		case 'l':			/* long output */
1040b561052SJoerg Wunsch 			++lflag;
1050b561052SJoerg Wunsch 			break;
1060b561052SJoerg Wunsch 		case 'P':		/* printer name */
1070b561052SJoerg Wunsch 			printer = optarg;
1080b561052SJoerg Wunsch 			break;
1090b561052SJoerg Wunsch 		case '?':
1100b561052SJoerg Wunsch 		default:
1110b561052SJoerg Wunsch 			usage();
1120b561052SJoerg Wunsch 		}
1130b561052SJoerg Wunsch 
1140b561052SJoerg Wunsch 	if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
1150b561052SJoerg Wunsch 		printer = DEFLP;
1160b561052SJoerg Wunsch 
1170b561052SJoerg Wunsch 	for (argc -= optind, argv += optind; argc; --argc, ++argv)
1180b561052SJoerg Wunsch 		if (isdigit(argv[0][0])) {
1190b561052SJoerg Wunsch 			if (requests >= MAXREQUESTS)
1200b561052SJoerg Wunsch 				fatal("too many requests");
1210b561052SJoerg Wunsch 			requ[requests++] = atoi(*argv);
1220b561052SJoerg Wunsch 		}
1230b561052SJoerg Wunsch 		else {
1240b561052SJoerg Wunsch 			if (users >= MAXUSERS)
1250b561052SJoerg Wunsch 				fatal("too many users");
1260b561052SJoerg Wunsch 			user[users++] = *argv;
1270b561052SJoerg Wunsch 		}
1280b561052SJoerg Wunsch 
1290b561052SJoerg Wunsch 	if (aflag) {
1300b561052SJoerg Wunsch 		while (cgetnext(&buf, printcapdb) > 0) {
1310b561052SJoerg Wunsch 			if (ckqueue(buf) <= 0) {
1320b561052SJoerg Wunsch 				free(buf);
1330b561052SJoerg Wunsch 				continue;	/* no jobs */
1340b561052SJoerg Wunsch 			}
1350b561052SJoerg Wunsch 			for (cp = buf; *cp; cp++)
1360b561052SJoerg Wunsch 				if (*cp == '|' || *cp == ':') {
1370b561052SJoerg Wunsch 					*cp = '\0';
1380b561052SJoerg Wunsch 					break;
1390b561052SJoerg Wunsch 				}
1400b561052SJoerg Wunsch 			printer = buf;
1410b561052SJoerg Wunsch 			printf("%s:\n", printer);
1420b561052SJoerg Wunsch 			displayq(lflag);
1430b561052SJoerg Wunsch 			free(buf);
1440b561052SJoerg Wunsch 			printf("\n");
1450b561052SJoerg Wunsch 		}
1460b561052SJoerg Wunsch 	} else
1470b561052SJoerg Wunsch 		displayq(lflag);
1480b561052SJoerg Wunsch 	exit(0);
1490b561052SJoerg Wunsch }
1500b561052SJoerg Wunsch 
1510b561052SJoerg Wunsch static int
1520b561052SJoerg Wunsch ckqueue(cap)
1530b561052SJoerg Wunsch 	char *cap;
1540b561052SJoerg Wunsch {
1550b561052SJoerg Wunsch 	register struct dirent *d;
1560b561052SJoerg Wunsch 	DIR *dirp;
1570b561052SJoerg Wunsch 	char *spooldir;
1580b561052SJoerg Wunsch 
1590b561052SJoerg Wunsch 	if (cgetstr(cap, "sd", &spooldir) == -1)
1600b561052SJoerg Wunsch 		spooldir = _PATH_DEFSPOOL;
1610b561052SJoerg Wunsch 	if ((dirp = opendir(spooldir)) == NULL)
1620b561052SJoerg Wunsch 		return (-1);
1630b561052SJoerg Wunsch 	while ((d = readdir(dirp)) != NULL) {
1640b561052SJoerg Wunsch 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
1650b561052SJoerg Wunsch 			continue;	/* daemon control files only */
1660b561052SJoerg Wunsch 		closedir(dirp);
1670b561052SJoerg Wunsch 		return (1);		/* found something */
1680b561052SJoerg Wunsch 	}
1690b561052SJoerg Wunsch 	closedir(dirp);
1700b561052SJoerg Wunsch 	return (0);
1710b561052SJoerg Wunsch }
1720b561052SJoerg Wunsch 
1730b561052SJoerg Wunsch void
1740b561052SJoerg Wunsch usage()
1750b561052SJoerg Wunsch {
1760b561052SJoerg Wunsch 	puts("usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]");
1770b561052SJoerg Wunsch 	exit(1);
1780b561052SJoerg Wunsch }
179