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