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