Home | History | Annotate | Line # | Download | only in pac
pac.c revision 1.6
      1 /*	$NetBSD: pac.c,v 1.6 1996/01/13 23:18:15 pk Exp $	*/
      2 /*
      3  * Copyright (c) 1983, 1993
      4  *	The Regents of the University of California.  All rights reserved.
      5  *
      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 #ifndef lint
     37 static char copyright[] =
     38 "@(#) Copyright (c) 1983, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n";
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 static char sccsid[] = "@(#)pac.c	8.1 (Berkeley) 6/6/93";
     44 #endif /* not lint */
     45 
     46 /*
     47  * Do Printer accounting summary.
     48  * Currently, usage is
     49  *	pac [-Pprinter] [-pprice] [-s] [-r] [-c] [-m] [user ...]
     50  * to print the usage information for the named people.
     51  */
     52 
     53 #include <sys/param.h>
     54 
     55 #include <dirent.h>
     56 #include <stdlib.h>
     57 #include <stdio.h>
     58 #include <string.h>
     59 #include "lp.h"
     60 #include "lp.local.h"
     61 
     62 static char	*acctfile;	/* accounting file (input data) */
     63 static int	 allflag = 1;	/* Get stats on everybody */
     64 static int	 errs;
     65 static int	 hcount;	/* Count of hash entries */
     66 static int	 mflag = 0;	/* disregard machine names */
     67 static int	 pflag = 0;	/* 1 if -p on cmd line */
     68 static float	 price = 0.02;	/* cost per page (or what ever) */
     69 static long	 price100;	/* per-page cost in 100th of a cent */
     70 static int	 reverse;	/* Reverse sort order */
     71 static int	 sort;		/* Sort by cost */
     72 static char	*sumfile;	/* summary file */
     73 static int	 summarize;	/* Compress accounting file */
     74 
     75 uid_t	uid, euid;
     76 
     77 /*
     78  * Grossness follows:
     79  *  Names to be accumulated are hashed into the following
     80  *  table.
     81  */
     82 
     83 #define	HSHSIZE	97			/* Number of hash buckets */
     84 
     85 struct hent {
     86 	struct	hent *h_link;		/* Forward hash link */
     87 	char	*h_name;		/* Name of this user */
     88 	float	h_feetpages;		/* Feet or pages of paper */
     89 	int	h_count;		/* Number of runs */
     90 };
     91 
     92 static struct	hent	*hashtab[HSHSIZE];	/* Hash table proper */
     93 
     94 static void	account __P((FILE *));
     95 static int	any __P((int, char []));
     96 static int	chkprinter __P((char *));
     97 static void	dumpit __P((void));
     98 static int	hash __P((char []));
     99 static struct	hent *enter __P((char []));
    100 static struct	hent *lookup __P((char []));
    101 static int	qucmp __P((const void *, const void *));
    102 static void	rewrite __P((void));
    103 
    104 void
    105 main(argc, argv)
    106 	int argc;
    107 	char **argv;
    108 {
    109 	register FILE *acct;
    110 	register char *cp;
    111 
    112 	euid = geteuid();	/* these aren't used in pac(1) */
    113 	uid = getuid();
    114 	while (--argc) {
    115 		cp = *++argv;
    116 		if (*cp++ == '-') {
    117 			switch(*cp++) {
    118 			case 'P':
    119 				/*
    120 				 * Printer name.
    121 				 */
    122 				printer = cp;
    123 				continue;
    124 
    125 			case 'p':
    126 				/*
    127 				 * get the price.
    128 				 */
    129 				price = atof(cp);
    130 				pflag = 1;
    131 				continue;
    132 
    133 			case 's':
    134 				/*
    135 				 * Summarize and compress accounting file.
    136 				 */
    137 				summarize++;
    138 				continue;
    139 
    140 			case 'c':
    141 				/*
    142 				 * Sort by cost.
    143 				 */
    144 				sort++;
    145 				continue;
    146 
    147 			case 'm':
    148 				/*
    149 				 * disregard machine names for each user
    150 				 */
    151 				mflag = 1;
    152 				continue;
    153 
    154 			case 'r':
    155 				/*
    156 				 * Reverse sorting order.
    157 				 */
    158 				reverse++;
    159 				continue;
    160 
    161 			default:
    162 fprintf(stderr,
    163     "usage: pac [-Pprinter] [-pprice] [-s] [-c] [-r] [-m] [user ...]\n");
    164 				exit(1);
    165 			}
    166 		}
    167 		(void) enter(--cp);
    168 		allflag = 0;
    169 	}
    170 	if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
    171 		printer = DEFLP;
    172 	if (!chkprinter(printer)) {
    173 		printf("pac: unknown printer %s\n", printer);
    174 		exit(2);
    175 	}
    176 
    177 	if ((acct = fopen(acctfile, "r")) == NULL) {
    178 		perror(acctfile);
    179 		exit(1);
    180 	}
    181 	account(acct);
    182 	fclose(acct);
    183 	if ((acct = fopen(sumfile, "r")) != NULL) {
    184 		account(acct);
    185 		fclose(acct);
    186 	}
    187 	if (summarize)
    188 		rewrite();
    189 	else
    190 		dumpit();
    191 	exit(errs);
    192 }
    193 
    194 /*
    195  * Read the entire accounting file, accumulating statistics
    196  * for the users that we have in the hash table.  If allflag
    197  * is set, then just gather the facts on everyone.
    198  * Note that we must accomodate both the active and summary file
    199  * formats here.
    200  * Host names are ignored if the -m flag is present.
    201  */
    202 static void
    203 account(acct)
    204 	register FILE *acct;
    205 {
    206 	char linebuf[BUFSIZ];
    207 	double t;
    208 	register char *cp, *cp2;
    209 	register struct hent *hp;
    210 	register int ic;
    211 
    212 	while (fgets(linebuf, BUFSIZ, acct) != NULL) {
    213 		cp = linebuf;
    214 		while (any(*cp, " \t"))
    215 			cp++;
    216 		t = atof(cp);
    217 		while (any(*cp, ".0123456789"))
    218 			cp++;
    219 		while (any(*cp, " \t"))
    220 			cp++;
    221 		for (cp2 = cp; !any(*cp2, " \t\n"); cp2++)
    222 			;
    223 		ic = atoi(cp2);
    224 		*cp2 = '\0';
    225 		if (mflag && index(cp, ':'))
    226 		    cp = index(cp, ':') + 1;
    227 		hp = lookup(cp);
    228 		if (hp == NULL) {
    229 			if (!allflag)
    230 				continue;
    231 			hp = enter(cp);
    232 		}
    233 		hp->h_feetpages += t;
    234 		if (ic)
    235 			hp->h_count += ic;
    236 		else
    237 			hp->h_count++;
    238 	}
    239 }
    240 
    241 /*
    242  * Sort the hashed entries by name or footage
    243  * and print it all out.
    244  */
    245 static void
    246 dumpit()
    247 {
    248 	struct hent **base;
    249 	register struct hent *hp, **ap;
    250 	register int hno, c, runs;
    251 	float feet;
    252 
    253 	hp = hashtab[0];
    254 	hno = 1;
    255 	base = (struct hent **) calloc(sizeof hp, hcount);
    256 	for (ap = base, c = hcount; c--; ap++) {
    257 		while (hp == NULL)
    258 			hp = hashtab[hno++];
    259 		*ap = hp;
    260 		hp = hp->h_link;
    261 	}
    262 	qsort(base, hcount, sizeof hp, qucmp);
    263 	printf("  Login               pages/feet   runs    price\n");
    264 	feet = 0.0;
    265 	runs = 0;
    266 	for (ap = base, c = hcount; c--; ap++) {
    267 		hp = *ap;
    268 		runs += hp->h_count;
    269 		feet += hp->h_feetpages;
    270 		printf("%-24s %7.2f %4d   $%6.2f\n", hp->h_name,
    271 		    hp->h_feetpages, hp->h_count, hp->h_feetpages * price);
    272 	}
    273 	if (allflag) {
    274 		printf("\n");
    275 		printf("%-24s %7.2f %4d   $%6.2f\n", "total", feet,
    276 		    runs, feet * price);
    277 	}
    278 }
    279 
    280 /*
    281  * Rewrite the summary file with the summary information we have accumulated.
    282  */
    283 static void
    284 rewrite()
    285 {
    286 	register struct hent *hp;
    287 	register int i;
    288 	register FILE *acctf;
    289 
    290 	if ((acctf = fopen(sumfile, "w")) == NULL) {
    291 		perror(sumfile);
    292 		errs++;
    293 		return;
    294 	}
    295 	for (i = 0; i < HSHSIZE; i++) {
    296 		hp = hashtab[i];
    297 		while (hp != NULL) {
    298 			fprintf(acctf, "%7.2f\t%s\t%d\n", hp->h_feetpages,
    299 			    hp->h_name, hp->h_count);
    300 			hp = hp->h_link;
    301 		}
    302 	}
    303 	fflush(acctf);
    304 	if (ferror(acctf)) {
    305 		perror(sumfile);
    306 		errs++;
    307 	}
    308 	fclose(acctf);
    309 	if ((acctf = fopen(acctfile, "w")) == NULL)
    310 		perror(acctfile);
    311 	else
    312 		fclose(acctf);
    313 }
    314 
    315 /*
    316  * Hashing routines.
    317  */
    318 
    319 /*
    320  * Enter the name into the hash table and return the pointer allocated.
    321  */
    322 
    323 static struct hent *
    324 enter(name)
    325 	char name[];
    326 {
    327 	register struct hent *hp;
    328 	register int h;
    329 
    330 	if ((hp = lookup(name)) != NULL)
    331 		return(hp);
    332 	h = hash(name);
    333 	hcount++;
    334 	hp = (struct hent *) calloc(sizeof *hp, 1);
    335 	hp->h_name = (char *) calloc(sizeof(char), strlen(name)+1);
    336 	strcpy(hp->h_name, name);
    337 	hp->h_feetpages = 0.0;
    338 	hp->h_count = 0;
    339 	hp->h_link = hashtab[h];
    340 	hashtab[h] = hp;
    341 	return(hp);
    342 }
    343 
    344 /*
    345  * Lookup a name in the hash table and return a pointer
    346  * to it.
    347  */
    348 
    349 static struct hent *
    350 lookup(name)
    351 	char name[];
    352 {
    353 	register int h;
    354 	register struct hent *hp;
    355 
    356 	h = hash(name);
    357 	for (hp = hashtab[h]; hp != NULL; hp = hp->h_link)
    358 		if (strcmp(hp->h_name, name) == 0)
    359 			return(hp);
    360 	return(NULL);
    361 }
    362 
    363 /*
    364  * Hash the passed name and return the index in
    365  * the hash table to begin the search.
    366  */
    367 static int
    368 hash(name)
    369 	char name[];
    370 {
    371 	register int h;
    372 	register char *cp;
    373 
    374 	for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
    375 		;
    376 	return((h & 0x7fffffff) % HSHSIZE);
    377 }
    378 
    379 /*
    380  * Other stuff
    381  */
    382 static int
    383 any(ch, str)
    384 	int ch;
    385 	char str[];
    386 {
    387 	register int c = ch;
    388 	register char *cp = str;
    389 
    390 	while (*cp)
    391 		if (*cp++ == c)
    392 			return(1);
    393 	return(0);
    394 }
    395 
    396 /*
    397  * The qsort comparison routine.
    398  * The comparison is ascii collating order
    399  * or by feet of typesetter film, according to sort.
    400  */
    401 static int
    402 qucmp(a, b)
    403 	const void *a, *b;
    404 {
    405 	register struct hent *h1, *h2;
    406 	register int r;
    407 
    408 	h1 = *(struct hent **)a;
    409 	h2 = *(struct hent **)b;
    410 	if (sort)
    411 		r = h1->h_feetpages < h2->h_feetpages ?
    412 		    -1 : h1->h_feetpages > h2->h_feetpages;
    413 	else
    414 		r = strcmp(h1->h_name, h2->h_name);
    415 	return(reverse ? -r : r);
    416 }
    417 
    418 /*
    419  * Perform lookup for printer name or abbreviation --
    420  */
    421 static int
    422 chkprinter(s)
    423 	register char *s;
    424 {
    425 	int stat;
    426 
    427 	if ((stat = cgetent(&bp, printcapdb, s)) == -2) {
    428 		printf("pac: can't open printer description file\n");
    429 		exit(3);
    430 	} else if (stat == -1)
    431 		return(0);
    432 	else if (stat == -3)
    433 		fatal("potential reference loop detected in printcap file");
    434 
    435 	if (cgetstr(bp, "af", &acctfile) == -1) {
    436 		printf("accounting not enabled for printer %s\n", printer);
    437 		exit(2);
    438 	}
    439 	if (!pflag && (cgetnum(bp, "pc", &price100) == 0))
    440 		price = price100/10000.0;
    441 	sumfile = (char *) calloc(sizeof(char), strlen(acctfile)+5);
    442 	if (sumfile == NULL) {
    443 		perror("pac");
    444 		exit(1);
    445 	}
    446 	strcpy(sumfile, acctfile);
    447 	strcat(sumfile, "_sum");
    448 	return(1);
    449 }
    450