xref: /freebsd/usr.sbin/lpr/common_source/displayq.c (revision cc3fd56f96f5726a445a8a6090bda47a94122df6)
10b561052SJoerg Wunsch /*
20b561052SJoerg Wunsch  * Copyright (c) 1983, 1993
30b561052SJoerg Wunsch  *	The Regents of the University of California.  All rights reserved.
40b561052SJoerg Wunsch  *
50b561052SJoerg Wunsch  * Redistribution and use in source and binary forms, with or without
60b561052SJoerg Wunsch  * modification, are permitted provided that the following conditions
70b561052SJoerg Wunsch  * are met:
80b561052SJoerg Wunsch  * 1. Redistributions of source code must retain the above copyright
90b561052SJoerg Wunsch  *    notice, this list of conditions and the following disclaimer.
100b561052SJoerg Wunsch  * 2. Redistributions in binary form must reproduce the above copyright
110b561052SJoerg Wunsch  *    notice, this list of conditions and the following disclaimer in the
120b561052SJoerg Wunsch  *    documentation and/or other materials provided with the distribution.
130b561052SJoerg Wunsch  * 3. All advertising materials mentioning features or use of this software
140b561052SJoerg Wunsch  *    must display the following acknowledgement:
150b561052SJoerg Wunsch  *	This product includes software developed by the University of
160b561052SJoerg Wunsch  *	California, Berkeley and its contributors.
170b561052SJoerg Wunsch  * 4. Neither the name of the University nor the names of its contributors
180b561052SJoerg Wunsch  *    may be used to endorse or promote products derived from this software
190b561052SJoerg Wunsch  *    without specific prior written permission.
200b561052SJoerg Wunsch  *
210b561052SJoerg Wunsch  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
220b561052SJoerg Wunsch  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
230b561052SJoerg Wunsch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
240b561052SJoerg Wunsch  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
250b561052SJoerg Wunsch  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
260b561052SJoerg Wunsch  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
270b561052SJoerg Wunsch  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
280b561052SJoerg Wunsch  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
290b561052SJoerg Wunsch  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
300b561052SJoerg Wunsch  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
310b561052SJoerg Wunsch  * SUCH DAMAGE.
320b561052SJoerg Wunsch  */
330b561052SJoerg Wunsch 
340b561052SJoerg Wunsch #ifndef lint
354a1a0dbeSGarrett Wollman /*
365458e2f4SJoerg Wunsch static char sccsid[] = "@(#)displayq.c	8.4 (Berkeley) 4/28/95";
374a1a0dbeSGarrett Wollman */
384a1a0dbeSGarrett Wollman static const char rcsid[] =
3997d92980SPeter Wemm   "$FreeBSD$";
400b561052SJoerg Wunsch #endif /* not lint */
410b561052SJoerg Wunsch 
420b561052SJoerg Wunsch #include <sys/param.h>
430b561052SJoerg Wunsch #include <sys/stat.h>
440b561052SJoerg Wunsch 
454a1a0dbeSGarrett Wollman #include <ctype.h>
460b561052SJoerg Wunsch #include <dirent.h>
474a1a0dbeSGarrett Wollman #include <errno.h>
484a1a0dbeSGarrett Wollman #include <fcntl.h>
494a1a0dbeSGarrett Wollman #include <signal.h>
500b561052SJoerg Wunsch #include <stdio.h>
510b561052SJoerg Wunsch #include <stdlib.h>
520b561052SJoerg Wunsch #include <string.h>
534a1a0dbeSGarrett Wollman #define psignal foil_gcc_psignal
544a1a0dbeSGarrett Wollman #define	sys_siglist foil_gcc_siglist
554a1a0dbeSGarrett Wollman #include <unistd.h>
564a1a0dbeSGarrett Wollman #undef psignal
574a1a0dbeSGarrett Wollman #undef sys_siglist
584a1a0dbeSGarrett Wollman 
590b561052SJoerg Wunsch #include "lp.h"
600b561052SJoerg Wunsch #include "lp.local.h"
610b561052SJoerg Wunsch #include "pathnames.h"
620b561052SJoerg Wunsch 
630b561052SJoerg Wunsch /*
640b561052SJoerg Wunsch  * Routines to display the state of the queue.
650b561052SJoerg Wunsch  */
660b561052SJoerg Wunsch #define JOBCOL	40		/* column for job # in -l format */
670b561052SJoerg Wunsch #define OWNCOL	7		/* start of Owner column in normal */
680b561052SJoerg Wunsch #define SIZCOL	62		/* start of Size column in normal */
690b561052SJoerg Wunsch 
700b561052SJoerg Wunsch /*
710b561052SJoerg Wunsch  * Stuff for handling job specifications
720b561052SJoerg Wunsch  */
73360d4ad5SWarner Losh extern uid_t	uid, euid;
74360d4ad5SWarner Losh 
750b561052SJoerg Wunsch static int	col;		/* column on screen */
760b561052SJoerg Wunsch static char	current[40];	/* current file being printed */
770b561052SJoerg Wunsch static char	file[132];	/* print file name */
780b561052SJoerg Wunsch static int	first;		/* first file in ``files'' column? */
790b561052SJoerg Wunsch static int	garbage;	/* # of garbage cf files */
800b561052SJoerg Wunsch static int	lflag;		/* long output option */
810b561052SJoerg Wunsch static int	rank;		/* order to be printed (-1=none, 0=active) */
820b561052SJoerg Wunsch static long	totsize;	/* total print job size in bytes */
830b561052SJoerg Wunsch 
84ba7a1ad7SGarance A Drosehn static const char  *head0 = "Rank   Owner      Job  Files";
85ba7a1ad7SGarance A Drosehn static const char  *head1 = "Total Size\n";
860b561052SJoerg Wunsch 
87ba7a1ad7SGarance A Drosehn static void	alarmhandler(int _signo);
88ba7a1ad7SGarance A Drosehn static void	warn(const struct printer *_pp);
89334a9508SJoerg Wunsch 
900b561052SJoerg Wunsch /*
910b561052SJoerg Wunsch  * Display the current state of the queue. Format = 1 if long format.
920b561052SJoerg Wunsch  */
930b561052SJoerg Wunsch void
94ba7a1ad7SGarance A Drosehn displayq(struct printer *pp, int format)
950b561052SJoerg Wunsch {
9630b4b758SGarance A Drosehn 	register struct jobqueue *q;
97360d4ad5SWarner Losh 	register int i, nitems, fd, ret;
980b561052SJoerg Wunsch 	register char	*cp;
9930b4b758SGarance A Drosehn 	struct jobqueue **queue;
1000b561052SJoerg Wunsch 	struct stat statb;
1010b561052SJoerg Wunsch 	FILE *fp;
102334a9508SJoerg Wunsch 	void (*savealrm)(int);
1030b561052SJoerg Wunsch 
1040b561052SJoerg Wunsch 	lflag = format;
1050b561052SJoerg Wunsch 	totsize = 0;
1060b561052SJoerg Wunsch 	rank = -1;
1074a1a0dbeSGarrett Wollman 
1084a1a0dbeSGarrett Wollman 	if ((cp = checkremote(pp))) {
1090b561052SJoerg Wunsch 		printf("Warning: %s\n", cp);
1104a1a0dbeSGarrett Wollman 		free(cp);
1114a1a0dbeSGarrett Wollman 	}
1120b561052SJoerg Wunsch 
1130b561052SJoerg Wunsch 	/*
1140b561052SJoerg Wunsch 	 * Print out local queue
1150b561052SJoerg Wunsch 	 * Find all the control files in the spooling directory
1160b561052SJoerg Wunsch 	 */
117360d4ad5SWarner Losh 	seteuid(euid);
1184a1a0dbeSGarrett Wollman 	if (chdir(pp->spool_dir) < 0)
1194a1a0dbeSGarrett Wollman 		fatal(pp, "cannot chdir to spooling directory: %s",
1204a1a0dbeSGarrett Wollman 		      strerror(errno));
121360d4ad5SWarner Losh 	seteuid(uid);
1224a1a0dbeSGarrett Wollman 	if ((nitems = getq(pp, &queue)) < 0)
1234a1a0dbeSGarrett Wollman 		fatal(pp, "cannot examine spooling area\n");
124360d4ad5SWarner Losh 	seteuid(euid);
1254a1a0dbeSGarrett Wollman 	ret = stat(pp->lock_file, &statb);
126360d4ad5SWarner Losh 	seteuid(uid);
127360d4ad5SWarner Losh 	if (ret >= 0) {
1284a1a0dbeSGarrett Wollman 		if (statb.st_mode & LFM_PRINT_DIS) {
1294a1a0dbeSGarrett Wollman 			if (pp->remote)
130cc3fd56fSGarance A Drosehn 				printf("%s: ", local_host);
1314a1a0dbeSGarrett Wollman 			printf("Warning: %s is down: ", pp->printer);
132360d4ad5SWarner Losh 			seteuid(euid);
1334a1a0dbeSGarrett Wollman 			fd = open(pp->status_file, O_RDONLY|O_SHLOCK);
134360d4ad5SWarner Losh 			seteuid(uid);
1350b561052SJoerg Wunsch 			if (fd >= 0) {
1360b561052SJoerg Wunsch 				while ((i = read(fd, line, sizeof(line))) > 0)
1370b561052SJoerg Wunsch 					(void) fwrite(line, 1, i, stdout);
1380b561052SJoerg Wunsch 				(void) close(fd);	/* unlocks as well */
1390b561052SJoerg Wunsch 			} else
1400b561052SJoerg Wunsch 				putchar('\n');
1410b561052SJoerg Wunsch 		}
1424a1a0dbeSGarrett Wollman 		if (statb.st_mode & LFM_QUEUE_DIS) {
1434a1a0dbeSGarrett Wollman 			if (pp->remote)
144cc3fd56fSGarance A Drosehn 				printf("%s: ", local_host);
1454a1a0dbeSGarrett Wollman 			printf("Warning: %s queue is turned off\n",
1464a1a0dbeSGarrett Wollman 			       pp->printer);
1470b561052SJoerg Wunsch 		}
1480b561052SJoerg Wunsch 	}
1490b561052SJoerg Wunsch 
1500b561052SJoerg Wunsch 	if (nitems) {
151360d4ad5SWarner Losh 		seteuid(euid);
1524a1a0dbeSGarrett Wollman 		fp = fopen(pp->lock_file, "r");
153360d4ad5SWarner Losh 		seteuid(uid);
1540b561052SJoerg Wunsch 		if (fp == NULL)
1554a1a0dbeSGarrett Wollman 			warn(pp);
1560b561052SJoerg Wunsch 		else {
1570b561052SJoerg Wunsch 			/* get daemon pid */
1580b561052SJoerg Wunsch 			cp = current;
1595458e2f4SJoerg Wunsch 			while ((i = getc(fp)) != EOF && i != '\n')
1605458e2f4SJoerg Wunsch 				*cp++ = i;
1610b561052SJoerg Wunsch 			*cp = '\0';
1620b561052SJoerg Wunsch 			i = atoi(current);
163360d4ad5SWarner Losh 			if (i <= 0) {
164360d4ad5SWarner Losh 				ret = -1;
165360d4ad5SWarner Losh 			} else {
166360d4ad5SWarner Losh 				seteuid(euid);
167360d4ad5SWarner Losh 				ret = kill(i, 0);
168360d4ad5SWarner Losh 				seteuid(uid);
169360d4ad5SWarner Losh 			}
170360d4ad5SWarner Losh 			if (ret < 0) {
1714a1a0dbeSGarrett Wollman 				warn(pp);
172360d4ad5SWarner Losh 			} else {
1730b561052SJoerg Wunsch 				/* read current file name */
1740b561052SJoerg Wunsch 				cp = current;
1755458e2f4SJoerg Wunsch 				while ((i = getc(fp)) != EOF && i != '\n')
1765458e2f4SJoerg Wunsch 					*cp++ = i;
1770b561052SJoerg Wunsch 				*cp = '\0';
1780b561052SJoerg Wunsch 				/*
1790b561052SJoerg Wunsch 				 * Print the status file.
1800b561052SJoerg Wunsch 				 */
1814a1a0dbeSGarrett Wollman 				if (pp->remote)
182cc3fd56fSGarance A Drosehn 					printf("%s: ", local_host);
183360d4ad5SWarner Losh 				seteuid(euid);
1844a1a0dbeSGarrett Wollman 				fd = open(pp->status_file, O_RDONLY|O_SHLOCK);
185360d4ad5SWarner Losh 				seteuid(uid);
1860b561052SJoerg Wunsch 				if (fd >= 0) {
1874a1a0dbeSGarrett Wollman 					while ((i = read(fd, line,
1884a1a0dbeSGarrett Wollman 							 sizeof(line))) > 0)
1894a1a0dbeSGarrett Wollman 						fwrite(line, 1, i, stdout);
1904a1a0dbeSGarrett Wollman 					close(fd);	/* unlocks as well */
1910b561052SJoerg Wunsch 				} else
1920b561052SJoerg Wunsch 					putchar('\n');
1930b561052SJoerg Wunsch 			}
1940b561052SJoerg Wunsch 			(void) fclose(fp);
1950b561052SJoerg Wunsch 		}
1960b561052SJoerg Wunsch 		/*
1970b561052SJoerg Wunsch 		 * Now, examine the control files and print out the jobs to
1980b561052SJoerg Wunsch 		 * be done for each user.
1990b561052SJoerg Wunsch 		 */
2000b561052SJoerg Wunsch 		if (!lflag)
2010b561052SJoerg Wunsch 			header();
2020b561052SJoerg Wunsch 		for (i = 0; i < nitems; i++) {
2030b561052SJoerg Wunsch 			q = queue[i];
20430b4b758SGarance A Drosehn 			inform(pp, q->job_cfname);
2050b561052SJoerg Wunsch 			free(q);
2060b561052SJoerg Wunsch 		}
2070b561052SJoerg Wunsch 		free(queue);
2080b561052SJoerg Wunsch 	}
2094a1a0dbeSGarrett Wollman 	if (!pp->remote) {
2100b561052SJoerg Wunsch 		if (nitems == 0)
2110b561052SJoerg Wunsch 			puts("no entries");
2120b561052SJoerg Wunsch 		return;
2130b561052SJoerg Wunsch 	}
2140b561052SJoerg Wunsch 
2150b561052SJoerg Wunsch 	/*
2160b561052SJoerg Wunsch 	 * Print foreign queue
2170b561052SJoerg Wunsch 	 * Note that a file in transit may show up in either queue.
2180b561052SJoerg Wunsch 	 */
2190b561052SJoerg Wunsch 	if (nitems)
2200b561052SJoerg Wunsch 		putchar('\n');
2214a1a0dbeSGarrett Wollman 	(void) snprintf(line, sizeof(line), "%c%s", format ? '\4' : '\3',
2224a1a0dbeSGarrett Wollman 			pp->remote_queue);
2230b561052SJoerg Wunsch 	cp = line;
2246ee8b269SWarner Losh 	for (i = 0; i < requests && cp-line+10 < sizeof(line) - 1; i++) {
2250b561052SJoerg Wunsch 		cp += strlen(cp);
2260b561052SJoerg Wunsch 		(void) sprintf(cp, " %d", requ[i]);
2270b561052SJoerg Wunsch 	}
228d583a7c3SWarner Losh 	for (i = 0; i < users && cp - line + 1 + strlen(user[i]) <
2296ee8b269SWarner Losh 		sizeof(line) - 1; i++) {
2300b561052SJoerg Wunsch 		cp += strlen(cp);
2310b561052SJoerg Wunsch 		*cp++ = ' ';
2320b561052SJoerg Wunsch 		(void) strcpy(cp, user[i]);
2330b561052SJoerg Wunsch 	}
2340b561052SJoerg Wunsch 	strcat(line, "\n");
235334a9508SJoerg Wunsch 	savealrm = signal(SIGALRM, alarmhandler);
2364a1a0dbeSGarrett Wollman 	alarm(pp->conn_timeout);
2374a1a0dbeSGarrett Wollman 	fd = getport(pp, pp->remote_host, 0);
2386d0727f4SJoerg Wunsch 	alarm(0);
239334a9508SJoerg Wunsch 	(void)signal(SIGALRM, savealrm);
2400b561052SJoerg Wunsch 	if (fd < 0) {
241cc3fd56fSGarance A Drosehn 		if (from_host != local_host)
242cc3fd56fSGarance A Drosehn 			printf("%s: ", local_host);
2434a1a0dbeSGarrett Wollman 		printf("connection to %s is down\n", pp->remote_host);
2440b561052SJoerg Wunsch 	}
2450b561052SJoerg Wunsch 	else {
2460b561052SJoerg Wunsch 		i = strlen(line);
2470b561052SJoerg Wunsch 		if (write(fd, line, i) != i)
2484a1a0dbeSGarrett Wollman 			fatal(pp, "Lost connection");
2490b561052SJoerg Wunsch 		while ((i = read(fd, line, sizeof(line))) > 0)
2500b561052SJoerg Wunsch 			(void) fwrite(line, 1, i, stdout);
2510b561052SJoerg Wunsch 		(void) close(fd);
2520b561052SJoerg Wunsch 	}
2530b561052SJoerg Wunsch }
2540b561052SJoerg Wunsch 
2550b561052SJoerg Wunsch /*
2560b561052SJoerg Wunsch  * Print a warning message if there is no daemon present.
2570b561052SJoerg Wunsch  */
25836d0e2a3SJoerg Wunsch static void
259ba7a1ad7SGarance A Drosehn warn(const struct printer *pp)
2600b561052SJoerg Wunsch {
2614a1a0dbeSGarrett Wollman 	if (pp->remote)
262cc3fd56fSGarance A Drosehn 		printf("%s: ", local_host);
2630b561052SJoerg Wunsch 	puts("Warning: no daemon present");
2640b561052SJoerg Wunsch 	current[0] = '\0';
2650b561052SJoerg Wunsch }
2660b561052SJoerg Wunsch 
2670b561052SJoerg Wunsch /*
2680b561052SJoerg Wunsch  * Print the header for the short listing format
2690b561052SJoerg Wunsch  */
2700b561052SJoerg Wunsch void
271ba7a1ad7SGarance A Drosehn header(void)
2720b561052SJoerg Wunsch {
2730b561052SJoerg Wunsch 	printf(head0);
2740b561052SJoerg Wunsch 	col = strlen(head0)+1;
2750b561052SJoerg Wunsch 	blankfill(SIZCOL);
2760b561052SJoerg Wunsch 	printf(head1);
2770b561052SJoerg Wunsch }
2780b561052SJoerg Wunsch 
2790b561052SJoerg Wunsch void
280ba7a1ad7SGarance A Drosehn inform(const struct printer *pp, char *cf)
2810b561052SJoerg Wunsch {
282ec6b8da5SGarance A Drosehn 	register int copycnt;
283ec6b8da5SGarance A Drosehn 	char	 savedname[MAXPATHLEN+1];
2840b561052SJoerg Wunsch 	FILE	*cfp;
2850b561052SJoerg Wunsch 
2860b561052SJoerg Wunsch 	/*
2870b561052SJoerg Wunsch 	 * There's a chance the control file has gone away
2880b561052SJoerg Wunsch 	 * in the meantime; if this is the case just keep going
2890b561052SJoerg Wunsch 	 */
290360d4ad5SWarner Losh 	seteuid(euid);
2910b561052SJoerg Wunsch 	if ((cfp = fopen(cf, "r")) == NULL)
2920b561052SJoerg Wunsch 		return;
293360d4ad5SWarner Losh 	seteuid(uid);
2940b561052SJoerg Wunsch 
2950b561052SJoerg Wunsch 	if (rank < 0)
2960b561052SJoerg Wunsch 		rank = 0;
2974a1a0dbeSGarrett Wollman 	if (pp->remote || garbage || strcmp(cf, current))
2980b561052SJoerg Wunsch 		rank++;
299ec6b8da5SGarance A Drosehn 
300ec6b8da5SGarance A Drosehn 	/*
301ec6b8da5SGarance A Drosehn 	 * The cf-file may include commands to print more than one datafile
302ec6b8da5SGarance A Drosehn 	 * from the user.  For each datafile, the cf-file contains at least
303ec6b8da5SGarance A Drosehn 	 * one line which starts with some format-specifier ('a'-'z'), and
304ec6b8da5SGarance A Drosehn 	 * a second line ('N'ame) which indicates the original name the user
305ec6b8da5SGarance A Drosehn 	 * specified for that file.  There can be multiple format-spec lines
306ec6b8da5SGarance A Drosehn 	 * for a single Name-line, if the user requested multiple copies of
307ec6b8da5SGarance A Drosehn 	 * that file.  Standard lpr puts the format-spec line(s) before the
308ec6b8da5SGarance A Drosehn 	 * Name-line, while lprNG puts the Name-line before the format-spec
309ec6b8da5SGarance A Drosehn 	 * line(s).  This section needs to handle the lines in either order.
310ec6b8da5SGarance A Drosehn 	 */
311ec6b8da5SGarance A Drosehn 	copycnt = 0;
312ec6b8da5SGarance A Drosehn 	file[0] = '\0';
313ec6b8da5SGarance A Drosehn 	savedname[0] = '\0';
3140b561052SJoerg Wunsch 	while (getline(cfp)) {
3150b561052SJoerg Wunsch 		switch (line[0]) {
3160b561052SJoerg Wunsch 		case 'P': /* Was this file specified in the user's list? */
3170b561052SJoerg Wunsch 			if (!inlist(line+1, cf)) {
3180b561052SJoerg Wunsch 				fclose(cfp);
3190b561052SJoerg Wunsch 				return;
3200b561052SJoerg Wunsch 			}
3210b561052SJoerg Wunsch 			if (lflag) {
3220b561052SJoerg Wunsch 				printf("\n%s: ", line+1);
3230b561052SJoerg Wunsch 				col = strlen(line+1) + 2;
3240b561052SJoerg Wunsch 				prank(rank);
3250b561052SJoerg Wunsch 				blankfill(JOBCOL);
3260b561052SJoerg Wunsch 				printf(" [job %s]\n", cf+3);
3270b561052SJoerg Wunsch 			} else {
3280b561052SJoerg Wunsch 				col = 0;
3290b561052SJoerg Wunsch 				prank(rank);
3300b561052SJoerg Wunsch 				blankfill(OWNCOL);
3310b561052SJoerg Wunsch 				printf("%-10s %-3d  ", line+1, atoi(cf+3));
3320b561052SJoerg Wunsch 				col += 16;
3330b561052SJoerg Wunsch 				first = 1;
3340b561052SJoerg Wunsch 			}
3350b561052SJoerg Wunsch 			continue;
3360b561052SJoerg Wunsch 		default: /* some format specifer and file name? */
3370b561052SJoerg Wunsch 			if (line[0] < 'a' || line[0] > 'z')
338ec6b8da5SGarance A Drosehn 				break;
339ec6b8da5SGarance A Drosehn 			if (copycnt == 0 || strcmp(file, line+1) != 0) {
340ec6b8da5SGarance A Drosehn 				strncpy(file, line + 1, sizeof(file) - 1);
341d583a7c3SWarner Losh 				file[sizeof(file) - 1] = '\0';
342d583a7c3SWarner Losh 			}
343ec6b8da5SGarance A Drosehn 			copycnt++;
344ec6b8da5SGarance A Drosehn 			/*
345ec6b8da5SGarance A Drosehn 			 * deliberately 'continue' to another getline(), so
346ec6b8da5SGarance A Drosehn 			 * all format-spec lines for this datafile are read
347ec6b8da5SGarance A Drosehn 			 * in and counted before calling show()
348ec6b8da5SGarance A Drosehn 			 */
3490b561052SJoerg Wunsch 			continue;
3500b561052SJoerg Wunsch 		case 'N':
351ec6b8da5SGarance A Drosehn 			strncpy(savedname, line + 1, sizeof(savedname) - 1);
352ec6b8da5SGarance A Drosehn 			savedname[sizeof(savedname) - 1] = '\0';
353ec6b8da5SGarance A Drosehn 			break;
354ec6b8da5SGarance A Drosehn 		}
355ec6b8da5SGarance A Drosehn 		if ((file[0] != '\0') && (savedname[0] != '\0')) {
356ec6b8da5SGarance A Drosehn 			show(savedname, file, copycnt);
357ec6b8da5SGarance A Drosehn 			copycnt = 0;
3580b561052SJoerg Wunsch 			file[0] = '\0';
359ec6b8da5SGarance A Drosehn 			savedname[0] = '\0';
3600b561052SJoerg Wunsch 		}
3610b561052SJoerg Wunsch 	}
3620b561052SJoerg Wunsch 	fclose(cfp);
363ec6b8da5SGarance A Drosehn 	/* check for a file which hasn't been shown yet */
364ec6b8da5SGarance A Drosehn 	if (file[0] != '\0') {
365ec6b8da5SGarance A Drosehn 		if (savedname[0] == '\0') {
366ec6b8da5SGarance A Drosehn 			/* a safeguard in case the N-ame line is missing */
367ec6b8da5SGarance A Drosehn 			strncpy(savedname, file, sizeof(savedname) - 1);
368ec6b8da5SGarance A Drosehn 			savedname[sizeof(savedname) - 1] = '\0';
369ec6b8da5SGarance A Drosehn 		}
370ec6b8da5SGarance A Drosehn 		show(savedname, file, copycnt);
371ec6b8da5SGarance A Drosehn 	}
3720b561052SJoerg Wunsch 	if (!lflag) {
3730b561052SJoerg Wunsch 		blankfill(SIZCOL);
3740b561052SJoerg Wunsch 		printf("%ld bytes\n", totsize);
3750b561052SJoerg Wunsch 		totsize = 0;
3760b561052SJoerg Wunsch 	}
3770b561052SJoerg Wunsch }
3780b561052SJoerg Wunsch 
3790b561052SJoerg Wunsch int
380ba7a1ad7SGarance A Drosehn inlist(char *uname, char *cfile)
3810b561052SJoerg Wunsch {
3820b561052SJoerg Wunsch 	register int *r, n;
3830b561052SJoerg Wunsch 	register char **u, *cp;
3840b561052SJoerg Wunsch 
3850b561052SJoerg Wunsch 	if (users == 0 && requests == 0)
3860b561052SJoerg Wunsch 		return(1);
3870b561052SJoerg Wunsch 	/*
3880b561052SJoerg Wunsch 	 * Check to see if it's in the user list
3890b561052SJoerg Wunsch 	 */
3900b561052SJoerg Wunsch 	for (u = user; u < &user[users]; u++)
391ba7a1ad7SGarance A Drosehn 		if (!strcmp(*u, uname))
3920b561052SJoerg Wunsch 			return(1);
3930b561052SJoerg Wunsch 	/*
3940b561052SJoerg Wunsch 	 * Check the request list
3950b561052SJoerg Wunsch 	 */
396ba7a1ad7SGarance A Drosehn 	for (n = 0, cp = cfile+3; isdigit(*cp); )
3970b561052SJoerg Wunsch 		n = n * 10 + (*cp++ - '0');
3980b561052SJoerg Wunsch 	for (r = requ; r < &requ[requests]; r++)
399cc3fd56fSGarance A Drosehn 		if (*r == n && !strcmp(cp, from_host))
4000b561052SJoerg Wunsch 			return(1);
4010b561052SJoerg Wunsch 	return(0);
4020b561052SJoerg Wunsch }
4030b561052SJoerg Wunsch 
4040b561052SJoerg Wunsch void
405ba7a1ad7SGarance A Drosehn show(const char *nfile, const char *datafile, int copies)
4060b561052SJoerg Wunsch {
4070b561052SJoerg Wunsch 	if (strcmp(nfile, " ") == 0)
4080b561052SJoerg Wunsch 		nfile = "(standard input)";
4090b561052SJoerg Wunsch 	if (lflag)
410ba7a1ad7SGarance A Drosehn 		ldump(nfile, datafile, copies);
4110b561052SJoerg Wunsch 	else
412ba7a1ad7SGarance A Drosehn 		dump(nfile, datafile, copies);
4130b561052SJoerg Wunsch }
4140b561052SJoerg Wunsch 
4150b561052SJoerg Wunsch /*
4160b561052SJoerg Wunsch  * Fill the line with blanks to the specified column
4170b561052SJoerg Wunsch  */
4180b561052SJoerg Wunsch void
419ba7a1ad7SGarance A Drosehn blankfill(int tocol)
4200b561052SJoerg Wunsch {
421ba7a1ad7SGarance A Drosehn 	while (col++ < tocol)
4220b561052SJoerg Wunsch 		putchar(' ');
4230b561052SJoerg Wunsch }
4240b561052SJoerg Wunsch 
4250b561052SJoerg Wunsch /*
4260b561052SJoerg Wunsch  * Give the abbreviated dump of the file names
4270b561052SJoerg Wunsch  */
4280b561052SJoerg Wunsch void
429ba7a1ad7SGarance A Drosehn dump(const char *nfile, const char *datafile, int copies)
4300b561052SJoerg Wunsch {
4310b561052SJoerg Wunsch 	struct stat lbuf;
432be794da7SGarance A Drosehn 	const char etctmpl[] = ", ...";
433be794da7SGarance A Drosehn 	char	 etc[sizeof(etctmpl)];
434be794da7SGarance A Drosehn 	char	*lastsep;
435be794da7SGarance A Drosehn 	short	 fill, nlen;
436be794da7SGarance A Drosehn 	short	 rem, remetc;
4370b561052SJoerg Wunsch 
4380b561052SJoerg Wunsch 	/*
439be794da7SGarance A Drosehn 	 * Print as many filenames as will fit
440be794da7SGarance A Drosehn 	 *      (leaving room for the 'total size' field)
4410b561052SJoerg Wunsch 	 */
4420b561052SJoerg Wunsch 	fill = first ? 0 : 2;	/* fill space for ``, '' */
443be794da7SGarance A Drosehn 	nlen = strlen(nfile);
444be794da7SGarance A Drosehn 	rem = SIZCOL - 1 - col;
445be794da7SGarance A Drosehn 	if (nlen + fill > rem) {
446be794da7SGarance A Drosehn 		if (first) {
447be794da7SGarance A Drosehn 			/* print the right-most part of the name */
448be794da7SGarance A Drosehn 			printf("...%s ", &nfile[3+nlen-rem]);
449be794da7SGarance A Drosehn 			col = SIZCOL;
450be794da7SGarance A Drosehn 		} else if (rem > 0) {
451be794da7SGarance A Drosehn 			/* fit as much of the etc-string as we can */
452be794da7SGarance A Drosehn 			remetc = rem;
453be794da7SGarance A Drosehn 			if (rem > strlen(etctmpl))
454be794da7SGarance A Drosehn 				remetc = strlen(etctmpl);
455be794da7SGarance A Drosehn 			etc[0] = '\0';
456be794da7SGarance A Drosehn 			strncat(etc, etctmpl, remetc);
457be794da7SGarance A Drosehn 			printf(etc);
458be794da7SGarance A Drosehn 			col += remetc;
459be794da7SGarance A Drosehn 			rem -= remetc;
460be794da7SGarance A Drosehn 			/* room for the last segment of this filename? */
461be794da7SGarance A Drosehn 			lastsep = strrchr(nfile, '/');
462be794da7SGarance A Drosehn 			if ((lastsep != NULL) && (rem > strlen(lastsep))) {
463be794da7SGarance A Drosehn 				/* print the right-most part of this name */
464be794da7SGarance A Drosehn 				printf("%s", lastsep);
465be794da7SGarance A Drosehn 				col += strlen(lastsep);
466be794da7SGarance A Drosehn 			} else {
467be794da7SGarance A Drosehn 				/* do not pack any more names in here */
4680b561052SJoerg Wunsch 				blankfill(SIZCOL);
4690b561052SJoerg Wunsch 			}
470be794da7SGarance A Drosehn 		}
4710b561052SJoerg Wunsch 	} else {
472be794da7SGarance A Drosehn 		if (!first)
4730b561052SJoerg Wunsch 			printf(", ");
4740b561052SJoerg Wunsch 		printf("%s", nfile);
475be794da7SGarance A Drosehn 		col += nlen + fill;
4760b561052SJoerg Wunsch 	}
477be794da7SGarance A Drosehn 	first = 0;
478be794da7SGarance A Drosehn 
479360d4ad5SWarner Losh 	seteuid(euid);
480ba7a1ad7SGarance A Drosehn 	if (*datafile && !stat(datafile, &lbuf))
4810b561052SJoerg Wunsch 		totsize += copies * lbuf.st_size;
482360d4ad5SWarner Losh 	seteuid(uid);
4830b561052SJoerg Wunsch }
4840b561052SJoerg Wunsch 
4850b561052SJoerg Wunsch /*
4860b561052SJoerg Wunsch  * Print the long info about the file
4870b561052SJoerg Wunsch  */
4880b561052SJoerg Wunsch void
489ba7a1ad7SGarance A Drosehn ldump(const char *nfile, const char *datafile, int copies)
4900b561052SJoerg Wunsch {
4910b561052SJoerg Wunsch 	struct stat lbuf;
4920b561052SJoerg Wunsch 
4930b561052SJoerg Wunsch 	putchar('\t');
4940b561052SJoerg Wunsch 	if (copies > 1)
4950b561052SJoerg Wunsch 		printf("%-2d copies of %-19s", copies, nfile);
4960b561052SJoerg Wunsch 	else
4970b561052SJoerg Wunsch 		printf("%-32s", nfile);
498ba7a1ad7SGarance A Drosehn 	if (*datafile && !stat(datafile, &lbuf))
499ef1c4c53SJohn Birrell 		printf(" %qd bytes", (long long) lbuf.st_size);
5000b561052SJoerg Wunsch 	else
5010b561052SJoerg Wunsch 		printf(" ??? bytes");
5020b561052SJoerg Wunsch 	putchar('\n');
5030b561052SJoerg Wunsch }
5040b561052SJoerg Wunsch 
5050b561052SJoerg Wunsch /*
5060b561052SJoerg Wunsch  * Print the job's rank in the queue,
5070b561052SJoerg Wunsch  *   update col for screen management
5080b561052SJoerg Wunsch  */
5090b561052SJoerg Wunsch void
510ba7a1ad7SGarance A Drosehn prank(int n)
5110b561052SJoerg Wunsch {
5120b561052SJoerg Wunsch 	char rline[100];
513ba7a1ad7SGarance A Drosehn 	static const char *r[] = {
5140b561052SJoerg Wunsch 		"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
5150b561052SJoerg Wunsch 	};
5160b561052SJoerg Wunsch 
5170b561052SJoerg Wunsch 	if (n == 0) {
5180b561052SJoerg Wunsch 		printf("active");
5190b561052SJoerg Wunsch 		col += 6;
5200b561052SJoerg Wunsch 		return;
5210b561052SJoerg Wunsch 	}
5220b561052SJoerg Wunsch 	if ((n/10)%10 == 1)
5230b561052SJoerg Wunsch 		(void)snprintf(rline, sizeof(rline), "%dth", n);
5240b561052SJoerg Wunsch 	else
5250b561052SJoerg Wunsch 		(void)snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]);
5260b561052SJoerg Wunsch 	col += strlen(rline);
5270b561052SJoerg Wunsch 	printf("%s", rline);
5280b561052SJoerg Wunsch }
529334a9508SJoerg Wunsch 
530334a9508SJoerg Wunsch void
531ba7a1ad7SGarance A Drosehn alarmhandler(int signo __unused)
532334a9508SJoerg Wunsch {
533ba7a1ad7SGarance A Drosehn 	/* the signal is ignored */
534ba7a1ad7SGarance A Drosehn 	/* (the '__unused' is just to avoid a compile-time warning) */
535334a9508SJoerg Wunsch }
536