2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)displayq.c 8.4 (Berkeley) 4/28/95";
38 static const char rcsid[] =
39 "$FreeBSD: src/usr.sbin/lpr/common_source/displayq.c,v 1.15.2.8 2001/08/30 09:27:41 kris Exp $";
42 #include <sys/param.h>
53 #define psignal foil_gcc_psignal
54 #define sys_siglist foil_gcc_siglist
61 #include "pathnames.h"
64 * Routines to display the state of the queue.
66 #define JOBCOL 40 /* column for job # in -l format */
67 #define OWNCOL 7 /* start of Owner column in normal */
68 #define SIZCOL 62 /* start of Size column in normal */
71 * Stuff for handling job specifications
73 extern uid_t uid, euid;
75 static int col; /* column on screen */
76 static char current[MAXNAMLEN+1]; /* current file being printed */
77 static char file[MAXNAMLEN+1]; /* print file name */
78 static int first; /* first file in ``files'' column? */
79 static int garbage; /* # of garbage cf files */
80 static int lflag; /* long output option */
81 static int rank; /* order to be printed (-1=none, 0=active) */
82 static long totsize; /* total print job size in bytes */
84 static const char *head0 = "Rank Owner Job Files";
85 static const char *head1 = "Total Size\n";
87 static void alarmhandler(int _signo);
88 static void warn(const struct printer *_pp);
91 * Display the current state of the queue. Format = 1 if long format.
94 displayq(struct printer *pp, int format)
96 register struct jobqueue *q;
97 register int i, nitems, fd, ret;
99 struct jobqueue **queue;
102 void (*savealrm)(int);
108 if ((cp = checkremote(pp))) {
109 printf("Warning: %s\n", cp);
114 * Print out local queue
115 * Find all the control files in the spooling directory
118 if (chdir(pp->spool_dir) < 0)
119 fatal(pp, "cannot chdir to spooling directory: %s",
122 if ((nitems = getq(pp, &queue)) < 0)
123 fatal(pp, "cannot examine spooling area\n");
125 ret = stat(pp->lock_file, &statb);
128 if (statb.st_mode & LFM_PRINT_DIS) {
130 printf("%s: ", local_host);
131 printf("Warning: %s is down: ", pp->printer);
133 fd = open(pp->status_file, O_RDONLY|O_SHLOCK);
136 while ((i = read(fd, line, sizeof(line))) > 0)
137 (void) fwrite(line, 1, i, stdout);
138 (void) close(fd); /* unlocks as well */
142 if (statb.st_mode & LFM_QUEUE_DIS) {
144 printf("%s: ", local_host);
145 printf("Warning: %s queue is turned off\n",
152 fp = fopen(pp->lock_file, "r");
159 endp = cp + sizeof(current) - 1;
160 while ((i = getc(fp)) != EOF && i != '\n') {
176 /* read current file name */
178 endp = cp + sizeof(current) - 1;
179 while ((i = getc(fp)) != EOF && i != '\n') {
185 * Print the status file.
188 printf("%s: ", local_host);
190 fd = open(pp->status_file, O_RDONLY|O_SHLOCK);
193 while ((i = read(fd, line,
195 fwrite(line, 1, i, stdout);
196 close(fd); /* unlocks as well */
203 * Now, examine the control files and print out the jobs to
204 * be done for each user.
208 for (i = 0; i < nitems; i++) {
210 inform(pp, q->job_cfname);
222 * Print foreign queue
223 * Note that a file in transit may show up in either queue.
227 (void) snprintf(line, sizeof(line), "%c%s", format ? '\4' : '\3',
230 for (i = 0; i < requests && cp-line+10 < sizeof(line) - 1; i++) {
232 (void) sprintf(cp, " %d", requ[i]);
234 for (i = 0; i < users && cp - line + 1 + strlen(user[i]) <
235 sizeof(line) - 1; i++) {
238 (void) strcpy(cp, user[i]);
241 savealrm = signal(SIGALRM, alarmhandler);
242 alarm(pp->conn_timeout);
243 fd = getport(pp, pp->remote_host, 0);
245 (void)signal(SIGALRM, savealrm);
247 if (from_host != local_host)
248 printf("%s: ", local_host);
249 printf("connection to %s is down\n", pp->remote_host);
253 if (write(fd, line, i) != i)
254 fatal(pp, "Lost connection");
255 while ((i = read(fd, line, sizeof(line))) > 0)
256 (void) fwrite(line, 1, i, stdout);
262 * Print a warning message if there is no daemon present.
265 warn(const struct printer *pp)
268 printf("%s: ", local_host);
269 puts("Warning: no daemon present");
274 * Print the header for the short listing format
280 col = strlen(head0)+1;
286 inform(const struct printer *pp, char *cf)
288 register int copycnt;
289 char savedname[MAXPATHLEN+1];
293 * There's a chance the control file has gone away
294 * in the meantime; if this is the case just keep going
297 if ((cfp = fopen(cf, "r")) == NULL)
303 if (pp->remote || garbage || strcmp(cf, current))
307 * The cf-file may include commands to print more than one datafile
308 * from the user. For each datafile, the cf-file contains at least
309 * one line which starts with some format-specifier ('a'-'z'), and
310 * a second line ('N'ame) which indicates the original name the user
311 * specified for that file. There can be multiple format-spec lines
312 * for a single Name-line, if the user requested multiple copies of
313 * that file. Standard lpr puts the format-spec line(s) before the
314 * Name-line, while lprNG puts the Name-line before the format-spec
315 * line(s). This section needs to handle the lines in either order.
320 while (getline(cfp)) {
322 case 'P': /* Was this file specified in the user's list? */
323 if (!inlist(line+1, cf)) {
328 printf("\n%s: ", line+1);
329 col = strlen(line+1) + 2;
332 printf(" [job %s]\n", cf+3);
337 printf("%-10s %-3d ", line+1, atoi(cf+3));
342 default: /* some format specifer and file name? */
343 if (line[0] < 'a' || line[0] > 'z')
345 if (copycnt == 0 || strcmp(file, line+1) != 0) {
346 strlcpy(file, line + 1, sizeof(file));
350 * deliberately 'continue' to another getline(), so
351 * all format-spec lines for this datafile are read
352 * in and counted before calling show()
356 strlcpy(savedname, line + 1, sizeof(savedname));
359 if ((file[0] != '\0') && (savedname[0] != '\0')) {
360 show(savedname, file, copycnt);
367 /* check for a file which hasn't been shown yet */
368 if (file[0] != '\0') {
369 if (savedname[0] == '\0') {
370 /* a safeguard in case the N-ame line is missing */
371 strlcpy(savedname, file, sizeof(savedname));
373 show(savedname, file, copycnt);
377 printf("%ld bytes\n", totsize);
383 inlist(char *uname, char *cfile)
386 register char **u, *cp;
388 if (users == 0 && requests == 0)
391 * Check to see if it's in the user list
393 for (u = user; u < &user[users]; u++)
394 if (!strcmp(*u, uname))
397 * Check the request list
399 for (n = 0, cp = cfile+3; isdigit(*cp); )
400 n = n * 10 + (*cp++ - '0');
401 for (r = requ; r < &requ[requests]; r++)
402 if (*r == n && !strcmp(cp, from_host))
408 show(const char *nfile, const char *datafile, int copies)
410 if (strcmp(nfile, " ") == 0)
411 nfile = "(standard input)";
413 ldump(nfile, datafile, copies);
415 dump(nfile, datafile, copies);
419 * Fill the line with blanks to the specified column
424 while (col++ < tocol)
429 * Give the abbreviated dump of the file names
432 dump(const char *nfile, const char *datafile, int copies)
435 const char etctmpl[] = ", ...";
436 char etc[sizeof(etctmpl)];
442 * Print as many filenames as will fit
443 * (leaving room for the 'total size' field)
445 fill = first ? 0 : 2; /* fill space for ``, '' */
446 nlen = strlen(nfile);
447 rem = SIZCOL - 1 - col;
448 if (nlen + fill > rem) {
450 /* print the right-most part of the name */
451 printf("...%s ", &nfile[3+nlen-rem]);
453 } else if (rem > 0) {
454 /* fit as much of the etc-string as we can */
456 if (rem > strlen(etctmpl))
457 remetc = strlen(etctmpl);
459 strncat(etc, etctmpl, remetc);
463 /* room for the last segment of this filename? */
464 lastsep = strrchr(nfile, '/');
465 if ((lastsep != NULL) && (rem > strlen(lastsep))) {
466 /* print the right-most part of this name */
467 printf("%s", lastsep);
468 col += strlen(lastsep);
470 /* do not pack any more names in here */
483 if (*datafile && !stat(datafile, &lbuf))
484 totsize += copies * lbuf.st_size;
489 * Print the long info about the file
492 ldump(const char *nfile, const char *datafile, int copies)
498 printf("%-2d copies of %-19s", copies, nfile);
500 printf("%-32s", nfile);
501 if (*datafile && !stat(datafile, &lbuf))
502 printf(" %qd bytes", (long long) lbuf.st_size);
504 printf(" ??? bytes");
509 * Print the job's rank in the queue,
510 * update col for screen management
516 static const char *r[] = {
517 "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
526 (void)snprintf(rline, sizeof(rline), "%dth", n);
528 (void)snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]);
529 col += strlen(rline);
534 alarmhandler(int signo __unused)
536 /* the signal is ignored */
537 /* (the '__unused' is just to avoid a compile-time warning) */