1*2fa7e141Sandvar /* $NetBSD: lpq.c,v 1.20 2022/04/08 10:17:55 andvar Exp $ */
2fe7ed7ceSmrg
361f28255Scgd /*
4f881d1d4Scgd * Copyright (c) 1983, 1993
5f881d1d4Scgd * The Regents of the University of California. All rights reserved.
6f881d1d4Scgd *
761f28255Scgd *
861f28255Scgd * Redistribution and use in source and binary forms, with or without
961f28255Scgd * modification, are permitted provided that the following conditions
1061f28255Scgd * are met:
1161f28255Scgd * 1. Redistributions of source code must retain the above copyright
1261f28255Scgd * notice, this list of conditions and the following disclaimer.
1361f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1461f28255Scgd * notice, this list of conditions and the following disclaimer in the
1561f28255Scgd * documentation and/or other materials provided with the distribution.
16326b2259Sagc * 3. Neither the name of the University nor the names of its contributors
1761f28255Scgd * may be used to endorse or promote products derived from this software
1861f28255Scgd * without specific prior written permission.
1961f28255Scgd *
2061f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2161f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2261f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2361f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2461f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2561f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2661f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2761f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2861f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2961f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3061f28255Scgd * SUCH DAMAGE.
3161f28255Scgd */
3261f28255Scgd
33fe7ed7ceSmrg #include <sys/cdefs.h>
3461f28255Scgd #ifndef lint
359c194566Slukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
369c194566Slukem The Regents of the University of California. All rights reserved.");
37fe7ed7ceSmrg #if 0
38e6a91a09Smrg static char sccsid[] = "@(#)lpq.c 8.3 (Berkeley) 5/10/95";
39fe7ed7ceSmrg #else
40*2fa7e141Sandvar __RCSID("$NetBSD: lpq.c,v 1.20 2022/04/08 10:17:55 andvar Exp $");
41fe7ed7ceSmrg #endif
4261f28255Scgd #endif /* not lint */
4361f28255Scgd
4461f28255Scgd /*
4561f28255Scgd * Spool Queue examination program
4661f28255Scgd *
47e6a91a09Smrg * lpq [-a] [-l] [-Pprinter] [user...] [job...]
4861f28255Scgd *
49e6a91a09Smrg * -a show all non-null queues on the local machine
5061f28255Scgd * -l long output
5161f28255Scgd * -P used to identify printer as per lpr/lprm
5261f28255Scgd */
5361f28255Scgd
54f881d1d4Scgd #include <sys/param.h>
5561f28255Scgd
56f881d1d4Scgd #include <syslog.h>
57f881d1d4Scgd #include <dirent.h>
58f881d1d4Scgd #include <unistd.h>
59f881d1d4Scgd #include <stdlib.h>
60f881d1d4Scgd #include <stdio.h>
61f881d1d4Scgd #include <ctype.h>
62fe7ed7ceSmrg #include <err.h>
63fe7ed7ceSmrg
64f881d1d4Scgd #include "lp.h"
65f881d1d4Scgd #include "lp.local.h"
66e6a91a09Smrg #include "pathnames.h"
67f881d1d4Scgd
6861f28255Scgd int requ[MAXREQUESTS]; /* job number of spool entries */
6961f28255Scgd int requests; /* # of spool requests */
70f881d1d4Scgd char *user[MAXUSERS]; /* users to process */
71f881d1d4Scgd int users; /* # of users in user array */
728e41ca80Shpeyerl uid_t uid, euid;
738e41ca80Shpeyerl
748b0f9554Sperry static void usage(void) __dead;
75f881d1d4Scgd
76f881d1d4Scgd int
main(int argc,char * argv[])77895dc72aSwiz main(int argc, char *argv[])
7861f28255Scgd {
79e6a91a09Smrg int ch, aflag, lflag;
80e6a91a09Smrg char *buf, *cp;
8161f28255Scgd
8204723c3fSchristos setprogname(*argv);
838e41ca80Shpeyerl euid = geteuid();
848e41ca80Shpeyerl uid = getuid();
858e41ca80Shpeyerl seteuid(uid);
86fe7ed7ceSmrg if (gethostname(host, sizeof(host)))
87fe7ed7ceSmrg err(1, "lpq: gethostname");
8832f51971Smrg host[sizeof(host) - 1] = '\0';
8961f28255Scgd openlog("lpd", 0, LOG_LPR);
9061f28255Scgd
91e6a91a09Smrg aflag = lflag = 0;
925b6d0e7eSmrg while ((ch = getopt(argc, argv, "alP:w:")) != -1)
9361f28255Scgd switch((char)ch) {
94e6a91a09Smrg case 'a':
95e6a91a09Smrg ++aflag;
96e6a91a09Smrg break;
9761f28255Scgd case 'l': /* long output */
9861f28255Scgd ++lflag;
9961f28255Scgd break;
10061f28255Scgd case 'P': /* printer name */
10161f28255Scgd printer = optarg;
10261f28255Scgd break;
1035b6d0e7eSmrg case 'w':
1045b6d0e7eSmrg wait_time = atoi(optarg);
1055b6d0e7eSmrg if (wait_time < 0)
106*2fa7e141Sandvar errx(1, "wait time must be positive: %s",
1075b6d0e7eSmrg optarg);
1085b6d0e7eSmrg if (wait_time < 30)
1095b6d0e7eSmrg warnx("warning: wait time less than 30 seconds");
1105b6d0e7eSmrg break;
11161f28255Scgd case '?':
11261f28255Scgd default:
11361f28255Scgd usage();
11461f28255Scgd }
11561f28255Scgd
116e6a91a09Smrg if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
11761f28255Scgd printer = DEFLP;
11861f28255Scgd
11961f28255Scgd for (argc -= optind, argv += optind; argc; --argc, ++argv)
12075ba9fc7Sdsl if (isdigit((unsigned char)argv[0][0])) {
12161f28255Scgd if (requests >= MAXREQUESTS)
12261f28255Scgd fatal("too many requests");
12361f28255Scgd requ[requests++] = atoi(*argv);
12461f28255Scgd }
12561f28255Scgd else {
12661f28255Scgd if (users >= MAXUSERS)
12761f28255Scgd fatal("too many users");
12861f28255Scgd user[users++] = *argv;
12961f28255Scgd }
13061f28255Scgd
131e6a91a09Smrg if (aflag) {
132e6a91a09Smrg while (cgetnext(&buf, printcapdb) > 0) {
133e6a91a09Smrg if (ckqueue(buf) <= 0) {
134e6a91a09Smrg free(buf);
135e6a91a09Smrg continue; /* no jobs */
136e6a91a09Smrg }
137e6a91a09Smrg for (cp = buf; *cp; cp++)
138e6a91a09Smrg if (*cp == '|' || *cp == ':') {
139e6a91a09Smrg *cp = '\0';
140e6a91a09Smrg break;
141e6a91a09Smrg }
142e6a91a09Smrg printer = buf;
143e6a91a09Smrg printf("%s:\n", printer);
144e6a91a09Smrg displayq(lflag);
145e6a91a09Smrg free(buf);
146e6a91a09Smrg printf("\n");
147e6a91a09Smrg }
148e6a91a09Smrg } else
14961f28255Scgd displayq(lflag);
15004723c3fSchristos return 0;
151e6a91a09Smrg }
152e6a91a09Smrg
153fe7ed7ceSmrg static void
usage(void)154895dc72aSwiz usage(void)
15561f28255Scgd {
1560678a172Smishka (void)fprintf(stderr, "Usage: %s [-a] [-l] [-Pprinter] [-w maxwait] [user ...] [job ...]\n", getprogname());
15761f28255Scgd exit(1);
15861f28255Scgd }
159