2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char copyright[] =
33 "@(#) Copyright (c) 1983, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
39 static char sccsid[] = "@(#)pac.c 8.1 (Berkeley) 6/6/93";
43 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
44 __FBSDID("$FreeBSD$");
47 * Do Printer accounting summary.
49 * pac [-Pprinter] [-pprice] [-s] [-r] [-c] [-m] [user ...]
50 * to print the usage information for the named people.
53 #include <sys/param.h>
64 static char *acctfile; /* accounting file (input data) */
65 static int allflag = 1; /* Get stats on everybody */
67 static size_t hcount; /* Count of hash entries */
68 static int mflag = 0; /* disregard machine names */
69 static int pflag = 0; /* 1 if -p on cmd line */
70 static float price = 0.02; /* cost per page (or what ever) */
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 */
80 * Names to be accumulated are hashed into the following
84 #define HSHSIZE 97 /* Number of hash buckets */
87 struct hent *h_link; /* Forward hash link */
88 char *h_name; /* Name of this user */
89 float h_feetpages; /* Feet or pages of paper */
90 int h_count; /* Number of runs */
93 static struct hent *hashtab[HSHSIZE]; /* Hash table proper */
95 int main(int argc, char **_argv);
96 static void account(FILE *_acctf);
97 static int any(int _ch, const char _str[]);
98 static int chkprinter(const char *_ptrname);
99 static void dumpit(void);
100 static int hash(const char _name[]);
101 static struct hent *enter(const char _name[]);
102 static struct hent *lookup(const char _name[]);
103 static int qucmp(const void *_a, const void *_b);
104 static void rewrite(void);
105 static void usage(void);
108 main(int argc, char **argv)
111 const char *cp, *printer;
114 euid = geteuid(); /* these aren't used in pac(1) */
137 * Summarize and compress accounting file.
151 * disregard machine names for each user
158 * Reverse sorting order.
170 if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
172 if (!chkprinter(printer)) {
173 printf("pac: unknown printer %s\n", printer);
177 if ((acctf = fopen(acctfile, "r")) == NULL) {
183 if ((acctf = fopen(sumfile, "r")) != NULL) {
198 "usage: pac [-Pprinter] [-pprice] [-s] [-c] [-r] [-m] [user ...]\n");
203 * Read the entire accounting file, accumulating statistics
204 * for the users that we have in the hash table. If allflag
205 * is set, then just gather the facts on everyone.
206 * Note that we must accommodate both the active and summary file
208 * Host names are ignored if the -m flag is present.
213 char linebuf[BUFSIZ];
215 register char *cp, *cp2;
216 register struct hent *hp;
219 while (fgets(linebuf, BUFSIZ, acctf) != NULL) {
221 while (any(*cp, " \t"))
224 while (any(*cp, ".0123456789"))
226 while (any(*cp, " \t"))
228 for (cp2 = cp; !any(*cp2, " \t\n"); cp2++)
232 if (mflag && strchr(cp, ':'))
233 cp = strchr(cp, ':') + 1;
240 hp->h_feetpages += t;
249 * Sort the hashed entries by name or footage
250 * and print it all out.
256 register struct hent *hp, **ap;
257 register int hno, runs;
263 base = (struct hent **) calloc(sizeof hp, hcount);
264 for (ap = base, c = hcount; c--; ap++) {
270 qsort(base, hcount, sizeof hp, qucmp);
271 printf(" Login pages/feet runs price\n");
274 for (ap = base, c = hcount; c--; ap++) {
277 feet += hp->h_feetpages;
278 printf("%-24s %7.2f %4d $%6.2f\n", hp->h_name,
279 hp->h_feetpages, hp->h_count, hp->h_feetpages * price);
283 printf("%-24s %7.2f %4d $%6.2f\n", "total", feet,
289 * Rewrite the summary file with the summary information we have accumulated.
294 register struct hent *hp;
298 if ((acctf = fopen(sumfile, "w")) == NULL) {
303 for (i = 0; i < HSHSIZE; i++) {
306 fprintf(acctf, "%7.2f\t%s\t%d\n", hp->h_feetpages,
307 hp->h_name, hp->h_count);
317 if ((acctf = fopen(acctfile, "w")) == NULL)
318 warn("%s", acctfile);
328 * Enter the name into the hash table and return the pointer allocated.
332 enter(const char name[])
334 register struct hent *hp;
337 if ((hp = lookup(name)) != NULL)
341 hp = (struct hent *) calloc(sizeof *hp, (size_t)1);
342 hp->h_name = (char *) calloc(sizeof(char), strlen(name)+1);
343 strcpy(hp->h_name, name);
344 hp->h_feetpages = 0.0;
346 hp->h_link = hashtab[h];
352 * Lookup a name in the hash table and return a pointer
357 lookup(const char name[])
360 register struct hent *hp;
363 for (hp = hashtab[h]; hp != NULL; hp = hp->h_link)
364 if (strcmp(hp->h_name, name) == 0)
370 * Hash the passed name and return the index in
371 * the hash table to begin the search.
374 hash(const char name[])
377 register const char *cp;
379 for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
381 return((h & 0x7fffffff) % HSHSIZE);
388 any(int ch, const char str[])
391 register const char *cp = str;
400 * The qsort comparison routine.
401 * The comparison is ascii collating order
402 * or by feet of typesetter film, according to sort.
405 qucmp(const void *a, const void *b)
407 register const struct hent *h1, *h2;
410 h1 = *(const struct hent * const *)a;
411 h2 = *(const struct hent * const *)b;
413 r = h1->h_feetpages < h2->h_feetpages ?
414 -1 : h1->h_feetpages > h2->h_feetpages;
416 r = strcmp(h1->h_name, h2->h_name);
417 return(reverse ? -r : r);
421 * Perform lookup for printer name or abbreviation --
424 chkprinter(const char *ptrname)
427 struct printer myprinter, *pp = &myprinter;
429 init_printer(&myprinter);
430 stat = getprintcap(ptrname, pp);
433 printf("pac: getprintcap: %s\n", pcaperr(stat));
435 case PCAPERR_NOTFOUND:
438 fatal(pp, "%s", pcaperr(stat));
440 if ((acctfile = pp->acct_file) == NULL)
441 errx(3, "accounting not enabled for printer %s", ptrname);
442 if (!pflag && pp->price100)
443 price = pp->price100/10000.0;
444 sumfile = (char *) calloc(sizeof(char), strlen(acctfile)+5);
446 errx(1, "calloc failed");
447 strcpy(sumfile, acctfile);
448 strcat(sumfile, "_sum");