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