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