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
33 * @(#)displayq.c 8.4 (Berkeley) 4/28/95
34 * $FreeBSD: src/usr.sbin/lpr/common_source/displayq.c,v 1.15.2.8 2001/08/30 09:27:41 kris Exp $
35 * $DragonFly: src/usr.sbin/lpr/common_source/displayq.c,v 1.4 2004/12/18 22:48:03 swildner Exp $
38 #include <sys/param.h>
49 #define psignal foil_gcc_psignal
50 #define sys_siglist foil_gcc_siglist
57 #include "pathnames.h"
60 * Routines to display the state of the queue.
62 #define JOBCOL 40 /* column for job # in -l format */
63 #define OWNCOL 7 /* start of Owner column in normal */
64 #define SIZCOL 62 /* start of Size column in normal */
67 * Stuff for handling job specifications
69 extern uid_t uid, euid;
71 static int col; /* column on screen */
72 static char current[MAXNAMLEN+1]; /* current file being printed */
73 static char file[MAXNAMLEN+1]; /* print file name */
74 static int first; /* first file in ``files'' column? */
75 static int garbage; /* # of garbage cf files */
76 static int lflag; /* long output option */
77 static int rank; /* order to be printed (-1=none, 0=active) */
78 static long totsize; /* total print job size in bytes */
80 static const char *head0 = "Rank Owner Job Files";
81 static const char *head1 = "Total Size\n";
83 static void alarmhandler(int _signo);
84 static void warn(const struct printer *_pp);
87 * Display the current state of the queue. Format = 1 if long format.
90 displayq(struct printer *pp, int format)
93 int i, nitems, fd, ret;
95 struct jobqueue **queue;
98 void (*savealrm)(int);
104 if ((cp = checkremote(pp))) {
105 printf("Warning: %s\n", cp);
110 * Print out local queue
111 * Find all the control files in the spooling directory
114 if (chdir(pp->spool_dir) < 0)
115 fatal(pp, "cannot chdir to spooling directory: %s",
118 if ((nitems = getq(pp, &queue)) < 0)
119 fatal(pp, "cannot examine spooling area\n");
121 ret = stat(pp->lock_file, &statb);
124 if (statb.st_mode & LFM_PRINT_DIS) {
126 printf("%s: ", local_host);
127 printf("Warning: %s is down: ", pp->printer);
129 fd = open(pp->status_file, O_RDONLY|O_SHLOCK);
132 while ((i = read(fd, line, sizeof(line))) > 0)
133 fwrite(line, 1, i, stdout);
134 close(fd); /* unlocks as well */
138 if (statb.st_mode & LFM_QUEUE_DIS) {
140 printf("%s: ", local_host);
141 printf("Warning: %s queue is turned off\n",
148 fp = fopen(pp->lock_file, "r");
155 endp = cp + sizeof(current) - 1;
156 while ((i = getc(fp)) != EOF && i != '\n') {
172 /* read current file name */
174 endp = cp + sizeof(current) - 1;
175 while ((i = getc(fp)) != EOF && i != '\n') {
181 * Print the status file.
184 printf("%s: ", local_host);
186 fd = open(pp->status_file, O_RDONLY|O_SHLOCK);
189 while ((i = read(fd, line,
191 fwrite(line, 1, i, stdout);
192 close(fd); /* unlocks as well */
199 * Now, examine the control files and print out the jobs to
200 * be done for each user.
204 for (i = 0; i < nitems; i++) {
206 inform(pp, q->job_cfname);
218 * Print foreign queue
219 * Note that a file in transit may show up in either queue.
223 snprintf(line, sizeof(line), "%c%s", format ? '\4' : '\3',
226 for (i = 0; i < requests && cp-line+10 < sizeof(line) - 1; i++) {
228 sprintf(cp, " %d", requ[i]);
230 for (i = 0; i < users && cp - line + 1 + strlen(user[i]) <
231 sizeof(line) - 1; i++) {
237 savealrm = signal(SIGALRM, alarmhandler);
238 alarm(pp->conn_timeout);
239 fd = getport(pp, pp->remote_host, 0);
241 signal(SIGALRM, savealrm);
243 if (from_host != local_host)
244 printf("%s: ", local_host);
245 printf("connection to %s is down\n", pp->remote_host);
249 if (write(fd, line, i) != i)
250 fatal(pp, "Lost connection");
251 while ((i = read(fd, line, sizeof(line))) > 0)
252 fwrite(line, 1, i, stdout);
258 * Print a warning message if there is no daemon present.
261 warn(const struct printer *pp)
264 printf("%s: ", local_host);
265 puts("Warning: no daemon present");
270 * Print the header for the short listing format
276 col = strlen(head0)+1;
282 inform(const struct printer *pp, char *cf)
285 char savedname[MAXPATHLEN+1];
289 * There's a chance the control file has gone away
290 * in the meantime; if this is the case just keep going
293 if ((cfp = fopen(cf, "r")) == NULL)
299 if (pp->remote || garbage || strcmp(cf, current))
303 * The cf-file may include commands to print more than one datafile
304 * from the user. For each datafile, the cf-file contains at least
305 * one line which starts with some format-specifier ('a'-'z'), and
306 * a second line ('N'ame) which indicates the original name the user
307 * specified for that file. There can be multiple format-spec lines
308 * for a single Name-line, if the user requested multiple copies of
309 * that file. Standard lpr puts the format-spec line(s) before the
310 * Name-line, while lprNG puts the Name-line before the format-spec
311 * line(s). This section needs to handle the lines in either order.
316 while (getline(cfp)) {
318 case 'P': /* Was this file specified in the user's list? */
319 if (!inlist(line+1, cf)) {
324 printf("\n%s: ", line+1);
325 col = strlen(line+1) + 2;
328 printf(" [job %s]\n", cf+3);
333 printf("%-10s %-3d ", line+1, atoi(cf+3));
338 default: /* some format specifer and file name? */
339 if (line[0] < 'a' || line[0] > 'z')
341 if (copycnt == 0 || strcmp(file, line+1) != 0) {
342 strlcpy(file, line + 1, sizeof(file));
346 * deliberately 'continue' to another getline(), so
347 * all format-spec lines for this datafile are read
348 * in and counted before calling show()
352 strlcpy(savedname, line + 1, sizeof(savedname));
355 if ((file[0] != '\0') && (savedname[0] != '\0')) {
356 show(savedname, file, copycnt);
363 /* check for a file which hasn't been shown yet */
364 if (file[0] != '\0') {
365 if (savedname[0] == '\0') {
366 /* a safeguard in case the N-ame line is missing */
367 strlcpy(savedname, file, sizeof(savedname));
369 show(savedname, file, copycnt);
373 printf("%ld bytes\n", totsize);
379 inlist(char *uname, char *cfile)
384 if (users == 0 && requests == 0)
387 * Check to see if it's in the user list
389 for (u = user; u < &user[users]; u++)
390 if (!strcmp(*u, uname))
393 * Check the request list
395 for (n = 0, cp = cfile+3; isdigit(*cp); )
396 n = n * 10 + (*cp++ - '0');
397 for (r = requ; r < &requ[requests]; r++)
398 if (*r == n && !strcmp(cp, from_host))
404 show(const char *nfile, const char *datafile, int copies)
406 if (strcmp(nfile, " ") == 0)
407 nfile = "(standard input)";
409 ldump(nfile, datafile, copies);
411 dump(nfile, datafile, copies);
415 * Fill the line with blanks to the specified column
420 while (col++ < tocol)
425 * Give the abbreviated dump of the file names
428 dump(const char *nfile, const char *datafile, int copies)
431 const char etctmpl[] = ", ...";
432 char etc[sizeof(etctmpl)];
438 * Print as many filenames as will fit
439 * (leaving room for the 'total size' field)
441 fill = first ? 0 : 2; /* fill space for ``, '' */
442 nlen = strlen(nfile);
443 rem = SIZCOL - 1 - col;
444 if (nlen + fill > rem) {
446 /* print the right-most part of the name */
447 printf("...%s ", &nfile[3+nlen-rem]);
449 } else if (rem > 0) {
450 /* fit as much of the etc-string as we can */
452 if (rem > strlen(etctmpl))
453 remetc = strlen(etctmpl);
455 strncat(etc, etctmpl, remetc);
459 /* room for the last segment of this filename? */
460 lastsep = strrchr(nfile, '/');
461 if ((lastsep != NULL) && (rem > strlen(lastsep))) {
462 /* print the right-most part of this name */
463 printf("%s", lastsep);
464 col += strlen(lastsep);
466 /* do not pack any more names in here */
479 if (*datafile && !stat(datafile, &lbuf))
480 totsize += copies * lbuf.st_size;
485 * Print the long info about the file
488 ldump(const char *nfile, const char *datafile, int copies)
494 printf("%-2d copies of %-19s", copies, nfile);
496 printf("%-32s", nfile);
497 if (*datafile && !stat(datafile, &lbuf))
498 printf(" %qd bytes", (long long) lbuf.st_size);
500 printf(" ??? bytes");
505 * Print the job's rank in the queue,
506 * update col for screen management
512 static const char *r[] = {
513 "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
522 snprintf(rline, sizeof(rline), "%dth", n);
524 snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]);
525 col += strlen(rline);
530 alarmhandler(int signo __unused)
532 /* the signal is ignored */
533 /* (the '__unused' is just to avoid a compile-time warning) */