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