1 /*	$NetBSD: displayq.c,v 1.5.2.1 1995/11/19 00:41:30 pk Exp $	*/
2 /*
3  * Copyright (c) 1983, 1993
4  *	The Regents of the University of California.  All rights reserved.
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 sccsid[] = "@(#)displayq.c	8.4 (Berkeley) 4/28/95";
37 #endif /* not lint */
38 
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #include <sys/file.h>
42 
43 #include <signal.h>
44 #include <fcntl.h>
45 #include <dirent.h>
46 #include <unistd.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <ctype.h>
51 #include "lp.h"
52 #include "lp.local.h"
53 #include "pathnames.h"
54 
55 /*
56  * Routines to display the state of the queue.
57  */
58 #define JOBCOL	40		/* column for job # in -l format */
59 #define OWNCOL	7		/* start of Owner column in normal */
60 #define SIZCOL	62		/* start of Size column in normal */
61 
62 /*
63  * Stuff for handling job specifications
64  */
65 extern int	requ[];		/* job number of spool entries */
66 extern int	requests;	/* # of spool requests */
67 extern char    *user[];	        /* users to process */
68 extern int	users;		/* # of users in user array */
69 
70 extern uid_t	uid, euid;
71 
72 static int	col;		/* column on screen */
73 static char	current[40];	/* current file being printed */
74 static char	file[132];	/* print file name */
75 static int	first;		/* first file in ``files'' column? */
76 static int	garbage;	/* # of garbage cf files */
77 static int	lflag;		/* long output option */
78 static int	rank;		/* order to be printed (-1=none, 0=active) */
79 static long	totsize;	/* total print job size in bytes */
80 
81 static char	*head0 = "Rank   Owner      Job  Files";
82 static char	*head1 = "Total Size\n";
83 
84 /*
85  * Display the current state of the queue. Format = 1 if long format.
86  */
87 void
88 displayq(format)
89 	int format;
90 {
91 	register struct queue *q;
92 	register int i, nitems, fd, ret;
93 	register char	*cp;
94 	struct queue **queue;
95 	struct stat statb;
96 	FILE *fp;
97 
98 	lflag = format;
99 	totsize = 0;
100 	rank = -1;
101 	if ((i = cgetent(&bp, printcapdb, printer)) == -2)
102 		fatal("can't open printer description file");
103 	else if (i == -1)
104 		fatal("unknown printer");
105 	else if (i == -3)
106 		fatal("potential reference loop detected in printcap file");
107 	if (cgetstr(bp, "lp", &LP) < 0)
108 		LP = _PATH_DEFDEVLP;
109 	if (cgetstr(bp, "rp", &RP) < 0)
110 		RP = DEFLP;
111 	if (cgetstr(bp, "sd", &SD) < 0)
112 		SD = _PATH_DEFSPOOL;
113 	if (cgetstr(bp,"lo", &LO) < 0)
114 		LO = DEFLOCK;
115 	if (cgetstr(bp, "st", &ST) < 0)
116 		ST = DEFSTAT;
117 	cgetstr(bp, "rm", &RM);
118 	if (cp = checkremote())
119 		printf("Warning: %s\n", cp);
120 
121 	/*
122 	 * Print out local queue
123 	 * Find all the control files in the spooling directory
124 	 */
125 	seteuid(euid);
126 	if (chdir(SD) < 0)
127 		fatal("cannot chdir to spooling directory");
128 	seteuid(uid);
129 	if ((nitems = getq(&queue)) < 0)
130 		fatal("cannot examine spooling area\n");
131 	seteuid(euid);
132 	ret = stat(LO, &statb);
133 	seteuid(uid);
134 	if (ret >= 0) {
135 		if (statb.st_mode & 0100) {
136 			if (remote)
137 				printf("%s: ", host);
138 			printf("Warning: %s is down: ", printer);
139 			seteuid(euid);
140 			fd = open(ST, O_RDONLY);
141 			seteuid(uid);
142 			if (fd >= 0) {
143 				(void) flock(fd, LOCK_SH);
144 				while ((i = read(fd, line, sizeof(line))) > 0)
145 					(void) fwrite(line, 1, i, stdout);
146 				(void) close(fd);	/* unlocks as well */
147 			} else
148 				putchar('\n');
149 		}
150 		if (statb.st_mode & 010) {
151 			if (remote)
152 				printf("%s: ", host);
153 			printf("Warning: %s queue is turned off\n", printer);
154 		}
155 	}
156 
157 	if (nitems) {
158 		seteuid(euid);
159 		fp = fopen(LO, "r");
160 		seteuid(uid);
161 		if (fp == NULL)
162 			warn();
163 		else {
164 			/* get daemon pid */
165 			cp = current;
166 			while ((i = getc(fp)) != EOF && i != '\n')
167 				*cp++ = i;
168 			*cp = '\0';
169 			i = atoi(current);
170 			if (i <= 0) {
171 				ret = -1;
172 			} else {
173 				seteuid(euid);
174 				ret = kill(i, 0);
175 				seteuid(uid);
176 			}
177 			if (ret < 0) {
178 				warn();
179 			} else {
180 				/* read current file name */
181 				cp = current;
182 				while ((i = getc(fp)) != EOF && i != '\n')
183 					*cp++ = i;
184 				*cp = '\0';
185 				/*
186 				 * Print the status file.
187 				 */
188 				if (remote)
189 					printf("%s: ", host);
190 				seteuid(euid);
191 				fd = open(ST, O_RDONLY);
192 				seteuid(uid);
193 				if (fd >= 0) {
194 					(void) flock(fd, LOCK_SH);
195 					while ((i = read(fd, line, sizeof(line))) > 0)
196 						(void) fwrite(line, 1, i, stdout);
197 					(void) close(fd);	/* unlocks as well */
198 				} else
199 					putchar('\n');
200 			}
201 			(void) fclose(fp);
202 		}
203 		/*
204 		 * Now, examine the control files and print out the jobs to
205 		 * be done for each user.
206 		 */
207 		if (!lflag)
208 			header();
209 		for (i = 0; i < nitems; i++) {
210 			q = queue[i];
211 			inform(q->q_name);
212 			free(q);
213 		}
214 		free(queue);
215 	}
216 	if (!remote) {
217 		if (nitems == 0)
218 			puts("no entries");
219 		return;
220 	}
221 
222 	/*
223 	 * Print foreign queue
224 	 * Note that a file in transit may show up in either queue.
225 	 */
226 	if (nitems)
227 		putchar('\n');
228 	(void) snprintf(line, sizeof line, "%c%s", format + '\3', RP);
229 	cp = line;
230 	for (i = 0; i < requests && cp-line+10 < sizeof line; i++) {
231 		cp += strlen(cp);
232 		(void) sprintf(cp, " %d", requ[i]);
233 	}
234 	for (i = 0; i < users && cp-line+1+strlen(user[i]) <
235 	    sizeof line; i++) {
236 		cp += strlen(cp);
237 		*cp++ = ' ';
238 		(void) strcpy(cp, user[i]);
239 	}
240 	strcat(line, "\n");
241 	fd = getport(RM, 0);
242 	if (fd < 0) {
243 		if (from != host)
244 			printf("%s: ", host);
245 		printf("connection to %s is down\n", RM);
246 	}
247 	else {
248 		i = strlen(line);
249 		if (write(fd, line, i) != i)
250 			fatal("Lost connection");
251 		while ((i = read(fd, line, sizeof(line))) > 0)
252 			(void) fwrite(line, 1, i, stdout);
253 		(void) close(fd);
254 	}
255 }
256 
257 /*
258  * Print a warning message if there is no daemon present.
259  */
260 void
261 warn()
262 {
263 	if (remote)
264 		printf("\n%s: ", host);
265 	puts("Warning: no daemon present");
266 	current[0] = '\0';
267 }
268 
269 /*
270  * Print the header for the short listing format
271  */
272 void
273 header()
274 {
275 	printf(head0);
276 	col = strlen(head0)+1;
277 	blankfill(SIZCOL);
278 	printf(head1);
279 }
280 
281 void
282 inform(cf)
283 	char *cf;
284 {
285 	register int j;
286 	FILE *cfp;
287 
288 	/*
289 	 * There's a chance the control file has gone away
290 	 * in the meantime; if this is the case just keep going
291 	 */
292 	seteuid(euid);
293 	if ((cfp = fopen(cf, "r")) == NULL)
294 		return;
295 	seteuid(uid);
296 
297 	if (rank < 0)
298 		rank = 0;
299 	if (remote || garbage || strcmp(cf, current))
300 		rank++;
301 	j = 0;
302 	while (getline(cfp)) {
303 		switch (line[0]) {
304 		case 'P': /* Was this file specified in the user's list? */
305 			if (!inlist(line+1, cf)) {
306 				fclose(cfp);
307 				return;
308 			}
309 			if (lflag) {
310 				printf("\n%s: ", line+1);
311 				col = strlen(line+1) + 2;
312 				prank(rank);
313 				blankfill(JOBCOL);
314 				printf(" [job %s]\n", cf+3);
315 			} else {
316 				col = 0;
317 				prank(rank);
318 				blankfill(OWNCOL);
319 				printf("%-10s %-3d  ", line+1, atoi(cf+3));
320 				col += 16;
321 				first = 1;
322 			}
323 			continue;
324 		default: /* some format specifer and file name? */
325 			if (line[0] < 'a' || line[0] > 'z')
326 				continue;
327 			if (j == 0 || strcmp(file, line+1) != 0)
328 				(void) strcpy(file, line+1);
329 			j++;
330 			continue;
331 		case 'N':
332 			show(line+1, file, j);
333 			file[0] = '\0';
334 			j = 0;
335 		}
336 	}
337 	fclose(cfp);
338 	if (!lflag) {
339 		blankfill(SIZCOL);
340 		printf("%ld bytes\n", totsize);
341 		totsize = 0;
342 	}
343 }
344 
345 int
346 inlist(name, file)
347 	char *name, *file;
348 {
349 	register int *r, n;
350 	register char **u, *cp;
351 
352 	if (users == 0 && requests == 0)
353 		return(1);
354 	/*
355 	 * Check to see if it's in the user list
356 	 */
357 	for (u = user; u < &user[users]; u++)
358 		if (!strcmp(*u, name))
359 			return(1);
360 	/*
361 	 * Check the request list
362 	 */
363 	for (n = 0, cp = file+3; isdigit(*cp); )
364 		n = n * 10 + (*cp++ - '0');
365 	for (r = requ; r < &requ[requests]; r++)
366 		if (*r == n && !strcmp(cp, from))
367 			return(1);
368 	return(0);
369 }
370 
371 void
372 show(nfile, file, copies)
373 	register char *nfile, *file;
374 	int copies;
375 {
376 	if (strcmp(nfile, " ") == 0)
377 		nfile = "(standard input)";
378 	if (lflag)
379 		ldump(nfile, file, copies);
380 	else
381 		dump(nfile, file, copies);
382 }
383 
384 /*
385  * Fill the line with blanks to the specified column
386  */
387 void
388 blankfill(n)
389 	register int n;
390 {
391 	while (col++ < n)
392 		putchar(' ');
393 }
394 
395 /*
396  * Give the abbreviated dump of the file names
397  */
398 void
399 dump(nfile, file, copies)
400 	char *nfile, *file;
401 	int copies;
402 {
403 	register short n, fill;
404 	struct stat lbuf;
405 
406 	/*
407 	 * Print as many files as will fit
408 	 *  (leaving room for the total size)
409 	 */
410 	 fill = first ? 0 : 2;	/* fill space for ``, '' */
411 	 if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) {
412 		if (col < SIZCOL) {
413 			printf(" ..."), col += 4;
414 			blankfill(SIZCOL);
415 		}
416 	} else {
417 		if (first)
418 			first = 0;
419 		else
420 			printf(", ");
421 		printf("%s", nfile);
422 		col += n+fill;
423 	}
424 	seteuid(euid);
425 	if (*file && !stat(file, &lbuf))
426 		totsize += copies * lbuf.st_size;
427 	seteuid(uid);
428 }
429 
430 /*
431  * Print the long info about the file
432  */
433 void
434 ldump(nfile, file, copies)
435 	char *nfile, *file;
436 	int copies;
437 {
438 	struct stat lbuf;
439 
440 	putchar('\t');
441 	if (copies > 1)
442 		printf("%-2d copies of %-19s", copies, nfile);
443 	else
444 		printf("%-32s", nfile);
445 	if (*file && !stat(file, &lbuf))
446 		printf(" %qd bytes", lbuf.st_size);
447 	else
448 		printf(" ??? bytes");
449 	putchar('\n');
450 }
451 
452 /*
453  * Print the job's rank in the queue,
454  *   update col for screen management
455  */
456 void
457 prank(n)
458 	int n;
459 {
460 	char rline[100];
461 	static char *r[] = {
462 		"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
463 	};
464 
465 	if (n == 0) {
466 		printf("active");
467 		col += 6;
468 		return;
469 	}
470 	if ((n/10)%10 == 1)
471 		(void)snprintf(rline, sizeof(rline), "%dth", n);
472 	else
473 		(void)snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]);
474 	col += strlen(rline);
475 	printf("%s", rline);
476 }
477