2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 static const char copyright[] =
35 "@(#) Copyright (c) 1983, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)pac.c 8.1 (Berkeley) 6/6/93";
45 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
46 __FBSDID("$FreeBSD$");
49 * Do Printer accounting summary.
51 * pac [-Pprinter] [-pprice] [-s] [-r] [-c] [-m] [user ...]
52 * to print the usage information for the named people.
55 #include <sys/param.h>
66 static char *acctfile; /* accounting file (input data) */
67 static int allflag = 1; /* Get stats on everybody */
69 static size_t hcount; /* Count of hash entries */
70 static int mflag = 0; /* disregard machine names */
71 static int pflag = 0; /* 1 if -p on cmd line */
72 static float price = 0.02; /* cost per page (or what ever) */
73 static int reverse; /* Reverse sort order */
74 static int sort; /* Sort by cost */
75 static char *sumfile; /* summary file */
76 static int summarize; /* Compress accounting file */
82 * Names to be accumulated are hashed into the following
86 #define HSHSIZE 97 /* Number of hash buckets */
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 */
95 static struct hent *hashtab[HSHSIZE]; /* Hash table proper */
97 int main(int argc, char **_argv);
98 static void account(FILE *_acctf);
99 static int any(int _ch, const char _str[]);
100 static int chkprinter(const char *_ptrname);
101 static void dumpit(void);
102 static int hash(const char _name[]);
103 static struct hent *enter(const char _name[]);
104 static struct hent *lookup(const char _name[]);
105 static int qucmp(const void *_a, const void *_b);
106 static void rewrite(void);
107 static void usage(void);
110 main(int argc, char **argv)
113 const char *cp, *printer;
116 euid = geteuid(); /* these aren't used in pac(1) */
139 * Summarize and compress accounting file.
153 * disregard machine names for each user
160 * Reverse sorting order.
172 if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
174 if (!chkprinter(printer)) {
175 printf("pac: unknown printer %s\n", printer);
179 if ((acctf = fopen(acctfile, "r")) == NULL) {
185 if ((acctf = fopen(sumfile, "r")) != NULL) {
200 "usage: pac [-Pprinter] [-pprice] [-s] [-c] [-r] [-m] [user ...]\n");
205 * Read the entire accounting file, accumulating statistics
206 * for the users that we have in the hash table. If allflag
207 * is set, then just gather the facts on everyone.
208 * Note that we must accommodate both the active and summary file
210 * Host names are ignored if the -m flag is present.
215 char linebuf[BUFSIZ];
217 register char *cp, *cp2;
218 register struct hent *hp;
221 while (fgets(linebuf, BUFSIZ, acctf) != NULL) {
223 while (any(*cp, " \t"))
226 while (any(*cp, ".0123456789"))
228 while (any(*cp, " \t"))
230 for (cp2 = cp; !any(*cp2, " \t\n"); cp2++)
234 if (mflag && strchr(cp, ':'))
235 cp = strchr(cp, ':') + 1;
242 hp->h_feetpages += t;
251 * Sort the hashed entries by name or footage
252 * and print it all out.
258 register struct hent *hp, **ap;
259 register int hno, runs;
265 base = (struct hent **) calloc(sizeof hp, hcount);
266 for (ap = base, c = hcount; c--; ap++) {
272 qsort(base, hcount, sizeof hp, qucmp);
273 printf(" Login pages/feet runs price\n");
276 for (ap = base, c = hcount; c--; ap++) {
279 feet += hp->h_feetpages;
280 printf("%-24s %7.2f %4d $%6.2f\n", hp->h_name,
281 hp->h_feetpages, hp->h_count, hp->h_feetpages * price);
285 printf("%-24s %7.2f %4d $%6.2f\n", "total", feet,
291 * Rewrite the summary file with the summary information we have accumulated.
296 register struct hent *hp;
300 if ((acctf = fopen(sumfile, "w")) == NULL) {
305 for (i = 0; i < HSHSIZE; i++) {
308 fprintf(acctf, "%7.2f\t%s\t%d\n", hp->h_feetpages,
309 hp->h_name, hp->h_count);
319 if ((acctf = fopen(acctfile, "w")) == NULL)
320 warn("%s", acctfile);
330 * Enter the name into the hash table and return the pointer allocated.
334 enter(const char name[])
336 register struct hent *hp;
339 if ((hp = lookup(name)) != NULL)
343 hp = (struct hent *) calloc(sizeof *hp, (size_t)1);
344 hp->h_name = (char *) calloc(sizeof(char), strlen(name)+1);
345 strcpy(hp->h_name, name);
346 hp->h_feetpages = 0.0;
348 hp->h_link = hashtab[h];
354 * Lookup a name in the hash table and return a pointer
359 lookup(const char name[])
362 register struct hent *hp;
365 for (hp = hashtab[h]; hp != NULL; hp = hp->h_link)
366 if (strcmp(hp->h_name, name) == 0)
372 * Hash the passed name and return the index in
373 * the hash table to begin the search.
376 hash(const char name[])
379 register const char *cp;
381 for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
383 return((h & 0x7fffffff) % HSHSIZE);
390 any(int ch, const char str[])
393 register const char *cp = str;
402 * The qsort comparison routine.
403 * The comparison is ascii collating order
404 * or by feet of typesetter film, according to sort.
407 qucmp(const void *a, const void *b)
409 register const struct hent *h1, *h2;
412 h1 = *(const struct hent * const *)a;
413 h2 = *(const struct hent * const *)b;
415 r = h1->h_feetpages < h2->h_feetpages ?
416 -1 : h1->h_feetpages > h2->h_feetpages;
418 r = strcmp(h1->h_name, h2->h_name);
419 return(reverse ? -r : r);
423 * Perform lookup for printer name or abbreviation --
426 chkprinter(const char *ptrname)
429 struct printer myprinter, *pp = &myprinter;
431 init_printer(&myprinter);
432 stat = getprintcap(ptrname, pp);
435 printf("pac: getprintcap: %s\n", pcaperr(stat));
437 case PCAPERR_NOTFOUND:
440 fatal(pp, "%s", pcaperr(stat));
442 if ((acctfile = pp->acct_file) == NULL)
443 errx(3, "accounting not enabled for printer %s", ptrname);
444 if (!pflag && pp->price100)
445 price = pp->price100/10000.0;
446 sumfile = (char *) calloc(sizeof(char), strlen(acctfile)+5);
448 errx(1, "calloc failed");
449 strcpy(sumfile, acctfile);
450 strcat(sumfile, "_sum");