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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)displayq.c 8.4 (Berkeley) 4/28/95";
36 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
50 #define psignal foil_gcc_psignal
51 #define sys_siglist foil_gcc_siglist
58 #include "pathnames.h"
61 * Routines to display the state of the queue.
63 #define JOBCOL 40 /* column for job # in -l format */
64 #define OWNCOL 7 /* start of Owner column in normal */
65 #define SIZCOL 62 /* start of Size column in normal */
68 * isprint() takes a parameter of 'int', but expect values in the range
69 * of unsigned char. Define a wrapper which takes a value of type 'char',
70 * whether signed or unsigned, and ensure it ends up in the right range.
72 #define isprintch(Anychar) isprint((u_char)(Anychar))
75 * Stuff for handling job specifications
77 extern uid_t uid, euid;
79 static int col; /* column on screen */
80 static char current[MAXNAMLEN+1]; /* current file being printed */
81 static char file[MAXNAMLEN+1]; /* print file name */
82 static int first; /* first file in ``files'' column? */
83 static int garbage; /* # of garbage cf files */
84 static int lflag; /* long output option */
85 static int rank; /* order to be printed (-1=none, 0=active) */
86 static long totsize; /* total print job size in bytes */
88 static const char *head0 = "Rank Owner Job Files";
89 static const char *head1 = "Total Size\n";
91 static void alarmhandler(int _signo);
92 static void filtered_write(char *_obuffer, int _wlen, FILE *_wstream);
93 static void warn(const struct printer *_pp);
96 * Display the current state of the queue. Format = 1 if long format.
99 displayq(struct printer *pp, int format)
101 register struct jobqueue *q;
102 register int i, nitems, fd, ret;
104 struct jobqueue **queue;
107 void (*savealrm)(int);
113 if ((cp = checkremote(pp))) {
114 printf("Warning: %s\n", cp);
119 * Print out local queue
120 * Find all the control files in the spooling directory
123 if (chdir(pp->spool_dir) < 0)
124 fatal(pp, "cannot chdir to spooling directory: %s",
127 if ((nitems = getq(pp, &queue)) < 0)
128 fatal(pp, "cannot examine spooling area\n");
130 ret = stat(pp->lock_file, &statb);
133 if (statb.st_mode & LFM_PRINT_DIS) {
135 printf("%s: ", local_host);
136 printf("Warning: %s is down: ", pp->printer);
138 fd = open(pp->status_file, O_RDONLY|O_SHLOCK);
141 while ((i = read(fd, line, sizeof(line))) > 0)
142 (void) fwrite(line, 1, i, stdout);
143 (void) close(fd); /* unlocks as well */
147 if (statb.st_mode & LFM_QUEUE_DIS) {
149 printf("%s: ", local_host);
150 printf("Warning: %s queue is turned off\n",
157 fp = fopen(pp->lock_file, "r");
164 endp = cp + sizeof(current) - 1;
165 while ((i = getc(fp)) != EOF && i != '\n') {
181 /* read current file name */
183 endp = cp + sizeof(current) - 1;
184 while ((i = getc(fp)) != EOF && i != '\n') {
190 * Print the status file.
193 printf("%s: ", local_host);
195 fd = open(pp->status_file, O_RDONLY|O_SHLOCK);
198 while ((i = read(fd, line,
200 fwrite(line, 1, i, stdout);
201 close(fd); /* unlocks as well */
208 * Now, examine the control files and print out the jobs to
209 * be done for each user.
213 for (i = 0; i < nitems; i++) {
215 inform(pp, q->job_cfname);
227 * Print foreign queue
228 * Note that a file in transit may show up in either queue.
232 (void) snprintf(line, sizeof(line), "%c%s", format ? '\4' : '\3',
235 for (i = 0; i < requests && cp-line+10 < sizeof(line) - 1; i++) {
237 (void) sprintf(cp, " %d", requ[i]);
239 for (i = 0; i < users && cp - line + 1 + strlen(user[i]) <
240 sizeof(line) - 1; i++) {
243 (void) strcpy(cp, user[i]);
246 savealrm = signal(SIGALRM, alarmhandler);
247 alarm(pp->conn_timeout);
248 fd = getport(pp, pp->remote_host, 0);
250 (void)signal(SIGALRM, savealrm);
252 if (from_host != local_host)
253 printf("%s: ", local_host);
254 printf("connection to %s is down\n", pp->remote_host);
258 if (write(fd, line, i) != i)
259 fatal(pp, "Lost connection");
260 while ((i = read(fd, line, sizeof(line))) > 0)
261 filtered_write(line, i, stdout);
262 filtered_write(NULL, -1, stdout);
268 * The lpq-info read from remote hosts may contain unprintable characters,
269 * or carriage-returns instead of line-feeds. Clean those up before echoing
270 * the lpq-info line(s) to stdout. The info may also be missing any kind of
271 * end-of-line character. This also turns CRLF and LFCR into a plain LF.
273 * This routine may be called multiple times to process a single set of
274 * information, and after a set is finished this routine must be called
275 * one extra time with NULL specified as the buffer address.
278 filtered_write(char *wbuffer, int wlen, FILE *wstream)
280 static char lastchar, savedchar;
281 char *chkptr, *dest_end, *dest_ch, *nxtptr, *w_end;
283 char destbuf[BUFSIZ];
285 if (wbuffer == NULL) {
286 if (savedchar != '\0') {
287 if (savedchar == '\r')
289 fputc(savedchar, wstream);
290 lastchar = savedchar;
293 if (lastchar != '\0' && lastchar != '\n')
294 fputc('\n', wstream);
299 dest_ch = &destbuf[0];
300 dest_end = dest_ch + sizeof(destbuf);
302 w_end = wbuffer + wlen;
304 if (savedchar != '\0') {
310 while (chkptr < w_end) {
311 if (nxtptr < w_end) {
312 if ((*chkptr == '\r' && *nxtptr == '\n') ||
313 (*chkptr == '\n' && *nxtptr == '\r')) {
315 /* want to skip past that second character */
320 /* This is the last byte in the buffer given on this
321 * call, so check if it could be the first-byte of a
322 * significant two-byte sequence. If it is, then
323 * don't write it out now, but save for checking in
327 if (*chkptr == '\r' || *chkptr == '\n') {
334 #if 0 /* XXX - don't translate unprintable characters (yet) */
335 else if (*chkptr != '\t' && *chkptr != '\n' &&
340 *dest_ch++ = *chkptr;
345 if (dest_ch >= dest_end) {
346 destlen = dest_ch - &destbuf[0];
347 fwrite(destbuf, 1, destlen, wstream);
348 lastchar = destbuf[destlen - 1];
349 dest_ch = &destbuf[0];
352 destlen = dest_ch - &destbuf[0];
354 fwrite(destbuf, 1, destlen, wstream);
355 lastchar = destbuf[destlen - 1];
360 * Print a warning message if there is no daemon present.
363 warn(const struct printer *pp)
366 printf("%s: ", local_host);
367 puts("Warning: no daemon present");
372 * Print the header for the short listing format
378 col = strlen(head0)+1;
384 inform(const struct printer *pp, char *cf)
387 char savedname[MAXPATHLEN+1];
391 * There's a chance the control file has gone away
392 * in the meantime; if this is the case just keep going
395 if ((cfp = fopen(cf, "r")) == NULL)
401 if (pp->remote || garbage || strcmp(cf, current))
405 * The cf-file may include commands to print more than one datafile
406 * from the user. For each datafile, the cf-file contains at least
407 * one line which starts with some format-specifier ('a'-'z'), and
408 * a second line ('N'ame) which indicates the original name the user
409 * specified for that file. There can be multiple format-spec lines
410 * for a single Name-line, if the user requested multiple copies of
411 * that file. Standard lpr puts the format-spec line(s) before the
412 * Name-line, while lprNG puts the Name-line before the format-spec
413 * line(s). This section needs to handle the lines in either order.
418 jnum = calc_jobnum(cf, NULL);
419 while (getline(cfp)) {
421 case 'P': /* Was this file specified in the user's list? */
422 if (!inlist(line+1, cf)) {
427 printf("\n%s: ", line+1);
428 col = strlen(line+1) + 2;
431 printf(" [job %s]\n", cf+3);
436 printf("%-10s %-3d ", line+1, jnum);
441 default: /* some format specifer and file name? */
442 if (line[0] < 'a' || line[0] > 'z')
444 if (copycnt == 0 || strcmp(file, line+1) != 0) {
445 strlcpy(file, line + 1, sizeof(file));
449 * deliberately 'continue' to another getline(), so
450 * all format-spec lines for this datafile are read
451 * in and counted before calling show()
455 strlcpy(savedname, line + 1, sizeof(savedname));
458 if ((file[0] != '\0') && (savedname[0] != '\0')) {
459 show(savedname, file, copycnt);
466 /* check for a file which hasn't been shown yet */
467 if (file[0] != '\0') {
468 if (savedname[0] == '\0') {
469 /* a safeguard in case the N-ame line is missing */
470 strlcpy(savedname, file, sizeof(savedname));
472 show(savedname, file, copycnt);
476 printf("%ld bytes\n", totsize);
482 inlist(char *uname, char *cfile)
488 if (users == 0 && requests == 0)
491 * Check to see if it's in the user list
493 for (u = user; u < &user[users]; u++)
494 if (!strcmp(*u, uname))
497 * Check the request list
499 jnum = calc_jobnum(cfile, &cfhost);
500 for (r = requ; r < &requ[requests]; r++)
501 if (*r == jnum && !strcmp(cfhost, from_host))
507 show(const char *nfile, const char *datafile, int copies)
509 if (strcmp(nfile, " ") == 0)
510 nfile = "(standard input)";
512 ldump(nfile, datafile, copies);
514 dump(nfile, datafile, copies);
518 * Fill the line with blanks to the specified column
523 while (col++ < tocol)
528 * Give the abbreviated dump of the file names
531 dump(const char *nfile, const char *datafile, int copies)
534 const char etctmpl[] = ", ...";
535 char etc[sizeof(etctmpl)];
541 * Print as many filenames as will fit
542 * (leaving room for the 'total size' field)
544 fill = first ? 0 : 2; /* fill space for ``, '' */
545 nlen = strlen(nfile);
546 rem = SIZCOL - 1 - col;
547 if (nlen + fill > rem) {
549 /* print the right-most part of the name */
550 printf("...%s ", &nfile[3+nlen-rem]);
552 } else if (rem > 0) {
553 /* fit as much of the etc-string as we can */
555 if (rem > strlen(etctmpl))
556 remetc = strlen(etctmpl);
558 strncat(etc, etctmpl, remetc);
562 /* room for the last segment of this filename? */
563 lastsep = strrchr(nfile, '/');
564 if ((lastsep != NULL) && (rem > strlen(lastsep))) {
565 /* print the right-most part of this name */
566 printf("%s", lastsep);
567 col += strlen(lastsep);
569 /* do not pack any more names in here */
582 if (*datafile && !stat(datafile, &lbuf))
583 totsize += copies * lbuf.st_size;
588 * Print the long info about the file
591 ldump(const char *nfile, const char *datafile, int copies)
597 printf("%-2d copies of %-19s", copies, nfile);
599 printf("%-32s", nfile);
600 if (*datafile && !stat(datafile, &lbuf))
601 printf(" %qd bytes", (long long) lbuf.st_size);
603 printf(" ??? bytes");
608 * Print the job's rank in the queue,
609 * update col for screen management
615 static const char *r[] = {
616 "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
625 (void)snprintf(rline, sizeof(rline), "%dth", n);
627 (void)snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]);
628 col += strlen(rline);
633 alarmhandler(int signo __unused)
635 /* the signal is ignored */
636 /* (the '__unused' is just to avoid a compile-time warning) */