Home | History | Annotate | Line # | Download | only in common_source
displayq.c revision 1.14
      1 /*	$NetBSD: displayq.c,v 1.14 1998/09/14 11:52:21 frueauf Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)displayq.c	8.4 (Berkeley) 4/28/95";
     40 #else
     41 __RCSID("$NetBSD: displayq.c,v 1.14 1998/09/14 11:52:21 frueauf Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/stat.h>
     47 #include <sys/file.h>
     48 
     49 #include <signal.h>
     50 #include <fcntl.h>
     51 #include <dirent.h>
     52 #include <unistd.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <ctype.h>
     57 #include "lp.h"
     58 #include "lp.local.h"
     59 #include "pathnames.h"
     60 
     61 /*
     62  * Routines to display the state of the queue.
     63  */
     64 #define JOBCOL	40		/* column for job # in -l format */
     65 #define OWNCOL	7		/* start of Owner column in normal */
     66 #define SIZCOL	62		/* start of Size column in normal */
     67 
     68 /*
     69  * Stuff for handling job specifications
     70  */
     71 extern int	requ[];		/* job number of spool entries */
     72 extern int	requests;	/* # of spool requests */
     73 extern char    *user[];	        /* users to process */
     74 extern int	users;		/* # of users in user array */
     75 
     76 extern uid_t	uid, euid;
     77 
     78 static int	col;		/* column on screen */
     79 static char	current[40];	/* current file being printed */
     80 static char	file[132];	/* print file name */
     81 static int	first;		/* first file in ``files'' column? */
     82 static int	garbage;	/* # of garbage cf files */
     83 static int	lflag;		/* long output option */
     84 static int	rank;		/* order to be printed (-1=none, 0=active) */
     85 static long	totsize;	/* total print job size in bytes */
     86 
     87 static char	*head0 = "Rank   Owner      Job  Files";
     88 static char	*head1 = "Total Size\n";
     89 
     90 /*
     91  * Display the current state of the queue. Format = 1 if long format.
     92  */
     93 void
     94 displayq(format)
     95 	int format;
     96 {
     97 	struct queue *q;
     98 	int i, nitems, fd, ret;
     99 	char *cp;
    100 	struct queue **queue;
    101 	struct stat statb;
    102 	FILE *fp;
    103 
    104 	lflag = format;
    105 	totsize = 0;
    106 	rank = -1;
    107 	if ((i = cgetent(&bp, printcapdb, printer)) == -2)
    108 		fatal("can't open printer description file");
    109 	else if (i == -1)
    110 		fatal("unknown printer");
    111 	else if (i == -3)
    112 		fatal("potential reference loop detected in printcap file");
    113 	if (cgetstr(bp, "lp", &LP) < 0)
    114 		LP = _PATH_DEFDEVLP;
    115 	if (cgetstr(bp, "rp", &RP) < 0)
    116 		RP = DEFLP;
    117 	if (cgetstr(bp, "sd", &SD) < 0)
    118 		SD = _PATH_DEFSPOOL;
    119 	if (cgetstr(bp,"lo", &LO) < 0)
    120 		LO = DEFLOCK;
    121 	if (cgetstr(bp, "st", &ST) < 0)
    122 		ST = DEFSTAT;
    123 	cgetstr(bp, "rm", &RM);
    124 	if ((cp = checkremote()) != NULL)
    125 		printf("Warning: %s\n", cp);
    126 
    127 	/*
    128 	 * Print out local queue
    129 	 * Find all the control files in the spooling directory
    130 	 */
    131 	seteuid(euid);
    132 	if (chdir(SD) < 0)
    133 		fatal("cannot chdir to spooling directory");
    134 	seteuid(uid);
    135 	if ((nitems = getq(&queue)) < 0)
    136 		fatal("cannot examine spooling area\n");
    137 	seteuid(euid);
    138 	ret = stat(LO, &statb);
    139 	seteuid(uid);
    140 	if (ret >= 0) {
    141 		if (statb.st_mode & 0100) {
    142 			if (remote)
    143 				printf("%s: ", host);
    144 			printf("Warning: %s is down: ", printer);
    145 			seteuid(euid);
    146 			fd = open(ST, O_RDONLY);
    147 			seteuid(uid);
    148 			if (fd >= 0) {
    149 				(void)flock(fd, LOCK_SH);
    150 				while ((i = read(fd, line, sizeof(line))) > 0)
    151 					(void)fwrite(line, 1, i, stdout);
    152 				(void)close(fd);	/* unlocks as well */
    153 			} else
    154 				putchar('\n');
    155 		}
    156 		if (statb.st_mode & 010) {
    157 			if (remote)
    158 				printf("%s: ", host);
    159 			printf("Warning: %s queue is turned off\n", printer);
    160 		}
    161 	}
    162 
    163 	if (nitems) {
    164 		seteuid(euid);
    165 		fp = fopen(LO, "r");
    166 		seteuid(uid);
    167 		if (fp == NULL)
    168 			nodaemon();
    169 		else {
    170 			/* get daemon pid */
    171 			cp = current;
    172 			while ((i = getc(fp)) != EOF && i != '\n')
    173 				*cp++ = i;
    174 			*cp = '\0';
    175 			i = atoi(current);
    176 			if (i <= 0) {
    177 				ret = -1;
    178 			} else {
    179 				seteuid(euid);
    180 				ret = kill(i, 0);
    181 				seteuid(uid);
    182 			}
    183 			if (ret < 0) {
    184 				nodaemon();
    185 			} else {
    186 				/* read current file name */
    187 				cp = current;
    188 				while ((i = getc(fp)) != EOF && i != '\n')
    189 					*cp++ = i;
    190 				*cp = '\0';
    191 				/*
    192 				 * Print the status file.
    193 				 */
    194 				if (remote)
    195 					printf("%s: ", host);
    196 				seteuid(euid);
    197 				fd = open(ST, O_RDONLY);
    198 				seteuid(uid);
    199 				if (fd >= 0) {
    200 					(void)flock(fd, LOCK_SH);
    201 					while ((i = read(fd, line, sizeof(line))) > 0)
    202 						(void)fwrite(line, 1, i, stdout);
    203 					(void)close(fd);	/* unlocks as well */
    204 				} else
    205 					putchar('\n');
    206 			}
    207 			(void)fclose(fp);
    208 		}
    209 		/*
    210 		 * Now, examine the control files and print out the jobs to
    211 		 * be done for each user.
    212 		 */
    213 		if (!lflag)
    214 			header();
    215 		for (i = 0; i < nitems; i++) {
    216 			q = queue[i];
    217 			inform(q->q_name);
    218 			free(q);
    219 		}
    220 		free(queue);
    221 	}
    222 	if (!remote) {
    223 		if (nitems == 0)
    224 			puts("no entries");
    225 		return;
    226 	}
    227 
    228 	/*
    229 	 * Print foreign queue
    230 	 * Note that a file in transit may show up in either queue.
    231 	 */
    232 	if (nitems)
    233 		putchar('\n');
    234 	(void)snprintf(line, sizeof(line), "%c%s", format + '\3', RP);
    235 	cp = line;
    236 	for (i = 0; i < requests && cp-line+10 < sizeof(line) - 1; i++) {
    237 		cp += strlen(cp);
    238 		(void)snprintf(cp, line - cp, " %d", requ[i]);
    239 	}
    240 	for (i = 0; i < users && cp - line + 1 + strlen(user[i]) <
    241 	    sizeof(line) - 1; i++) {
    242 		cp += strlen(cp);
    243 		if (cp - line > sizeof(line) - 1)
    244 			break;
    245 		*cp++ = ' ';
    246 		if (line - cp - 1 >= 0)
    247 			(void)strncpy(cp, user[i], line - cp - 1);
    248 		else
    249 			(void)strcpy(cp, user[i]);
    250 	}
    251 	(void)strncat(line, "\n", sizeof(line) - strlen(line) - 1);
    252 	fd = getport(RM, 0);
    253 	if (fd < 0) {
    254 		if (from != host)
    255 			printf("%s: ", host);
    256 		(void)printf("connection to %s is down\n", RM);
    257 	}
    258 	else {
    259 		i = strlen(line);
    260 		if (write(fd, line, i) != i)
    261 			fatal("Lost connection");
    262 		while ((i = read(fd, line, sizeof(line))) > 0)
    263 			(void)fwrite(line, 1, i, stdout);
    264 		(void)close(fd);
    265 	}
    266 }
    267 
    268 /*
    269  * Print a warning message if there is no daemon present.
    270  */
    271 void
    272 nodaemon()
    273 {
    274 	if (remote)
    275 		printf("\n%s: ", host);
    276 	puts("Warning: no daemon present");
    277 	current[0] = '\0';
    278 }
    279 
    280 /*
    281  * Print the header for the short listing format
    282  */
    283 void
    284 header()
    285 {
    286 	printf(head0);
    287 	col = strlen(head0)+1;
    288 	blankfill(SIZCOL);
    289 	printf(head1);
    290 }
    291 
    292 void
    293 inform(cf)
    294 	char *cf;
    295 {
    296 	int j;
    297 	FILE *cfp;
    298 
    299 	/*
    300 	 * There's a chance the control file has gone away
    301 	 * in the meantime; if this is the case just keep going
    302 	 */
    303 	seteuid(euid);
    304 	if ((cfp = fopen(cf, "r")) == NULL)
    305 		return;
    306 	seteuid(uid);
    307 
    308 	if (rank < 0)
    309 		rank = 0;
    310 	if (remote || garbage || strcmp(cf, current))
    311 		rank++;
    312 	j = 0;
    313 	while (getline(cfp)) {
    314 		switch (line[0]) {
    315 		case 'P': /* Was this file specified in the user's list? */
    316 			if (!inlist(line+1, cf)) {
    317 				fclose(cfp);
    318 				return;
    319 			}
    320 			if (lflag) {
    321 				printf("\n%s: ", line+1);
    322 				col = strlen(line+1) + 2;
    323 				prank(rank);
    324 				blankfill(JOBCOL);
    325 				printf(" [job %s]\n", cf+3);
    326 			} else {
    327 				col = 0;
    328 				prank(rank);
    329 				blankfill(OWNCOL);
    330 				printf("%-10s %-3d  ", line+1, atoi(cf+3));
    331 				col += 16;
    332 				first = 1;
    333 			}
    334 			continue;
    335 		default: /* some format specifer and file name? */
    336 			if (line[0] < 'a' || line[0] > 'z')
    337 				continue;
    338 			if (j == 0 || strcmp(file, line+1) != 0) {
    339 				(void)strncpy(file, line+1, sizeof(file) - 1);
    340 				file[sizeof(file) - 1] = '\0';
    341 			}
    342 			j++;
    343 			continue;
    344 		case 'N':
    345 			show(line+1, file, j);
    346 			file[0] = '\0';
    347 			j = 0;
    348 		}
    349 	}
    350 	fclose(cfp);
    351 	if (!lflag) {
    352 		blankfill(SIZCOL);
    353 		printf("%ld bytes\n", totsize);
    354 		totsize = 0;
    355 	}
    356 }
    357 
    358 int
    359 inlist(name, file)
    360 	char *name, *file;
    361 {
    362 	int *r, n;
    363 	char **u, *cp;
    364 
    365 	if (users == 0 && requests == 0)
    366 		return(1);
    367 	/*
    368 	 * Check to see if it's in the user list
    369 	 */
    370 	for (u = user; u < &user[users]; u++)
    371 		if (!strcmp(*u, name))
    372 			return(1);
    373 	/*
    374 	 * Check the request list
    375 	 */
    376 	for (n = 0, cp = file+3; isdigit(*cp); )
    377 		n = n * 10 + (*cp++ - '0');
    378 	for (r = requ; r < &requ[requests]; r++)
    379 		if (*r == n && !strcmp(cp, from))
    380 			return(1);
    381 	return(0);
    382 }
    383 
    384 void
    385 show(nfile, file, copies)
    386 	char *nfile, *file;
    387 	int copies;
    388 {
    389 	if (strcmp(nfile, " ") == 0)
    390 		nfile = "(standard input)";
    391 	if (lflag)
    392 		ldump(nfile, file, copies);
    393 	else
    394 		dump(nfile, file, copies);
    395 }
    396 
    397 /*
    398  * Fill the line with blanks to the specified column
    399  */
    400 void
    401 blankfill(n)
    402 	int n;
    403 {
    404 	while (col++ < n)
    405 		putchar(' ');
    406 }
    407 
    408 /*
    409  * Give the abbreviated dump of the file names
    410  */
    411 void
    412 dump(nfile, file, copies)
    413 	char *nfile, *file;
    414 	int copies;
    415 {
    416 	short n, fill;
    417 	struct stat lbuf;
    418 
    419 	/*
    420 	 * Print as many files as will fit
    421 	 *  (leaving room for the total size)
    422 	 */
    423 	 fill = first ? 0 : 2;	/* fill space for ``, '' */
    424 	 if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) {
    425 		if (col < SIZCOL) {
    426 			printf(" ..."), col += 4;
    427 			blankfill(SIZCOL);
    428 		}
    429 	} else {
    430 		if (first)
    431 			first = 0;
    432 		else
    433 			printf(", ");
    434 		printf("%s", nfile);
    435 		col += n+fill;
    436 	}
    437 	seteuid(euid);
    438 	if (*file && !stat(file, &lbuf))
    439 		totsize += copies * lbuf.st_size;
    440 	seteuid(uid);
    441 }
    442 
    443 /*
    444  * Print the long info about the file
    445  */
    446 void
    447 ldump(nfile, file, copies)
    448 	char *nfile, *file;
    449 	int copies;
    450 {
    451 	struct stat lbuf;
    452 
    453 	putchar('\t');
    454 	if (copies > 1)
    455 		printf("%-2d copies of %-19s", copies, nfile);
    456 	else
    457 		printf("%-32s", nfile);
    458 	if (*file && !stat(file, &lbuf))
    459 		printf(" %qd bytes", (long long)lbuf.st_size);
    460 	else
    461 		printf(" ??? bytes");
    462 	putchar('\n');
    463 }
    464 
    465 /*
    466  * Print the job's rank in the queue,
    467  *   update col for screen management
    468  */
    469 void
    470 prank(n)
    471 	int n;
    472 {
    473 	char rline[100];
    474 	static char *r[] = {
    475 		"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
    476 	};
    477 
    478 	if (n == 0) {
    479 		printf("active");
    480 		col += 6;
    481 		return;
    482 	}
    483 	if ((n/10)%10 == 1)
    484 		(void)snprintf(rline, sizeof(rline), "%dth", n);
    485 	else
    486 		(void)snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]);
    487 	col += strlen(rline);
    488 	printf("%s", rline);
    489 }
    490