displayq.c revision 1.29 1 1.29 garbled /* $NetBSD: displayq.c,v 1.29 2006/01/04 15:32:50 garbled Exp $ */
2 1.7 jtc
3 1.1 cgd /*
4 1.4 cgd * Copyright (c) 1983, 1993
5 1.4 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.26 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.12 mrg #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.10 mikel #if 0
35 1.11 mrg static char sccsid[] = "@(#)displayq.c 8.4 (Berkeley) 4/28/95";
36 1.10 mikel #else
37 1.29 garbled __RCSID("$NetBSD: displayq.c,v 1.29 2006/01/04 15:32:50 garbled Exp $");
38 1.10 mikel #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.4 cgd #include <sys/param.h>
42 1.4 cgd #include <sys/stat.h>
43 1.11 mrg #include <sys/file.h>
44 1.1 cgd
45 1.4 cgd #include <signal.h>
46 1.4 cgd #include <fcntl.h>
47 1.4 cgd #include <dirent.h>
48 1.4 cgd #include <unistd.h>
49 1.4 cgd #include <stdio.h>
50 1.4 cgd #include <stdlib.h>
51 1.4 cgd #include <string.h>
52 1.4 cgd #include <ctype.h>
53 1.1 cgd #include "lp.h"
54 1.4 cgd #include "lp.local.h"
55 1.1 cgd #include "pathnames.h"
56 1.1 cgd
57 1.4 cgd /*
58 1.4 cgd * Routines to display the state of the queue.
59 1.4 cgd */
60 1.1 cgd #define JOBCOL 40 /* column for job # in -l format */
61 1.1 cgd #define OWNCOL 7 /* start of Owner column in normal */
62 1.1 cgd #define SIZCOL 62 /* start of Size column in normal */
63 1.1 cgd
64 1.1 cgd /*
65 1.1 cgd * Stuff for handling job specifications
66 1.1 cgd */
67 1.1 cgd extern int requ[]; /* job number of spool entries */
68 1.1 cgd extern int requests; /* # of spool requests */
69 1.4 cgd extern char *user[]; /* users to process */
70 1.4 cgd extern int users; /* # of users in user array */
71 1.1 cgd
72 1.5 hpeyerl extern uid_t uid, euid;
73 1.5 hpeyerl
74 1.4 cgd static int col; /* column on screen */
75 1.21 itojun static char current[MAXPATHLEN]; /* current file being printed */
76 1.28 christos static char fname[MAXPATHLEN]; /* print file name */
77 1.4 cgd static int first; /* first file in ``files'' column? */
78 1.4 cgd static int garbage; /* # of garbage cf files */
79 1.4 cgd static int lflag; /* long output option */
80 1.4 cgd static int rank; /* order to be printed (-1=none, 0=active) */
81 1.4 cgd static long totsize; /* total print job size in bytes */
82 1.1 cgd
83 1.18 is static const char head0[] = "Rank Owner Job Files";
84 1.18 is static const char head1[] = "Total Size\n";
85 1.1 cgd
86 1.24 wiz static void alarmer(int);
87 1.17 mrg
88 1.17 mrg int wait_time = 300; /* time out after 5 minutes by default */
89 1.17 mrg
90 1.1 cgd /*
91 1.1 cgd * Display the current state of the queue. Format = 1 if long format.
92 1.1 cgd */
93 1.4 cgd void
94 1.24 wiz displayq(int format)
95 1.1 cgd {
96 1.12 mrg struct queue *q;
97 1.12 mrg int i, nitems, fd, ret;
98 1.29 garbled char *cp, *ecp, *rmhost;
99 1.1 cgd struct queue **queue;
100 1.1 cgd struct stat statb;
101 1.1 cgd FILE *fp;
102 1.1 cgd
103 1.1 cgd lflag = format;
104 1.1 cgd totsize = 0;
105 1.1 cgd rank = -1;
106 1.28 christos getprintcap(printer);
107 1.1 cgd
108 1.1 cgd /*
109 1.1 cgd * Print out local queue
110 1.1 cgd * Find all the control files in the spooling directory
111 1.1 cgd */
112 1.5 hpeyerl seteuid(euid);
113 1.1 cgd if (chdir(SD) < 0)
114 1.1 cgd fatal("cannot chdir to spooling directory");
115 1.5 hpeyerl seteuid(uid);
116 1.1 cgd if ((nitems = getq(&queue)) < 0)
117 1.1 cgd fatal("cannot examine spooling area\n");
118 1.5 hpeyerl seteuid(euid);
119 1.5 hpeyerl ret = stat(LO, &statb);
120 1.5 hpeyerl seteuid(uid);
121 1.5 hpeyerl if (ret >= 0) {
122 1.23 itojun if (statb.st_mode & S_IXUSR) {
123 1.11 mrg if (remote)
124 1.1 cgd printf("%s: ", host);
125 1.1 cgd printf("Warning: %s is down: ", printer);
126 1.5 hpeyerl seteuid(euid);
127 1.1 cgd fd = open(ST, O_RDONLY);
128 1.5 hpeyerl seteuid(uid);
129 1.1 cgd if (fd >= 0) {
130 1.8 mrg (void)flock(fd, LOCK_SH);
131 1.1 cgd while ((i = read(fd, line, sizeof(line))) > 0)
132 1.16 mrg (void)fwrite(line, 1, (size_t)i, stdout);
133 1.8 mrg (void)close(fd); /* unlocks as well */
134 1.1 cgd } else
135 1.1 cgd putchar('\n');
136 1.1 cgd }
137 1.23 itojun if (statb.st_mode & S_IXGRP) {
138 1.11 mrg if (remote)
139 1.1 cgd printf("%s: ", host);
140 1.1 cgd printf("Warning: %s queue is turned off\n", printer);
141 1.1 cgd }
142 1.1 cgd }
143 1.1 cgd
144 1.1 cgd if (nitems) {
145 1.5 hpeyerl seteuid(euid);
146 1.1 cgd fp = fopen(LO, "r");
147 1.5 hpeyerl seteuid(uid);
148 1.1 cgd if (fp == NULL)
149 1.12 mrg nodaemon();
150 1.1 cgd else {
151 1.1 cgd /* get daemon pid */
152 1.1 cgd cp = current;
153 1.21 itojun ecp = cp + sizeof(current) - 1;
154 1.21 itojun while ((i = getc(fp)) != EOF && i != '\n') {
155 1.21 itojun if (cp < ecp)
156 1.21 itojun *cp++ = i;
157 1.21 itojun }
158 1.1 cgd *cp = '\0';
159 1.1 cgd i = atoi(current);
160 1.5 hpeyerl if (i <= 0) {
161 1.6 pk ret = -1;
162 1.6 pk } else {
163 1.5 hpeyerl seteuid(euid);
164 1.5 hpeyerl ret = kill(i, 0);
165 1.5 hpeyerl seteuid(uid);
166 1.5 hpeyerl }
167 1.5 hpeyerl if (ret < 0) {
168 1.12 mrg nodaemon();
169 1.6 pk } else {
170 1.1 cgd /* read current file name */
171 1.1 cgd cp = current;
172 1.21 itojun ecp = cp + sizeof(current) - 1;
173 1.21 itojun while ((i = getc(fp)) != EOF && i != '\n') {
174 1.21 itojun if (cp < ecp)
175 1.21 itojun *cp++ = i;
176 1.21 itojun }
177 1.1 cgd *cp = '\0';
178 1.1 cgd /*
179 1.1 cgd * Print the status file.
180 1.1 cgd */
181 1.11 mrg if (remote)
182 1.1 cgd printf("%s: ", host);
183 1.5 hpeyerl seteuid(euid);
184 1.1 cgd fd = open(ST, O_RDONLY);
185 1.5 hpeyerl seteuid(uid);
186 1.1 cgd if (fd >= 0) {
187 1.8 mrg (void)flock(fd, LOCK_SH);
188 1.1 cgd while ((i = read(fd, line, sizeof(line))) > 0)
189 1.16 mrg (void)fwrite(line, 1, (size_t)i, stdout);
190 1.8 mrg (void)close(fd); /* unlocks as well */
191 1.1 cgd } else
192 1.1 cgd putchar('\n');
193 1.1 cgd }
194 1.8 mrg (void)fclose(fp);
195 1.1 cgd }
196 1.1 cgd /*
197 1.1 cgd * Now, examine the control files and print out the jobs to
198 1.1 cgd * be done for each user.
199 1.1 cgd */
200 1.1 cgd if (!lflag)
201 1.1 cgd header();
202 1.1 cgd for (i = 0; i < nitems; i++) {
203 1.1 cgd q = queue[i];
204 1.1 cgd inform(q->q_name);
205 1.1 cgd free(q);
206 1.1 cgd }
207 1.1 cgd free(queue);
208 1.1 cgd }
209 1.11 mrg if (!remote) {
210 1.1 cgd if (nitems == 0)
211 1.1 cgd puts("no entries");
212 1.1 cgd return;
213 1.1 cgd }
214 1.1 cgd
215 1.1 cgd /*
216 1.1 cgd * Print foreign queue
217 1.1 cgd * Note that a file in transit may show up in either queue.
218 1.1 cgd */
219 1.1 cgd if (nitems)
220 1.1 cgd putchar('\n');
221 1.8 mrg (void)snprintf(line, sizeof(line), "%c%s", format + '\3', RP);
222 1.1 cgd cp = line;
223 1.25 itojun ecp = line + sizeof(line);
224 1.25 itojun for (i = 0; i < requests && cp - line + 11 < sizeof(line) - 2; i++) {
225 1.1 cgd cp += strlen(cp);
226 1.25 itojun (void)snprintf(cp, ecp - cp, " %d", requ[i]);
227 1.1 cgd }
228 1.12 mrg for (i = 0; i < users && cp - line + 1 + strlen(user[i]) <
229 1.25 itojun sizeof(line) - 2; i++) {
230 1.1 cgd cp += strlen(cp);
231 1.25 itojun if (cp - line > sizeof(line) - 2)
232 1.8 mrg break;
233 1.1 cgd *cp++ = ' ';
234 1.25 itojun /* truncation may happen */
235 1.25 itojun (void)strlcpy(cp, user[i], ecp - cp);
236 1.1 cgd }
237 1.25 itojun (void)strlcat(line, "\n", sizeof(line));
238 1.29 garbled if ((rmhost = strchr(RM, '@')))
239 1.29 garbled fd = getport(rmhost+1, atoi(RM));
240 1.29 garbled else
241 1.29 garbled fd = getport(RM, 0);
242 1.1 cgd if (fd < 0) {
243 1.1 cgd if (from != host)
244 1.1 cgd printf("%s: ", host);
245 1.8 mrg (void)printf("connection to %s is down\n", RM);
246 1.1 cgd }
247 1.1 cgd else {
248 1.17 mrg struct sigaction osa, nsa;
249 1.17 mrg
250 1.1 cgd i = strlen(line);
251 1.16 mrg if (write(fd, line, (size_t)i) != i)
252 1.1 cgd fatal("Lost connection");
253 1.17 mrg nsa.sa_handler = alarmer;
254 1.17 mrg sigemptyset(&nsa.sa_mask);
255 1.17 mrg sigaddset(&nsa.sa_mask, SIGALRM);
256 1.17 mrg nsa.sa_flags = 0;
257 1.17 mrg (void)sigaction(SIGALRM, &nsa, &osa);
258 1.17 mrg alarm(wait_time);
259 1.17 mrg while ((i = read(fd, line, sizeof(line))) > 0) {
260 1.16 mrg (void)fwrite(line, 1, (size_t)i, stdout);
261 1.17 mrg alarm(wait_time);
262 1.17 mrg }
263 1.17 mrg alarm(0);
264 1.17 mrg (void)sigaction(SIGALRM, &osa, NULL);
265 1.8 mrg (void)close(fd);
266 1.1 cgd }
267 1.17 mrg }
268 1.17 mrg
269 1.17 mrg static void
270 1.24 wiz alarmer(int s)
271 1.17 mrg {
272 1.17 mrg /* nothing */
273 1.1 cgd }
274 1.1 cgd
275 1.1 cgd /*
276 1.1 cgd * Print a warning message if there is no daemon present.
277 1.1 cgd */
278 1.4 cgd void
279 1.24 wiz nodaemon(void)
280 1.1 cgd {
281 1.11 mrg if (remote)
282 1.1 cgd printf("\n%s: ", host);
283 1.1 cgd puts("Warning: no daemon present");
284 1.1 cgd current[0] = '\0';
285 1.1 cgd }
286 1.1 cgd
287 1.1 cgd /*
288 1.1 cgd * Print the header for the short listing format
289 1.1 cgd */
290 1.4 cgd void
291 1.24 wiz header(void)
292 1.1 cgd {
293 1.1 cgd printf(head0);
294 1.1 cgd col = strlen(head0)+1;
295 1.1 cgd blankfill(SIZCOL);
296 1.1 cgd printf(head1);
297 1.1 cgd }
298 1.1 cgd
299 1.4 cgd void
300 1.28 christos inform(const char *cf)
301 1.1 cgd {
302 1.12 mrg int j;
303 1.1 cgd FILE *cfp;
304 1.1 cgd
305 1.1 cgd /*
306 1.1 cgd * There's a chance the control file has gone away
307 1.1 cgd * in the meantime; if this is the case just keep going
308 1.1 cgd */
309 1.5 hpeyerl seteuid(euid);
310 1.1 cgd if ((cfp = fopen(cf, "r")) == NULL)
311 1.1 cgd return;
312 1.5 hpeyerl seteuid(uid);
313 1.1 cgd
314 1.1 cgd if (rank < 0)
315 1.1 cgd rank = 0;
316 1.11 mrg if (remote || garbage || strcmp(cf, current))
317 1.1 cgd rank++;
318 1.1 cgd j = 0;
319 1.1 cgd while (getline(cfp)) {
320 1.1 cgd switch (line[0]) {
321 1.1 cgd case 'P': /* Was this file specified in the user's list? */
322 1.1 cgd if (!inlist(line+1, cf)) {
323 1.1 cgd fclose(cfp);
324 1.1 cgd return;
325 1.1 cgd }
326 1.1 cgd if (lflag) {
327 1.1 cgd printf("\n%s: ", line+1);
328 1.1 cgd col = strlen(line+1) + 2;
329 1.1 cgd prank(rank);
330 1.1 cgd blankfill(JOBCOL);
331 1.1 cgd printf(" [job %s]\n", cf+3);
332 1.1 cgd } else {
333 1.1 cgd col = 0;
334 1.1 cgd prank(rank);
335 1.1 cgd blankfill(OWNCOL);
336 1.1 cgd printf("%-10s %-3d ", line+1, atoi(cf+3));
337 1.1 cgd col += 16;
338 1.1 cgd first = 1;
339 1.1 cgd }
340 1.1 cgd continue;
341 1.1 cgd default: /* some format specifer and file name? */
342 1.1 cgd if (line[0] < 'a' || line[0] > 'z')
343 1.1 cgd continue;
344 1.28 christos if (j == 0 || strcmp(fname, line+1) != 0) {
345 1.28 christos (void)strlcpy(fname, line+1, sizeof(fname));
346 1.12 mrg }
347 1.1 cgd j++;
348 1.1 cgd continue;
349 1.1 cgd case 'N':
350 1.28 christos show(line + 1, fname, j);
351 1.28 christos fname[0] = '\0';
352 1.1 cgd j = 0;
353 1.1 cgd }
354 1.1 cgd }
355 1.1 cgd fclose(cfp);
356 1.1 cgd if (!lflag) {
357 1.1 cgd blankfill(SIZCOL);
358 1.1 cgd printf("%ld bytes\n", totsize);
359 1.1 cgd totsize = 0;
360 1.1 cgd }
361 1.1 cgd }
362 1.1 cgd
363 1.4 cgd int
364 1.28 christos inlist(const char *name, const char *file)
365 1.1 cgd {
366 1.12 mrg int *r, n;
367 1.28 christos char **u;
368 1.28 christos const char *cp;
369 1.1 cgd
370 1.1 cgd if (users == 0 && requests == 0)
371 1.1 cgd return(1);
372 1.1 cgd /*
373 1.1 cgd * Check to see if it's in the user list
374 1.1 cgd */
375 1.1 cgd for (u = user; u < &user[users]; u++)
376 1.1 cgd if (!strcmp(*u, name))
377 1.1 cgd return(1);
378 1.1 cgd /*
379 1.1 cgd * Check the request list
380 1.1 cgd */
381 1.27 dsl for (n = 0, cp = file+3; isdigit((unsigned char)*cp); )
382 1.1 cgd n = n * 10 + (*cp++ - '0');
383 1.1 cgd for (r = requ; r < &requ[requests]; r++)
384 1.1 cgd if (*r == n && !strcmp(cp, from))
385 1.1 cgd return(1);
386 1.1 cgd return(0);
387 1.1 cgd }
388 1.1 cgd
389 1.4 cgd void
390 1.28 christos show(const char *nfile, const char *file, int copies)
391 1.1 cgd {
392 1.1 cgd if (strcmp(nfile, " ") == 0)
393 1.1 cgd nfile = "(standard input)";
394 1.1 cgd if (lflag)
395 1.1 cgd ldump(nfile, file, copies);
396 1.1 cgd else
397 1.1 cgd dump(nfile, file, copies);
398 1.1 cgd }
399 1.1 cgd
400 1.1 cgd /*
401 1.1 cgd * Fill the line with blanks to the specified column
402 1.1 cgd */
403 1.4 cgd void
404 1.24 wiz blankfill(int n)
405 1.1 cgd {
406 1.1 cgd while (col++ < n)
407 1.1 cgd putchar(' ');
408 1.1 cgd }
409 1.1 cgd
410 1.1 cgd /*
411 1.1 cgd * Give the abbreviated dump of the file names
412 1.1 cgd */
413 1.4 cgd void
414 1.28 christos dump(const char *nfile, const char *file, int copies)
415 1.1 cgd {
416 1.12 mrg short n, fill;
417 1.1 cgd struct stat lbuf;
418 1.1 cgd
419 1.1 cgd /*
420 1.1 cgd * Print as many files as will fit
421 1.1 cgd * (leaving room for the total size)
422 1.1 cgd */
423 1.1 cgd fill = first ? 0 : 2; /* fill space for ``, '' */
424 1.1 cgd if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) {
425 1.1 cgd if (col < SIZCOL) {
426 1.1 cgd printf(" ..."), col += 4;
427 1.1 cgd blankfill(SIZCOL);
428 1.1 cgd }
429 1.1 cgd } else {
430 1.1 cgd if (first)
431 1.1 cgd first = 0;
432 1.1 cgd else
433 1.1 cgd printf(", ");
434 1.1 cgd printf("%s", nfile);
435 1.1 cgd col += n+fill;
436 1.1 cgd }
437 1.5 hpeyerl seteuid(euid);
438 1.1 cgd if (*file && !stat(file, &lbuf))
439 1.16 mrg totsize += copies * (long)lbuf.st_size;
440 1.5 hpeyerl seteuid(uid);
441 1.1 cgd }
442 1.1 cgd
443 1.1 cgd /*
444 1.1 cgd * Print the long info about the file
445 1.1 cgd */
446 1.4 cgd void
447 1.28 christos ldump(const char *nfile, const char *file, int copies)
448 1.1 cgd {
449 1.1 cgd struct stat lbuf;
450 1.1 cgd
451 1.1 cgd putchar('\t');
452 1.1 cgd if (copies > 1)
453 1.1 cgd printf("%-2d copies of %-19s", copies, nfile);
454 1.1 cgd else
455 1.1 cgd printf("%-32s", nfile);
456 1.1 cgd if (*file && !stat(file, &lbuf))
457 1.19 lukem printf(" %lld bytes", (long long)lbuf.st_size);
458 1.1 cgd else
459 1.1 cgd printf(" ??? bytes");
460 1.1 cgd putchar('\n');
461 1.1 cgd }
462 1.1 cgd
463 1.1 cgd /*
464 1.1 cgd * Print the job's rank in the queue,
465 1.1 cgd * update col for screen management
466 1.1 cgd */
467 1.4 cgd void
468 1.24 wiz prank(int n)
469 1.1 cgd {
470 1.4 cgd char rline[100];
471 1.28 christos static const char *r[] = {
472 1.1 cgd "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
473 1.1 cgd };
474 1.1 cgd
475 1.1 cgd if (n == 0) {
476 1.1 cgd printf("active");
477 1.1 cgd col += 6;
478 1.1 cgd return;
479 1.1 cgd }
480 1.1 cgd if ((n/10)%10 == 1)
481 1.4 cgd (void)snprintf(rline, sizeof(rline), "%dth", n);
482 1.1 cgd else
483 1.4 cgd (void)snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]);
484 1.4 cgd col += strlen(rline);
485 1.4 cgd printf("%s", rline);
486 1.1 cgd }
487