2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)displayq.c 8.4 (Berkeley) 4/28/95";
38 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
39 __FBSDID("$FreeBSD$");
41 #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 * isprint() takes a parameter of 'int', but expect values in the range
72 * of unsigned char. Define a wrapper which takes a value of type 'char',
73 * whether signed or unsigned, and ensure it ends up in the right range.
75 #define isprintch(Anychar) isprint((u_char)(Anychar))
78 * Stuff for handling job specifications
80 static int col; /* column on screen */
81 static char current[MAXNAMLEN+1]; /* current file being printed */
82 static char file[MAXNAMLEN+1]; /* print file name */
83 static int first; /* first file in ``files'' column? */
84 static int garbage; /* # of garbage cf files */
85 static int lflag; /* long output option */
86 static int rank; /* order to be printed (-1=none, 0=active) */
87 static long totsize; /* total print job size in bytes */
89 static const char *head0 = "Rank Owner Job Files";
90 static const char *head1 = "Total Size\n";
92 static void alarmhandler(int _signo);
93 static void filtered_write(char *_obuffer, int _wlen, FILE *_wstream);
94 static void daemonwarn(const struct printer *_pp);
97 * Display the current state of the queue. Format = 1 if long format.
100 displayq(struct printer *pp, int format)
102 register struct jobqueue *q;
103 register int i, nitems, fd, ret;
105 struct jobqueue **queue;
108 void (*savealrm)(int);
114 if ((cp = checkremote(pp))) {
115 printf("Warning: %s\n", cp);
120 * Print out local queue
121 * Find all the control files in the spooling directory
124 if (chdir(pp->spool_dir) < 0)
125 fatal(pp, "cannot chdir to spooling directory: %s",
128 if ((nitems = getq(pp, &queue)) < 0)
129 fatal(pp, "cannot examine spooling area\n");
131 ret = stat(pp->lock_file, &statb);
134 if (statb.st_mode & LFM_PRINT_DIS) {
136 printf("%s: ", local_host);
137 printf("Warning: %s is down: ", pp->printer);
139 fd = open(pp->status_file, O_RDONLY|O_SHLOCK);
142 while ((i = read(fd, line, sizeof(line))) > 0)
143 (void) fwrite(line, 1, i, stdout);
144 (void) close(fd); /* unlocks as well */
148 if (statb.st_mode & LFM_QUEUE_DIS) {
150 printf("%s: ", local_host);
151 printf("Warning: %s queue is turned off\n",
158 fp = fopen(pp->lock_file, "r");
165 endp = cp + sizeof(current) - 1;
166 while ((i = getc(fp)) != EOF && i != '\n') {
182 /* read current file name */
184 endp = cp + sizeof(current) - 1;
185 while ((i = getc(fp)) != EOF && i != '\n') {
191 * Print the status file.
194 printf("%s: ", local_host);
196 fd = open(pp->status_file, O_RDONLY|O_SHLOCK);
199 while ((i = read(fd, line,
201 fwrite(line, 1, i, stdout);
202 close(fd); /* unlocks as well */
209 * Now, examine the control files and print out the jobs to
210 * be done for each user.
214 for (i = 0; i < nitems; i++) {
216 inform(pp, q->job_cfname);
228 * Print foreign queue
229 * Note that a file in transit may show up in either queue.
233 (void) snprintf(line, sizeof(line), "%c%s", format ? '\4' : '\3',
236 for (i = 0; i < requests && cp-line+10 < sizeof(line) - 1; i++) {
238 (void) sprintf(cp, " %d", requ[i]);
240 for (i = 0; i < users && cp - line + 1 + strlen(user[i]) <
241 sizeof(line) - 1; i++) {
244 (void) strcpy(cp, user[i]);
247 savealrm = signal(SIGALRM, alarmhandler);
248 alarm(pp->conn_timeout);
249 fd = getport(pp, pp->remote_host, 0);
251 (void)signal(SIGALRM, savealrm);
253 if (from_host != local_host)
254 printf("%s: ", local_host);
255 printf("connection to %s is down\n", pp->remote_host);
259 if (write(fd, line, i) != i)
260 fatal(pp, "Lost connection");
261 while ((i = read(fd, line, sizeof(line))) > 0)
262 filtered_write(line, i, stdout);
263 filtered_write(NULL, -1, stdout);
269 * The lpq-info read from remote hosts may contain unprintable characters,
270 * or carriage-returns instead of line-feeds. Clean those up before echoing
271 * the lpq-info line(s) to stdout. The info may also be missing any kind of
272 * end-of-line character. This also turns CRLF and LFCR into a plain LF.
274 * This routine may be called multiple times to process a single set of
275 * information, and after a set is finished this routine must be called
276 * one extra time with NULL specified as the buffer address.
279 filtered_write(char *wbuffer, int wlen, FILE *wstream)
281 static char lastchar, savedchar;
282 char *chkptr, *dest_end, *dest_ch, *nxtptr, *w_end;
284 char destbuf[BUFSIZ];
286 if (wbuffer == NULL) {
287 if (savedchar != '\0') {
288 if (savedchar == '\r')
290 fputc(savedchar, wstream);
291 lastchar = savedchar;
294 if (lastchar != '\0' && lastchar != '\n')
295 fputc('\n', wstream);
300 dest_ch = &destbuf[0];
301 dest_end = dest_ch + sizeof(destbuf);
303 w_end = wbuffer + wlen;
305 if (savedchar != '\0') {
311 while (chkptr < w_end) {
312 if (nxtptr < w_end) {
313 if ((*chkptr == '\r' && *nxtptr == '\n') ||
314 (*chkptr == '\n' && *nxtptr == '\r')) {
316 /* want to skip past that second character */
321 /* This is the last byte in the buffer given on this
322 * call, so check if it could be the first-byte of a
323 * significant two-byte sequence. If it is, then
324 * don't write it out now, but save for checking in
328 if (*chkptr == '\r' || *chkptr == '\n') {
335 #if 0 /* XXX - don't translate unprintable characters (yet) */
336 else if (*chkptr != '\t' && *chkptr != '\n' &&
341 *dest_ch++ = *chkptr;
346 if (dest_ch >= dest_end) {
347 destlen = dest_ch - &destbuf[0];
348 fwrite(destbuf, 1, destlen, wstream);
349 lastchar = destbuf[destlen - 1];
350 dest_ch = &destbuf[0];
353 destlen = dest_ch - &destbuf[0];
355 fwrite(destbuf, 1, destlen, wstream);
356 lastchar = destbuf[destlen - 1];
361 * Print a warning message if there is no daemon present.
364 daemonwarn(const struct printer *pp)
367 printf("%s: ", local_host);
368 puts("Warning: no daemon present");
373 * Print the header for the short listing format
379 col = strlen(head0)+1;
385 inform(const struct printer *pp, char *cf)
388 char savedname[MAXPATHLEN+1];
392 * There's a chance the control file has gone away
393 * in the meantime; if this is the case just keep going
396 if ((cfp = fopen(cf, "r")) == NULL)
402 if (pp->remote || garbage || strcmp(cf, current))
406 * The cf-file may include commands to print more than one datafile
407 * from the user. For each datafile, the cf-file contains at least
408 * one line which starts with some format-specifier ('a'-'z'), and
409 * a second line ('N'ame) which indicates the original name the user
410 * specified for that file. There can be multiple format-spec lines
411 * for a single Name-line, if the user requested multiple copies of
412 * that file. Standard lpr puts the format-spec line(s) before the
413 * Name-line, while lprNG puts the Name-line before the format-spec
414 * line(s). This section needs to handle the lines in either order.
419 jnum = calc_jobnum(cf, NULL);
420 while (get_line(cfp)) {
422 case 'P': /* Was this file specified in the user's list? */
423 if (!inlist(line+1, cf)) {
428 printf("\n%s: ", line+1);
429 col = strlen(line+1) + 2;
432 printf(" [job %s]\n", cf+3);
437 printf("%-10s %-3d ", line+1, jnum);
442 default: /* some format specifer and file name? */
443 if (line[0] < 'a' || line[0] > 'z')
445 if (copycnt == 0 || strcmp(file, line+1) != 0) {
446 strlcpy(file, line + 1, sizeof(file));
450 * deliberately 'continue' to another get_line(), so
451 * all format-spec lines for this datafile are read
452 * in and counted before calling show()
456 strlcpy(savedname, line + 1, sizeof(savedname));
459 if ((file[0] != '\0') && (savedname[0] != '\0')) {
460 show(savedname, file, copycnt);
467 /* check for a file which hasn't been shown yet */
468 if (file[0] != '\0') {
469 if (savedname[0] == '\0') {
470 /* a safeguard in case the N-ame line is missing */
471 strlcpy(savedname, file, sizeof(savedname));
473 show(savedname, file, copycnt);
477 printf("%ld bytes\n", totsize);
483 inlist(char *uname, char *cfile)
489 if (users == 0 && requests == 0)
492 * Check to see if it's in the user list
494 for (u = user; u < &user[users]; u++)
495 if (!strcmp(*u, uname))
498 * Check the request list
500 jnum = calc_jobnum(cfile, &cfhost);
501 for (r = requ; r < &requ[requests]; r++)
502 if (*r == jnum && !strcmp(cfhost, from_host))
508 show(const char *nfile, const char *datafile, int copies)
510 if (strcmp(nfile, " ") == 0)
511 nfile = "(standard input)";
513 ldump(nfile, datafile, copies);
515 dump(nfile, datafile, copies);
519 * Fill the line with blanks to the specified column
524 while (col++ < tocol)
529 * Give the abbreviated dump of the file names
532 dump(const char *nfile, const char *datafile, int copies)
535 const char etctmpl[] = ", ...";
536 char etc[sizeof(etctmpl)];
542 * Print as many filenames as will fit
543 * (leaving room for the 'total size' field)
545 fill = first ? 0 : 2; /* fill space for ``, '' */
546 nlen = strlen(nfile);
547 rem = SIZCOL - 1 - col;
548 if (nlen + fill > rem) {
550 /* print the right-most part of the name */
551 printf("...%s ", &nfile[3+nlen-rem]);
553 } else if (rem > 0) {
554 /* fit as much of the etc-string as we can */
556 if (rem > strlen(etctmpl))
557 remetc = strlen(etctmpl);
559 strncat(etc, etctmpl, remetc);
563 /* room for the last segment of this filename? */
564 lastsep = strrchr(nfile, '/');
565 if ((lastsep != NULL) && (rem > strlen(lastsep))) {
566 /* print the right-most part of this name */
567 printf("%s", lastsep);
568 col += strlen(lastsep);
570 /* do not pack any more names in here */
583 if (*datafile && !stat(datafile, &lbuf))
584 totsize += copies * lbuf.st_size;
589 * Print the long info about the file
592 ldump(const char *nfile, const char *datafile, int copies)
598 printf("%-2d copies of %-19s", copies, nfile);
600 printf("%-32s", nfile);
601 if (*datafile && !stat(datafile, &lbuf))
602 printf(" %qd bytes", (long long) lbuf.st_size);
604 printf(" ??? bytes");
609 * Print the job's rank in the queue,
610 * update col for screen management
616 static const char *r[] = {
617 "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
626 (void)snprintf(rline, sizeof(rline), "%dth", n);
628 (void)snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]);
629 col += strlen(rline);
634 alarmhandler(int signo __unused)
636 /* the signal is ignored */
637 /* (the '__unused' is just to avoid a compile-time warning) */