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