Home | History | Annotate | Line # | Download | only in pac
pac.c revision 1.12
      1 /*	$NetBSD: pac.c,v 1.12 1999/12/05 22:22:05 jdolecek Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #ifndef lint
     39 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
     40 	The Regents of the University of California.  All rights reserved.\n");
     41 #if 0
     42 static char sccsid[] = "@(#)pac.c	8.1 (Berkeley) 6/6/93";
     43 #else
     44 __RCSID("$NetBSD: pac.c,v 1.12 1999/12/05 22:22:05 jdolecek Exp $");
     45 #endif
     46 #endif /* not lint */
     47 
     48 /*
     49  * Do Printer accounting summary.
     50  * Currently, usage is
     51  *	pac [-Pprinter] [-pprice] [-s] [-r] [-c] [-m] [user ...]
     52  * to print the usage information for the named people.
     53  */
     54 
     55 #include <sys/param.h>
     56 
     57 #include <dirent.h>
     58 #include <stdio.h>
     59 #include <stdlib.h>
     60 #include <string.h>
     61 #include <unistd.h>
     62 #include <err.h>
     63 
     64 #include "lp.h"
     65 #include "lp.local.h"
     66 
     67 static char	*acctfile;	/* accounting file (input data) */
     68 static int	 allflag = 1;	/* Get stats on everybody */
     69 static int	 errs;
     70 static int	 hcount;	/* Count of hash entries */
     71 static int	 mflag = 0;	/* disregard machine names */
     72 static int	 pflag = 0;	/* 1 if -p on cmd line */
     73 static float	 price = 0.02;	/* cost per page (or what ever) */
     74 static long	 price100;	/* per-page cost in 100th of a cent */
     75 static int	 reverse;	/* Reverse sort order */
     76 static int	 sort;		/* Sort by cost */
     77 static char	*sumfile;	/* summary file */
     78 static int	 summarize;	/* Compress accounting file */
     79 
     80 /*
     81  * Grossness follows:
     82  *  Names to be accumulated are hashed into the following
     83  *  table.
     84  */
     85 
     86 #define	HSHSIZE	97			/* Number of hash buckets */
     87 
     88 struct hent {
     89 	struct	hent *h_link;		/* Forward hash link */
     90 	char	*h_name;		/* Name of this user */
     91 	float	h_feetpages;		/* Feet or pages of paper */
     92 	int	h_count;		/* Number of runs */
     93 };
     94 
     95 static struct	hent	*hashtab[HSHSIZE];	/* Hash table proper */
     96 
     97 static void	account __P((FILE *));
     98 static int	any __P((int, const char *));
     99 static int	chkprinter __P((const char *));
    100 static void	dumpit __P((void));
    101 static int	hash __P((const char *));
    102 static struct	hent *enter __P((const char *));
    103 static struct	hent *lookup __P((const char *));
    104 static int	qucmp __P((const void *, const void *));
    105 static void	rewrite __P((void));
    106 static void	usage __P((void));
    107 int		main __P((int, char * const []));
    108 
    109 int
    110 main(argc, argv)
    111 	int argc;
    112 	char * const argv[];
    113 {
    114 	FILE *acct;
    115 	char *cp;
    116 	int opt;
    117 
    118 	while ((opt = getopt(argc, argv, "P:p:scmr")) != -1) {
    119 		switch(opt) {
    120 		case 'P':
    121 			/*
    122 			 * Printer name.
    123 			 */
    124 			printer = cp;
    125 			continue;
    126 
    127 		case 'p':
    128 			/*
    129 			 * get the price.
    130 			 */
    131 			price = atof(cp);
    132 			pflag = 1;
    133 			continue;
    134 
    135 		case 's':
    136 			/*
    137 			 * Summarize and compress accounting file.
    138 			 */
    139 			summarize++;
    140 			continue;
    141 
    142 		case 'c':
    143 			/*
    144 			 * Sort by cost.
    145 			 */
    146 			sort++;
    147 			continue;
    148 
    149 		case 'm':
    150 			/*
    151 			 * disregard machine names for each user
    152 			 */
    153 			mflag = 1;
    154 			continue;
    155 
    156 		case 'r':
    157 			/*
    158 			 * Reverse sorting order.
    159 			 */
    160 			reverse++;
    161 			continue;
    162 
    163 		default:
    164 			usage();
    165 			/* NOTREACHED */
    166 		}
    167 	}
    168 	argc -= optind;
    169 	argv += optind;
    170 
    171 	/*
    172 	 * If there are any arguments left, they're names of users
    173 	 * we want to print info for. In that case, put them in the hash
    174 	 * table and unset allflag.
    175 	 */
    176 	for( ; argc > 0; argc--, argv++) {
    177 		(void)enter(*argv);
    178 		allflag = 0;
    179 	}
    180 
    181 	if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
    182 		printer = DEFLP;
    183 	if (!chkprinter(printer)) {
    184 		printf("pac: unknown printer %s\n", printer);
    185 		exit(2);
    186 	}
    187 
    188 	if ((acct = fopen(acctfile, "r")) == NULL)
    189 		err(1, "%s", acctfile);
    190 	account(acct);
    191 	fclose(acct);
    192 	if ((acct = fopen(sumfile, "r")) != NULL) {
    193 		account(acct);
    194 		fclose(acct);
    195 	}
    196 	if (summarize)
    197 		rewrite();
    198 	else
    199 		dumpit();
    200 	exit(errs);
    201 }
    202 
    203 /*
    204  * Read the entire accounting file, accumulating statistics
    205  * for the users that we have in the hash table.  If allflag
    206  * is set, then just gather the facts on everyone.
    207  * Note that we must accomodate both the active and summary file
    208  * formats here.
    209  * Host names are ignored if the -m flag is present.
    210  */
    211 static void
    212 account(acct)
    213 	FILE *acct;
    214 {
    215 	char linebuf[BUFSIZ];
    216 	double t;
    217 	char *cp, *cp2;
    218 	struct hent *hp;
    219 	int ic;
    220 
    221 	while (fgets(linebuf, BUFSIZ, acct) != NULL) {
    222 		cp = linebuf;
    223 		while (any(*cp, " \t"))
    224 			cp++;
    225 		t = atof(cp);
    226 		while (any(*cp, ".0123456789"))
    227 			cp++;
    228 		while (any(*cp, " \t"))
    229 			cp++;
    230 		for (cp2 = cp; !any(*cp2, " \t\n"); cp2++)
    231 			;
    232 		ic = atoi(cp2);
    233 		*cp2 = '\0';
    234 		if (mflag && strchr(cp, ':'))
    235 		    cp = strchr(cp, ':') + 1;
    236 		hp = lookup(cp);
    237 		if (hp == NULL) {
    238 			if (!allflag)
    239 				continue;
    240 			hp = enter(cp);
    241 		}
    242 		hp->h_feetpages += t;
    243 		if (ic)
    244 			hp->h_count += ic;
    245 		else
    246 			hp->h_count++;
    247 	}
    248 }
    249 
    250 /*
    251  * Sort the hashed entries by name or footage
    252  * and print it all out.
    253  */
    254 static void
    255 dumpit()
    256 {
    257 	struct hent **base;
    258 	struct hent *hp, **ap;
    259 	int hno, c, runs;
    260 	float feet;
    261 
    262 	hp = hashtab[0];
    263 	hno = 1;
    264 	base = (struct hent **) calloc(sizeof hp, hcount);
    265 	if (base == NULL)
    266 		err(1, "calloc");
    267 	for (ap = base, c = hcount; c--; ap++) {
    268 		while (hp == NULL)
    269 			hp = hashtab[hno++];
    270 		*ap = hp;
    271 		hp = hp->h_link;
    272 	}
    273 	qsort(base, hcount, sizeof hp, qucmp);
    274 	printf(" pages/feet runs price    %s\n",
    275 		(mflag ? "login" : "host name and login"));
    276 	printf(" ---------- ---- -------- ----------------------\n");
    277 	feet = 0.0;
    278 	runs = 0;
    279 	for (ap = base, c = hcount; c--; ap++) {
    280 		hp = *ap;
    281 		runs += hp->h_count;
    282 		feet += hp->h_feetpages;
    283 		printf("    %7.2f %4d $%7.2f %s\n",
    284 			hp->h_feetpages, hp->h_count, hp->h_feetpages * price,
    285 			hp->h_name);
    286 	}
    287 	if (allflag) {
    288 		printf(" ---------- ---- -------- ----------------------\n");
    289 		printf("Sum:%7.2f %4d $%7.2f\n", feet, runs, feet * price);
    290 	}
    291 }
    292 
    293 /*
    294  * Rewrite the summary file with the summary information we have accumulated.
    295  */
    296 static void
    297 rewrite()
    298 {
    299 	struct hent *hp;
    300 	int i;
    301 	FILE *acctf;
    302 
    303 	if ((acctf = fopen(sumfile, "w")) == NULL) {
    304 		warn("%s", sumfile);
    305 		errs++;
    306 		return;
    307 	}
    308 	for (i = 0; i < HSHSIZE; i++) {
    309 		hp = hashtab[i];
    310 		while (hp != NULL) {
    311 			fprintf(acctf, "%7.2f\t%s\t%d\n", hp->h_feetpages,
    312 			    hp->h_name, hp->h_count);
    313 			hp = hp->h_link;
    314 		}
    315 	}
    316 	fflush(acctf);
    317 	if (ferror(acctf)) {
    318 		warn("%s", sumfile);
    319 		errs++;
    320 	}
    321 	fclose(acctf);
    322 	if ((acctf = fopen(acctfile, "w")) == NULL)
    323 		warn("%s", acctfile);
    324 	else
    325 		fclose(acctf);
    326 }
    327 
    328 /*
    329  * Hashing routines.
    330  */
    331 
    332 /*
    333  * Enter the name into the hash table and return the pointer allocated.
    334  */
    335 
    336 static struct hent *
    337 enter(name)
    338 	const char *name;
    339 {
    340 	struct hent *hp;
    341 	int h;
    342 
    343 	if ((hp = lookup(name)) != NULL)
    344 		return(hp);
    345 	h = hash(name);
    346 	hcount++;
    347 	hp = (struct hent *) calloc(sizeof *hp, 1);
    348 	if (hp == NULL)
    349 		err(1, "calloc");
    350 	hp->h_name = strdup(name);
    351 	if (hp->h_name == NULL)
    352 		err(1, "malloc");
    353 	hp->h_feetpages = 0.0;
    354 	hp->h_count = 0;
    355 	hp->h_link = hashtab[h];
    356 	hashtab[h] = hp;
    357 	return(hp);
    358 }
    359 
    360 /*
    361  * Lookup a name in the hash table and return a pointer
    362  * to it.
    363  */
    364 
    365 static struct hent *
    366 lookup(name)
    367 	const char *name;
    368 {
    369 	int h;
    370 	struct hent *hp;
    371 
    372 	h = hash(name);
    373 	for (hp = hashtab[h]; hp != NULL; hp = hp->h_link)
    374 		if (strcmp(hp->h_name, name) == 0)
    375 			return(hp);
    376 	return(NULL);
    377 }
    378 
    379 /*
    380  * Hash the passed name and return the index in
    381  * the hash table to begin the search.
    382  */
    383 static int
    384 hash(name)
    385 	const char *name;
    386 {
    387 	int h;
    388 	const char *cp;
    389 
    390 	for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
    391 		;
    392 	return((h & 0x7fffffff) % HSHSIZE);
    393 }
    394 
    395 /*
    396  * Other stuff
    397  */
    398 static int
    399 any(ch, str)
    400 	int ch;
    401 	const char *str;
    402 {
    403 	int c = ch;
    404 	const char *cp = str;
    405 
    406 	while (*cp)
    407 		if (*cp++ == c)
    408 			return(1);
    409 	return(0);
    410 }
    411 
    412 /*
    413  * The qsort comparison routine.
    414  * The comparison is ascii collating order
    415  * or by feet of typesetter film, according to sort.
    416  */
    417 static int
    418 qucmp(a, b)
    419 	const void *a, *b;
    420 {
    421 	struct hent *h1, *h2;
    422 	int r;
    423 
    424 	h1 = *(struct hent **)a;
    425 	h2 = *(struct hent **)b;
    426 	if (sort)
    427 		r = h1->h_feetpages < h2->h_feetpages ?
    428 		    -1 : h1->h_feetpages > h2->h_feetpages;
    429 	else
    430 		r = strcmp(h1->h_name, h2->h_name);
    431 	return(reverse ? -r : r);
    432 }
    433 
    434 /*
    435  * Perform lookup for printer name or abbreviation --
    436  */
    437 static int
    438 chkprinter(s)
    439 	const char *s;
    440 {
    441 	int stat;
    442 
    443 	if ((stat = cgetent(&bp, printcapdb, s)) == -2) {
    444 		printf("pac: can't open printer description file\n");
    445 		exit(3);
    446 	} else if (stat == -1)
    447 		return(0);
    448 	else if (stat == -3)
    449 		fatal("potential reference loop detected in printcap file");
    450 
    451 	if (cgetstr(bp, "af", &acctfile) == -1) {
    452 		printf("accounting not enabled for printer %s\n", printer);
    453 		exit(2);
    454 	}
    455 	if (!pflag && (cgetnum(bp, "pc", &price100) == 0))
    456 		price = price100/10000.0;
    457 	sumfile = (char *) calloc(sizeof(char), strlen(acctfile)+5);
    458 	if (sumfile == NULL)
    459 		err(1, "pac");
    460 	strcpy(sumfile, acctfile);	/* XXX: strcpy is safe */
    461 	strcat(sumfile, "_sum");	/* XXX: strcat is safe */
    462 	return(1);
    463 }
    464 
    465 static void
    466 usage()
    467 {
    468 	fprintf(stderr,
    469 	  "usage: pac [-Pprinter] [-pprice] [-s] [-c] [-r] [-m] [user ...]\n");
    470 	exit(1);
    471 }
    472