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