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. 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
29 * @(#)displayq.c 8.4 (Berkeley) 4/28/95
30 * $FreeBSD: src/usr.sbin/lpr/common_source/displayq.c,v 1.15.2.8 2001/08/30 09:27:41 kris Exp $
31 * $DragonFly: src/usr.sbin/lpr/common_source/displayq.c,v 1.5 2005/08/08 18:58:56 joerg Exp $
34 #include <sys/param.h>
45 #define psignal foil_gcc_psignal
46 #define sys_siglist foil_gcc_siglist
53 #include "pathnames.h"
56 * Routines to display the state of the queue.
58 #define JOBCOL 40 /* column for job # in -l format */
59 #define OWNCOL 7 /* start of Owner column in normal */
60 #define SIZCOL 62 /* start of Size column in normal */
63 * Stuff for handling job specifications
65 extern uid_t uid
, euid
;
67 static int col
; /* column on screen */
68 static char current
[NAME_MAX
+1]; /* current file being printed */
69 static char file
[NAME_MAX
+1]; /* print file name */
70 static int first
; /* first file in ``files'' column? */
71 static int garbage
; /* # of garbage cf files */
72 static int lflag
; /* long output option */
73 static int rank
; /* order to be printed (-1=none, 0=active) */
74 static long totsize
; /* total print job size in bytes */
76 static const char *head0
= "Rank Owner Job Files";
77 static const char *head1
= "Total Size\n";
79 static void alarmhandler(int _signo
);
80 static void warn(const struct printer
*_pp
);
83 * Display the current state of the queue. Format = 1 if long format.
86 displayq(struct printer
*pp
, int format
)
89 int i
, nitems
, fd
, ret
;
91 struct jobqueue
**queue
;
94 void (*savealrm
)(int);
100 if ((cp
= checkremote(pp
))) {
101 printf("Warning: %s\n", cp
);
106 * Print out local queue
107 * Find all the control files in the spooling directory
110 if (chdir(pp
->spool_dir
) < 0)
111 fatal(pp
, "cannot chdir to spooling directory: %s",
114 if ((nitems
= getq(pp
, &queue
)) < 0)
115 fatal(pp
, "cannot examine spooling area\n");
117 ret
= stat(pp
->lock_file
, &statb
);
120 if (statb
.st_mode
& LFM_PRINT_DIS
) {
122 printf("%s: ", local_host
);
123 printf("Warning: %s is down: ", pp
->printer
);
125 fd
= open(pp
->status_file
, O_RDONLY
|O_SHLOCK
);
128 while ((i
= read(fd
, line
, sizeof(line
))) > 0)
129 fwrite(line
, 1, i
, stdout
);
130 close(fd
); /* unlocks as well */
134 if (statb
.st_mode
& LFM_QUEUE_DIS
) {
136 printf("%s: ", local_host
);
137 printf("Warning: %s queue is turned off\n",
144 fp
= fopen(pp
->lock_file
, "r");
151 endp
= cp
+ sizeof(current
) - 1;
152 while ((i
= getc(fp
)) != EOF
&& i
!= '\n') {
168 /* read current file name */
170 endp
= cp
+ sizeof(current
) - 1;
171 while ((i
= getc(fp
)) != EOF
&& i
!= '\n') {
177 * Print the status file.
180 printf("%s: ", local_host
);
182 fd
= open(pp
->status_file
, O_RDONLY
|O_SHLOCK
);
185 while ((i
= read(fd
, line
,
187 fwrite(line
, 1, i
, stdout
);
188 close(fd
); /* unlocks as well */
195 * Now, examine the control files and print out the jobs to
196 * be done for each user.
200 for (i
= 0; i
< nitems
; i
++) {
202 inform(pp
, q
->job_cfname
);
214 * Print foreign queue
215 * Note that a file in transit may show up in either queue.
219 snprintf(line
, sizeof(line
), "%c%s", format
? '\4' : '\3',
222 for (i
= 0; i
< requests
&& cp
-line
+10 < sizeof(line
) - 1; i
++) {
224 sprintf(cp
, " %d", requ
[i
]);
226 for (i
= 0; i
< users
&& cp
- line
+ 1 + strlen(user
[i
]) <
227 sizeof(line
) - 1; i
++) {
233 savealrm
= signal(SIGALRM
, alarmhandler
);
234 alarm(pp
->conn_timeout
);
235 fd
= getport(pp
, pp
->remote_host
, 0);
237 signal(SIGALRM
, savealrm
);
239 if (from_host
!= local_host
)
240 printf("%s: ", local_host
);
241 printf("connection to %s is down\n", pp
->remote_host
);
245 if (write(fd
, line
, i
) != i
)
246 fatal(pp
, "Lost connection");
247 while ((i
= read(fd
, line
, sizeof(line
))) > 0)
248 fwrite(line
, 1, i
, stdout
);
254 * Print a warning message if there is no daemon present.
257 warn(const struct printer
*pp
)
260 printf("%s: ", local_host
);
261 puts("Warning: no daemon present");
266 * Print the header for the short listing format
272 col
= strlen(head0
)+1;
278 inform(const struct printer
*pp
, char *cf
)
281 char savedname
[MAXPATHLEN
+1];
285 * There's a chance the control file has gone away
286 * in the meantime; if this is the case just keep going
289 if ((cfp
= fopen(cf
, "r")) == NULL
)
295 if (pp
->remote
|| garbage
|| strcmp(cf
, current
))
299 * The cf-file may include commands to print more than one datafile
300 * from the user. For each datafile, the cf-file contains at least
301 * one line which starts with some format-specifier ('a'-'z'), and
302 * a second line ('N'ame) which indicates the original name the user
303 * specified for that file. There can be multiple format-spec lines
304 * for a single Name-line, if the user requested multiple copies of
305 * that file. Standard lpr puts the format-spec line(s) before the
306 * Name-line, while lprNG puts the Name-line before the format-spec
307 * line(s). This section needs to handle the lines in either order.
312 while (get_line(cfp
)) {
314 case 'P': /* Was this file specified in the user's list? */
315 if (!inlist(line
+1, cf
)) {
320 printf("\n%s: ", line
+1);
321 col
= strlen(line
+1) + 2;
324 printf(" [job %s]\n", cf
+3);
329 printf("%-10s %-3d ", line
+1, atoi(cf
+3));
334 default: /* some format specifer and file name? */
335 if (line
[0] < 'a' || line
[0] > 'z')
337 if (copycnt
== 0 || strcmp(file
, line
+1) != 0) {
338 strlcpy(file
, line
+ 1, sizeof(file
));
342 * deliberately 'continue' to another get_line(), so
343 * all format-spec lines for this datafile are read
344 * in and counted before calling show()
348 strlcpy(savedname
, line
+ 1, sizeof(savedname
));
351 if ((file
[0] != '\0') && (savedname
[0] != '\0')) {
352 show(savedname
, file
, copycnt
);
359 /* check for a file which hasn't been shown yet */
360 if (file
[0] != '\0') {
361 if (savedname
[0] == '\0') {
362 /* a safeguard in case the N-ame line is missing */
363 strlcpy(savedname
, file
, sizeof(savedname
));
365 show(savedname
, file
, copycnt
);
369 printf("%ld bytes\n", totsize
);
375 inlist(char *uname
, char *cfile
)
380 if (users
== 0 && requests
== 0)
383 * Check to see if it's in the user list
385 for (u
= user
; u
< &user
[users
]; u
++)
386 if (!strcmp(*u
, uname
))
389 * Check the request list
391 for (n
= 0, cp
= cfile
+3; isdigit(*cp
); )
392 n
= n
* 10 + (*cp
++ - '0');
393 for (r
= requ
; r
< &requ
[requests
]; r
++)
394 if (*r
== n
&& !strcmp(cp
, from_host
))
400 show(const char *nfile
, const char *datafile
, int copies
)
402 if (strcmp(nfile
, " ") == 0)
403 nfile
= "(standard input)";
405 ldump(nfile
, datafile
, copies
);
407 dump(nfile
, datafile
, copies
);
411 * Fill the line with blanks to the specified column
416 while (col
++ < tocol
)
421 * Give the abbreviated dump of the file names
424 dump(const char *nfile
, const char *datafile
, int copies
)
427 const char etctmpl
[] = ", ...";
428 char etc
[sizeof(etctmpl
)];
434 * Print as many filenames as will fit
435 * (leaving room for the 'total size' field)
437 fill
= first
? 0 : 2; /* fill space for ``, '' */
438 nlen
= strlen(nfile
);
439 rem
= SIZCOL
- 1 - col
;
440 if (nlen
+ fill
> rem
) {
442 /* print the right-most part of the name */
443 printf("...%s ", &nfile
[3+nlen
-rem
]);
445 } else if (rem
> 0) {
446 /* fit as much of the etc-string as we can */
448 if (rem
> strlen(etctmpl
))
449 remetc
= strlen(etctmpl
);
451 strncat(etc
, etctmpl
, remetc
);
455 /* room for the last segment of this filename? */
456 lastsep
= strrchr(nfile
, '/');
457 if ((lastsep
!= NULL
) && (rem
> strlen(lastsep
))) {
458 /* print the right-most part of this name */
459 printf("%s", lastsep
);
460 col
+= strlen(lastsep
);
462 /* do not pack any more names in here */
475 if (*datafile
&& !stat(datafile
, &lbuf
))
476 totsize
+= copies
* lbuf
.st_size
;
481 * Print the long info about the file
484 ldump(const char *nfile
, const char *datafile
, int copies
)
490 printf("%-2d copies of %-19s", copies
, nfile
);
492 printf("%-32s", nfile
);
493 if (*datafile
&& !stat(datafile
, &lbuf
))
494 printf(" %qd bytes", (long long) lbuf
.st_size
);
496 printf(" ??? bytes");
501 * Print the job's rank in the queue,
502 * update col for screen management
508 static const char *r
[] = {
509 "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
518 snprintf(rline
, sizeof(rline
), "%dth", n
);
520 snprintf(rline
, sizeof(rline
), "%d%s", n
, r
[n
%10]);
521 col
+= strlen(rline
);
526 alarmhandler(int signo __unused
)
528 /* the signal is ignored */
529 /* (the '__unused' is just to avoid a compile-time warning) */