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