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