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