Home | History | Annotate | Line # | Download | only in lpq
      1 /*	$NetBSD: lpq.c,v 1.20 2022/04/08 10:17:55 andvar Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the University nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 #ifndef lint
     35 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
     36  The Regents of the University of California.  All rights reserved.");
     37 #if 0
     38 static char sccsid[] = "@(#)lpq.c	8.3 (Berkeley) 5/10/95";
     39 #else
     40 __RCSID("$NetBSD: lpq.c,v 1.20 2022/04/08 10:17:55 andvar Exp $");
     41 #endif
     42 #endif /* not lint */
     43 
     44 /*
     45  * Spool Queue examination program
     46  *
     47  * lpq [-a] [-l] [-Pprinter] [user...] [job...]
     48  *
     49  * -a show all non-null queues on the local machine
     50  * -l long output
     51  * -P used to identify printer as per lpr/lprm
     52  */
     53 
     54 #include <sys/param.h>
     55 
     56 #include <syslog.h>
     57 #include <dirent.h>
     58 #include <unistd.h>
     59 #include <stdlib.h>
     60 #include <stdio.h>
     61 #include <ctype.h>
     62 #include <err.h>
     63 
     64 #include "lp.h"
     65 #include "lp.local.h"
     66 #include "pathnames.h"
     67 
     68 int	 requ[MAXREQUESTS];	/* job number of spool entries */
     69 int	 requests;		/* # of spool requests */
     70 char	*user[MAXUSERS];	/* users to process */
     71 int	 users;			/* # of users in user array */
     72 uid_t	uid, euid;
     73 
     74 static void usage(void) __dead;
     75 
     76 int
     77 main(int argc, char *argv[])
     78 {
     79 	int	ch, aflag, lflag;
     80 	char	*buf, *cp;
     81 
     82 	setprogname(*argv);
     83 	euid = geteuid();
     84 	uid = getuid();
     85 	seteuid(uid);
     86 	if (gethostname(host, sizeof(host)))
     87 		err(1, "lpq: gethostname");
     88 	host[sizeof(host) - 1] = '\0';
     89 	openlog("lpd", 0, LOG_LPR);
     90 
     91 	aflag = lflag = 0;
     92 	while ((ch = getopt(argc, argv, "alP:w:")) != -1)
     93 		switch((char)ch) {
     94 		case 'a':
     95 			++aflag;
     96 			break;
     97 		case 'l':			/* long output */
     98 			++lflag;
     99 			break;
    100 		case 'P':		/* printer name */
    101 			printer = optarg;
    102 			break;
    103 		case 'w':
    104 			wait_time = atoi(optarg);
    105 			if (wait_time < 0)
    106 				errx(1, "wait time must be positive: %s",
    107 				    optarg);
    108 			if (wait_time < 30)
    109 			    warnx("warning: wait time less than 30 seconds");
    110 			break;
    111 		case '?':
    112 		default:
    113 			usage();
    114 		}
    115 
    116 	if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
    117 		printer = DEFLP;
    118 
    119 	for (argc -= optind, argv += optind; argc; --argc, ++argv)
    120 		if (isdigit((unsigned char)argv[0][0])) {
    121 			if (requests >= MAXREQUESTS)
    122 				fatal("too many requests");
    123 			requ[requests++] = atoi(*argv);
    124 		}
    125 		else {
    126 			if (users >= MAXUSERS)
    127 				fatal("too many users");
    128 			user[users++] = *argv;
    129 		}
    130 
    131 	if (aflag) {
    132 		while (cgetnext(&buf, printcapdb) > 0) {
    133 			if (ckqueue(buf) <= 0) {
    134 				free(buf);
    135 				continue;	/* no jobs */
    136 			}
    137 			for (cp = buf; *cp; cp++)
    138 				if (*cp == '|' || *cp == ':') {
    139 					*cp = '\0';
    140 					break;
    141 				}
    142 			printer = buf;
    143 			printf("%s:\n", printer);
    144 			displayq(lflag);
    145 			free(buf);
    146 			printf("\n");
    147 		}
    148 	} else
    149 		displayq(lflag);
    150 	return 0;
    151 }
    152 
    153 static void
    154 usage(void)
    155 {
    156 	(void)fprintf(stderr, "Usage: %s [-a] [-l] [-Pprinter] [-w maxwait] [user ...] [job ...]\n", getprogname());
    157 	exit(1);
    158 }
    159