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";
40 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
54 #define psignal foil_gcc_psignal
55 #define sys_siglist foil_gcc_siglist
62 #include "pathnames.h"
65 * Routines to display the state of the queue.
67 #define JOBCOL 40 /* column for job # in -l format */
68 #define OWNCOL 7 /* start of Owner column in normal */
69 #define SIZCOL 62 /* start of Size column in normal */
72 * Stuff for handling job specifications
74 extern uid_t uid, euid;
76 static int col; /* column on screen */
77 static char current[MAXNAMLEN+1]; /* current file being printed */
78 static char file[MAXNAMLEN+1]; /* print file name */
79 static int first; /* first file in ``files'' column? */
80 static int garbage; /* # of garbage cf files */
81 static int lflag; /* long output option */
82 static int rank; /* order to be printed (-1=none, 0=active) */
83 static long totsize; /* total print job size in bytes */
85 static const char *head0 = "Rank Owner Job Files";
86 static const char *head1 = "Total Size\n";
88 static void alarmhandler(int _signo);
89 static void warn(const struct printer *_pp);
92 * Display the current state of the queue. Format = 1 if long format.
95 displayq(struct printer *pp, int format)
97 register struct jobqueue *q;
98 register int i, nitems, fd, ret;
100 struct jobqueue **queue;
103 void (*savealrm)(int);
109 if ((cp = checkremote(pp))) {
110 printf("Warning: %s\n", cp);
115 * Print out local queue
116 * Find all the control files in the spooling directory
119 if (chdir(pp->spool_dir) < 0)
120 fatal(pp, "cannot chdir to spooling directory: %s",
123 if ((nitems = getq(pp, &queue)) < 0)
124 fatal(pp, "cannot examine spooling area\n");
126 ret = stat(pp->lock_file, &statb);
129 if (statb.st_mode & LFM_PRINT_DIS) {
131 printf("%s: ", local_host);
132 printf("Warning: %s is down: ", pp->printer);
134 fd = open(pp->status_file, O_RDONLY|O_SHLOCK);
137 while ((i = read(fd, line, sizeof(line))) > 0)
138 (void) fwrite(line, 1, i, stdout);
139 (void) close(fd); /* unlocks as well */
143 if (statb.st_mode & LFM_QUEUE_DIS) {
145 printf("%s: ", local_host);
146 printf("Warning: %s queue is turned off\n",
153 fp = fopen(pp->lock_file, "r");
160 endp = cp + sizeof(current) - 1;
161 while ((i = getc(fp)) != EOF && i != '\n') {
177 /* read current file name */
179 endp = cp + sizeof(current) - 1;
180 while ((i = getc(fp)) != EOF && i != '\n') {
186 * Print the status file.
189 printf("%s: ", local_host);
191 fd = open(pp->status_file, O_RDONLY|O_SHLOCK);
194 while ((i = read(fd, line,
196 fwrite(line, 1, i, stdout);
197 close(fd); /* unlocks as well */
204 * Now, examine the control files and print out the jobs to
205 * be done for each user.
209 for (i = 0; i < nitems; i++) {
211 inform(pp, q->job_cfname);
223 * Print foreign queue
224 * Note that a file in transit may show up in either queue.
228 (void) snprintf(line, sizeof(line), "%c%s", format ? '\4' : '\3',
231 for (i = 0; i < requests && cp-line+10 < sizeof(line) - 1; i++) {
233 (void) sprintf(cp, " %d", requ[i]);
235 for (i = 0; i < users && cp - line + 1 + strlen(user[i]) <
236 sizeof(line) - 1; i++) {
239 (void) strcpy(cp, user[i]);
242 savealrm = signal(SIGALRM, alarmhandler);
243 alarm(pp->conn_timeout);
244 fd = getport(pp, pp->remote_host, 0);
246 (void)signal(SIGALRM, savealrm);
248 if (from_host != local_host)
249 printf("%s: ", local_host);
250 printf("connection to %s is down\n", pp->remote_host);
254 if (write(fd, line, i) != i)
255 fatal(pp, "Lost connection");
256 while ((i = read(fd, line, sizeof(line))) > 0)
257 (void) fwrite(line, 1, i, stdout);
263 * Print a warning message if there is no daemon present.
266 warn(const struct printer *pp)
269 printf("%s: ", local_host);
270 puts("Warning: no daemon present");
275 * Print the header for the short listing format
281 col = strlen(head0)+1;
287 inform(const struct printer *pp, char *cf)
290 char savedname[MAXPATHLEN+1];
294 * There's a chance the control file has gone away
295 * in the meantime; if this is the case just keep going
298 if ((cfp = fopen(cf, "r")) == NULL)
304 if (pp->remote || garbage || strcmp(cf, current))
308 * The cf-file may include commands to print more than one datafile
309 * from the user. For each datafile, the cf-file contains at least
310 * one line which starts with some format-specifier ('a'-'z'), and
311 * a second line ('N'ame) which indicates the original name the user
312 * specified for that file. There can be multiple format-spec lines
313 * for a single Name-line, if the user requested multiple copies of
314 * that file. Standard lpr puts the format-spec line(s) before the
315 * Name-line, while lprNG puts the Name-line before the format-spec
316 * line(s). This section needs to handle the lines in either order.
321 jnum = calc_jobnum(cf, NULL);
322 while (getline(cfp)) {
324 case 'P': /* Was this file specified in the user's list? */
325 if (!inlist(line+1, cf)) {
330 printf("\n%s: ", line+1);
331 col = strlen(line+1) + 2;
334 printf(" [job %s]\n", cf+3);
339 printf("%-10s %-3d ", line+1, jnum);
344 default: /* some format specifer and file name? */
345 if (line[0] < 'a' || line[0] > 'z')
347 if (copycnt == 0 || strcmp(file, line+1) != 0) {
348 strlcpy(file, line + 1, sizeof(file));
352 * deliberately 'continue' to another getline(), so
353 * all format-spec lines for this datafile are read
354 * in and counted before calling show()
358 strlcpy(savedname, line + 1, sizeof(savedname));
361 if ((file[0] != '\0') && (savedname[0] != '\0')) {
362 show(savedname, file, copycnt);
369 /* check for a file which hasn't been shown yet */
370 if (file[0] != '\0') {
371 if (savedname[0] == '\0') {
372 /* a safeguard in case the N-ame line is missing */
373 strlcpy(savedname, file, sizeof(savedname));
375 show(savedname, file, copycnt);
379 printf("%ld bytes\n", totsize);
385 inlist(char *uname, char *cfile)
391 if (users == 0 && requests == 0)
394 * Check to see if it's in the user list
396 for (u = user; u < &user[users]; u++)
397 if (!strcmp(*u, uname))
400 * Check the request list
402 jnum = calc_jobnum(cfile, &cfhost);
403 for (r = requ; r < &requ[requests]; r++)
404 if (*r == jnum && !strcmp(cfhost, from_host))
410 show(const char *nfile, const char *datafile, int copies)
412 if (strcmp(nfile, " ") == 0)
413 nfile = "(standard input)";
415 ldump(nfile, datafile, copies);
417 dump(nfile, datafile, copies);
421 * Fill the line with blanks to the specified column
426 while (col++ < tocol)
431 * Give the abbreviated dump of the file names
434 dump(const char *nfile, const char *datafile, int copies)
437 const char etctmpl[] = ", ...";
438 char etc[sizeof(etctmpl)];
444 * Print as many filenames as will fit
445 * (leaving room for the 'total size' field)
447 fill = first ? 0 : 2; /* fill space for ``, '' */
448 nlen = strlen(nfile);
449 rem = SIZCOL - 1 - col;
450 if (nlen + fill > rem) {
452 /* print the right-most part of the name */
453 printf("...%s ", &nfile[3+nlen-rem]);
455 } else if (rem > 0) {
456 /* fit as much of the etc-string as we can */
458 if (rem > strlen(etctmpl))
459 remetc = strlen(etctmpl);
461 strncat(etc, etctmpl, remetc);
465 /* room for the last segment of this filename? */
466 lastsep = strrchr(nfile, '/');
467 if ((lastsep != NULL) && (rem > strlen(lastsep))) {
468 /* print the right-most part of this name */
469 printf("%s", lastsep);
470 col += strlen(lastsep);
472 /* do not pack any more names in here */
485 if (*datafile && !stat(datafile, &lbuf))
486 totsize += copies * lbuf.st_size;
491 * Print the long info about the file
494 ldump(const char *nfile, const char *datafile, int copies)
500 printf("%-2d copies of %-19s", copies, nfile);
502 printf("%-32s", nfile);
503 if (*datafile && !stat(datafile, &lbuf))
504 printf(" %qd bytes", (long long) lbuf.st_size);
506 printf(" ??? bytes");
511 * Print the job's rank in the queue,
512 * update col for screen management
518 static const char *r[] = {
519 "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
528 (void)snprintf(rline, sizeof(rline), "%dth", n);
530 (void)snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]);
531 col += strlen(rline);
536 alarmhandler(int signo __unused)
538 /* the signal is ignored */
539 /* (the '__unused' is just to avoid a compile-time warning) */