displayq.c revision 1.8 1 /* $NetBSD: displayq.c,v 1.8 1996/12/09 09:57:41 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 static char sccsid[] = "@(#)displayq.c 8.1 (Berkeley) 6/6/93";
38 #endif /* not lint */
39
40 #include <sys/param.h>
41 #include <sys/stat.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 (sendtorem)
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 (sendtorem)
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 ((*cp = getc(fp)) != EOF && *cp != '\n')
167 cp++;
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 ((*cp = getc(fp)) != EOF && *cp != '\n')
183 cp++;
184 *cp = '\0';
185 /*
186 * Print the status file.
187 */
188 if (sendtorem)
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 (!sendtorem) {
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; i++) {
231 cp += strlen(cp);
232 (void)snprintf(cp, line - cp, " %d", requ[i]);
233 }
234 for (i = 0; i < users; i++) {
235 cp += strlen(cp);
236 if (cp - line > sizeof(line) - 1)
237 break;
238 *cp++ = ' ';
239 (void)strncpy(cp, user[i], line - cp - 1);
240 }
241 (void)strncat(line, "\n", sizeof(line) - strlen(line) - 1);
242 fd = getport(RM);
243 if (fd < 0) {
244 if (from != host)
245 printf("%s: ", host);
246 (void)printf("connection to %s is down\n", RM);
247 }
248 else {
249 i = strlen(line);
250 if (write(fd, line, i) != i)
251 fatal("Lost connection");
252 while ((i = read(fd, line, sizeof(line))) > 0)
253 (void)fwrite(line, 1, i, stdout);
254 (void)close(fd);
255 }
256 }
257
258 /*
259 * Print a warning message if there is no daemon present.
260 */
261 void
262 warn()
263 {
264 if (sendtorem)
265 printf("\n%s: ", host);
266 puts("Warning: no daemon present");
267 current[0] = '\0';
268 }
269
270 /*
271 * Print the header for the short listing format
272 */
273 void
274 header()
275 {
276 printf(head0);
277 col = strlen(head0)+1;
278 blankfill(SIZCOL);
279 printf(head1);
280 }
281
282 void
283 inform(cf)
284 char *cf;
285 {
286 register int j;
287 FILE *cfp;
288
289 /*
290 * There's a chance the control file has gone away
291 * in the meantime; if this is the case just keep going
292 */
293 seteuid(euid);
294 if ((cfp = fopen(cf, "r")) == NULL)
295 return;
296 seteuid(uid);
297
298 if (rank < 0)
299 rank = 0;
300 if (sendtorem || garbage || strcmp(cf, current))
301 rank++;
302 j = 0;
303 while (getline(cfp)) {
304 switch (line[0]) {
305 case 'P': /* Was this file specified in the user's list? */
306 if (!inlist(line+1, cf)) {
307 fclose(cfp);
308 return;
309 }
310 if (lflag) {
311 printf("\n%s: ", line+1);
312 col = strlen(line+1) + 2;
313 prank(rank);
314 blankfill(JOBCOL);
315 printf(" [job %s]\n", cf+3);
316 } else {
317 col = 0;
318 prank(rank);
319 blankfill(OWNCOL);
320 printf("%-10s %-3d ", line+1, atoi(cf+3));
321 col += 16;
322 first = 1;
323 }
324 continue;
325 default: /* some format specifer and file name? */
326 if (line[0] < 'a' || line[0] > 'z')
327 continue;
328 if (j == 0 || strcmp(file, line+1) != 0)
329 (void)strncpy(file, line+1, sizeof(file) - 1);
330 j++;
331 continue;
332 case 'N':
333 show(line+1, file, j);
334 file[0] = '\0';
335 j = 0;
336 }
337 }
338 fclose(cfp);
339 if (!lflag) {
340 blankfill(SIZCOL);
341 printf("%ld bytes\n", totsize);
342 totsize = 0;
343 }
344 }
345
346 int
347 inlist(name, file)
348 char *name, *file;
349 {
350 register int *r, n;
351 register char **u, *cp;
352
353 if (users == 0 && requests == 0)
354 return(1);
355 /*
356 * Check to see if it's in the user list
357 */
358 for (u = user; u < &user[users]; u++)
359 if (!strcmp(*u, name))
360 return(1);
361 /*
362 * Check the request list
363 */
364 for (n = 0, cp = file+3; isdigit(*cp); )
365 n = n * 10 + (*cp++ - '0');
366 for (r = requ; r < &requ[requests]; r++)
367 if (*r == n && !strcmp(cp, from))
368 return(1);
369 return(0);
370 }
371
372 void
373 show(nfile, file, copies)
374 register char *nfile, *file;
375 int copies;
376 {
377 if (strcmp(nfile, " ") == 0)
378 nfile = "(standard input)";
379 if (lflag)
380 ldump(nfile, file, copies);
381 else
382 dump(nfile, file, copies);
383 }
384
385 /*
386 * Fill the line with blanks to the specified column
387 */
388 void
389 blankfill(n)
390 register int n;
391 {
392 while (col++ < n)
393 putchar(' ');
394 }
395
396 /*
397 * Give the abbreviated dump of the file names
398 */
399 void
400 dump(nfile, file, copies)
401 char *nfile, *file;
402 int copies;
403 {
404 register short n, fill;
405 struct stat lbuf;
406
407 /*
408 * Print as many files as will fit
409 * (leaving room for the total size)
410 */
411 fill = first ? 0 : 2; /* fill space for ``, '' */
412 if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) {
413 if (col < SIZCOL) {
414 printf(" ..."), col += 4;
415 blankfill(SIZCOL);
416 }
417 } else {
418 if (first)
419 first = 0;
420 else
421 printf(", ");
422 printf("%s", nfile);
423 col += n+fill;
424 }
425 seteuid(euid);
426 if (*file && !stat(file, &lbuf))
427 totsize += copies * lbuf.st_size;
428 seteuid(uid);
429 }
430
431 /*
432 * Print the long info about the file
433 */
434 void
435 ldump(nfile, file, copies)
436 char *nfile, *file;
437 int copies;
438 {
439 struct stat lbuf;
440
441 putchar('\t');
442 if (copies > 1)
443 printf("%-2d copies of %-19s", copies, nfile);
444 else
445 printf("%-32s", nfile);
446 if (*file && !stat(file, &lbuf))
447 printf(" %qd bytes", lbuf.st_size);
448 else
449 printf(" ??? bytes");
450 putchar('\n');
451 }
452
453 /*
454 * Print the job's rank in the queue,
455 * update col for screen management
456 */
457 void
458 prank(n)
459 int n;
460 {
461 char rline[100];
462 static char *r[] = {
463 "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
464 };
465
466 if (n == 0) {
467 printf("active");
468 col += 6;
469 return;
470 }
471 if ((n/10)%10 == 1)
472 (void)snprintf(rline, sizeof(rline), "%dth", n);
473 else
474 (void)snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]);
475 col += strlen(rline);
476 printf("%s", rline);
477 }
478