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