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 * isprint() takes a parameter of 'int', but expect values in the range
73 * of unsigned char. Define a wrapper which takes a value of type 'char',
74 * whether signed or unsigned, and ensure it ends up in the right range.
76 #define isprintch(Anychar) isprint((u_char)(Anychar))
79 * Stuff for handling job specifications
81 extern uid_t uid, euid;
83 static int col; /* column on screen */
84 static char current[MAXNAMLEN+1]; /* current file being printed */
85 static char file[MAXNAMLEN+1]; /* print file name */
86 static int first; /* first file in ``files'' column? */
87 static int garbage; /* # of garbage cf files */
88 static int lflag; /* long output option */
89 static int rank; /* order to be printed (-1=none, 0=active) */
90 static long totsize; /* total print job size in bytes */
92 static const char *head0 = "Rank Owner Job Files";
93 static const char *head1 = "Total Size\n";
95 static void alarmhandler(int _signo);
96 static void filtered_write(char *_obuffer, int _wlen, FILE *_wstream);
97 static void warn(const struct printer *_pp);
100 * Display the current state of the queue. Format = 1 if long format.
103 displayq(struct printer *pp, int format)
105 register struct jobqueue *q;
106 register int i, nitems, fd, ret;
108 struct jobqueue **queue;
111 void (*savealrm)(int);
117 if ((cp = checkremote(pp))) {
118 printf("Warning: %s\n", cp);
123 * Print out local queue
124 * Find all the control files in the spooling directory
127 if (chdir(pp->spool_dir) < 0)
128 fatal(pp, "cannot chdir to spooling directory: %s",
131 if ((nitems = getq(pp, &queue)) < 0)
132 fatal(pp, "cannot examine spooling area\n");
134 ret = stat(pp->lock_file, &statb);
137 if (statb.st_mode & LFM_PRINT_DIS) {
139 printf("%s: ", local_host);
140 printf("Warning: %s is down: ", pp->printer);
142 fd = open(pp->status_file, O_RDONLY|O_SHLOCK);
145 while ((i = read(fd, line, sizeof(line))) > 0)
146 (void) fwrite(line, 1, i, stdout);
147 (void) close(fd); /* unlocks as well */
151 if (statb.st_mode & LFM_QUEUE_DIS) {
153 printf("%s: ", local_host);
154 printf("Warning: %s queue is turned off\n",
161 fp = fopen(pp->lock_file, "r");
168 endp = cp + sizeof(current) - 1;
169 while ((i = getc(fp)) != EOF && i != '\n') {
185 /* read current file name */
187 endp = cp + sizeof(current) - 1;
188 while ((i = getc(fp)) != EOF && i != '\n') {
194 * Print the status file.
197 printf("%s: ", local_host);
199 fd = open(pp->status_file, O_RDONLY|O_SHLOCK);
202 while ((i = read(fd, line,
204 fwrite(line, 1, i, stdout);
205 close(fd); /* unlocks as well */
212 * Now, examine the control files and print out the jobs to
213 * be done for each user.
217 for (i = 0; i < nitems; i++) {
219 inform(pp, q->job_cfname);
231 * Print foreign queue
232 * Note that a file in transit may show up in either queue.
236 (void) snprintf(line, sizeof(line), "%c%s", format ? '\4' : '\3',
239 for (i = 0; i < requests && cp-line+10 < sizeof(line) - 1; i++) {
241 (void) sprintf(cp, " %d", requ[i]);
243 for (i = 0; i < users && cp - line + 1 + strlen(user[i]) <
244 sizeof(line) - 1; i++) {
247 (void) strcpy(cp, user[i]);
250 savealrm = signal(SIGALRM, alarmhandler);
251 alarm(pp->conn_timeout);
252 fd = getport(pp, pp->remote_host, 0);
254 (void)signal(SIGALRM, savealrm);
256 if (from_host != local_host)
257 printf("%s: ", local_host);
258 printf("connection to %s is down\n", pp->remote_host);
262 if (write(fd, line, i) != i)
263 fatal(pp, "Lost connection");
264 while ((i = read(fd, line, sizeof(line))) > 0)
265 filtered_write(line, i, stdout);
266 filtered_write(NULL, -1, stdout);
272 * The lpq-info read from remote hosts may contain unprintable characters,
273 * or carriage-returns instead of line-feeds. Clean those up before echoing
274 * the lpq-info line(s) to stdout. The info may also be missing any kind of
275 * end-of-line character. This also turns CRLF and LFCR into a plain LF.
277 * This routine may be called multiple times to process a single set of
278 * information, and after a set is finished this routine must be called
279 * one extra time with NULL specified as the buffer address.
282 filtered_write(char *wbuffer, int wlen, FILE *wstream)
284 static char lastchar, savedchar;
285 char *chkptr, *dest_end, *dest_ch, *nxtptr, *w_end;
287 char destbuf[BUFSIZ];
289 if (wbuffer == NULL) {
290 if (savedchar != '\0') {
291 if (savedchar == '\r')
293 fputc(savedchar, wstream);
294 lastchar = savedchar;
297 if (lastchar != '\0' && lastchar != '\n')
298 fputc('\n', wstream);
303 dest_ch = &destbuf[0];
304 dest_end = dest_ch + sizeof(destbuf);
306 w_end = wbuffer + wlen;
308 if (savedchar != '\0') {
314 while (chkptr < w_end) {
315 if (nxtptr < w_end) {
316 if ((*chkptr == '\r' && *nxtptr == '\n') ||
317 (*chkptr == '\n' && *nxtptr == '\r')) {
319 /* want to skip past that second character */
324 /* This is the last byte in the buffer given on this
325 * call, so check if it could be the first-byte of a
326 * significant two-byte sequence. If it is, then
327 * don't write it out now, but save for checking in
331 if (*chkptr == '\r' || *chkptr == '\n') {
338 #if 0 /* XXX - don't translate unprintable characters (yet) */
339 else if (*chkptr != '\t' && *chkptr != '\n' &&
344 *dest_ch++ = *chkptr;
349 if (dest_ch >= dest_end) {
350 destlen = dest_ch - &destbuf[0];
351 fwrite(destbuf, 1, destlen, wstream);
352 lastchar = destbuf[destlen - 1];
353 dest_ch = &destbuf[0];
356 destlen = dest_ch - &destbuf[0];
358 fwrite(destbuf, 1, destlen, wstream);
359 lastchar = destbuf[destlen - 1];
364 * Print a warning message if there is no daemon present.
367 warn(const struct printer *pp)
370 printf("%s: ", local_host);
371 puts("Warning: no daemon present");
376 * Print the header for the short listing format
382 col = strlen(head0)+1;
388 inform(const struct printer *pp, char *cf)
391 char savedname[MAXPATHLEN+1];
395 * There's a chance the control file has gone away
396 * in the meantime; if this is the case just keep going
399 if ((cfp = fopen(cf, "r")) == NULL)
405 if (pp->remote || garbage || strcmp(cf, current))
409 * The cf-file may include commands to print more than one datafile
410 * from the user. For each datafile, the cf-file contains at least
411 * one line which starts with some format-specifier ('a'-'z'), and
412 * a second line ('N'ame) which indicates the original name the user
413 * specified for that file. There can be multiple format-spec lines
414 * for a single Name-line, if the user requested multiple copies of
415 * that file. Standard lpr puts the format-spec line(s) before the
416 * Name-line, while lprNG puts the Name-line before the format-spec
417 * line(s). This section needs to handle the lines in either order.
422 jnum = calc_jobnum(cf, NULL);
423 while (getline(cfp)) {
425 case 'P': /* Was this file specified in the user's list? */
426 if (!inlist(line+1, cf)) {
431 printf("\n%s: ", line+1);
432 col = strlen(line+1) + 2;
435 printf(" [job %s]\n", cf+3);
440 printf("%-10s %-3d ", line+1, jnum);
445 default: /* some format specifer and file name? */
446 if (line[0] < 'a' || line[0] > 'z')
448 if (copycnt == 0 || strcmp(file, line+1) != 0) {
449 strlcpy(file, line + 1, sizeof(file));
453 * deliberately 'continue' to another getline(), so
454 * all format-spec lines for this datafile are read
455 * in and counted before calling show()
459 strlcpy(savedname, line + 1, sizeof(savedname));
462 if ((file[0] != '\0') && (savedname[0] != '\0')) {
463 show(savedname, file, copycnt);
470 /* check for a file which hasn't been shown yet */
471 if (file[0] != '\0') {
472 if (savedname[0] == '\0') {
473 /* a safeguard in case the N-ame line is missing */
474 strlcpy(savedname, file, sizeof(savedname));
476 show(savedname, file, copycnt);
480 printf("%ld bytes\n", totsize);
486 inlist(char *uname, char *cfile)
492 if (users == 0 && requests == 0)
495 * Check to see if it's in the user list
497 for (u = user; u < &user[users]; u++)
498 if (!strcmp(*u, uname))
501 * Check the request list
503 jnum = calc_jobnum(cfile, &cfhost);
504 for (r = requ; r < &requ[requests]; r++)
505 if (*r == jnum && !strcmp(cfhost, from_host))
511 show(const char *nfile, const char *datafile, int copies)
513 if (strcmp(nfile, " ") == 0)
514 nfile = "(standard input)";
516 ldump(nfile, datafile, copies);
518 dump(nfile, datafile, copies);
522 * Fill the line with blanks to the specified column
527 while (col++ < tocol)
532 * Give the abbreviated dump of the file names
535 dump(const char *nfile, const char *datafile, int copies)
538 const char etctmpl[] = ", ...";
539 char etc[sizeof(etctmpl)];
545 * Print as many filenames as will fit
546 * (leaving room for the 'total size' field)
548 fill = first ? 0 : 2; /* fill space for ``, '' */
549 nlen = strlen(nfile);
550 rem = SIZCOL - 1 - col;
551 if (nlen + fill > rem) {
553 /* print the right-most part of the name */
554 printf("...%s ", &nfile[3+nlen-rem]);
556 } else if (rem > 0) {
557 /* fit as much of the etc-string as we can */
559 if (rem > strlen(etctmpl))
560 remetc = strlen(etctmpl);
562 strncat(etc, etctmpl, remetc);
566 /* room for the last segment of this filename? */
567 lastsep = strrchr(nfile, '/');
568 if ((lastsep != NULL) && (rem > strlen(lastsep))) {
569 /* print the right-most part of this name */
570 printf("%s", lastsep);
571 col += strlen(lastsep);
573 /* do not pack any more names in here */
586 if (*datafile && !stat(datafile, &lbuf))
587 totsize += copies * lbuf.st_size;
592 * Print the long info about the file
595 ldump(const char *nfile, const char *datafile, int copies)
601 printf("%-2d copies of %-19s", copies, nfile);
603 printf("%-32s", nfile);
604 if (*datafile && !stat(datafile, &lbuf))
605 printf(" %qd bytes", (long long) lbuf.st_size);
607 printf(" ??? bytes");
612 * Print the job's rank in the queue,
613 * update col for screen management
619 static const char *r[] = {
620 "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
629 (void)snprintf(rline, sizeof(rline), "%dth", n);
631 (void)snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]);
632 col += strlen(rline);
637 alarmhandler(int signo __unused)
639 /* the signal is ignored */
640 /* (the '__unused' is just to avoid a compile-time warning) */