xref: /freebsd/usr.sbin/lpr/pac/pac.c (revision 4a1a0dbedbb3fa74041adb9862f4608cd990a3c5)
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: pac.c,v 1.7 1997/09/24 06:48:24 charnier Exp $";
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 	FILE *acct;
114 	char *cp, *printer;
115 
116 	printer = NULL;
117 	euid = geteuid();	/* these aren't used in pac(1) */
118 	uid = getuid();
119 	while (--argc) {
120 		cp = *++argv;
121 		if (*cp++ == '-') {
122 			switch(*cp++) {
123 			case 'P':
124 				/*
125 				 * Printer name.
126 				 */
127 				printer = cp;
128 				continue;
129 
130 			case 'p':
131 				/*
132 				 * get the price.
133 				 */
134 				price = atof(cp);
135 				pflag = 1;
136 				continue;
137 
138 			case 's':
139 				/*
140 				 * Summarize and compress accounting file.
141 				 */
142 				summarize++;
143 				continue;
144 
145 			case 'c':
146 				/*
147 				 * Sort by cost.
148 				 */
149 				sort++;
150 				continue;
151 
152 			case 'm':
153 				/*
154 				 * disregard machine names for each user
155 				 */
156 				mflag = 1;
157 				continue;
158 
159 			case 'r':
160 				/*
161 				 * Reverse sorting order.
162 				 */
163 				reverse++;
164 				continue;
165 
166 			default:
167 				usage();
168 			}
169 		}
170 		(void) enter(--cp);
171 		allflag = 0;
172 	}
173 	if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
174 		printer = DEFLP;
175 	if (!chkprinter(printer)) {
176 		printf("pac: unknown printer %s\n", printer);
177 		exit(2);
178 	}
179 
180 	if ((acct = fopen(acctfile, "r")) == NULL) {
181 		perror(acctfile);
182 		exit(1);
183 	}
184 	account(acct);
185 	fclose(acct);
186 	if ((acct = fopen(sumfile, "r")) != NULL) {
187 		account(acct);
188 		fclose(acct);
189 	}
190 	if (summarize)
191 		rewrite();
192 	else
193 		dumpit();
194 	exit(errs);
195 }
196 
197 static void
198 usage()
199 {
200 	fprintf(stderr,
201 	"usage: pac [-Pprinter] [-pprice] [-s] [-c] [-r] [-m] [user ...]\n");
202 	exit(1);
203 }
204 
205 /*
206  * Read the entire accounting file, accumulating statistics
207  * for the users that we have in the hash table.  If allflag
208  * is set, then just gather the facts on everyone.
209  * Note that we must accomodate both the active and summary file
210  * formats here.
211  * Host names are ignored if the -m flag is present.
212  */
213 static void
214 account(acct)
215 	register FILE *acct;
216 {
217 	char linebuf[BUFSIZ];
218 	double t;
219 	register char *cp, *cp2;
220 	register struct hent *hp;
221 	register int ic;
222 
223 	while (fgets(linebuf, BUFSIZ, acct) != NULL) {
224 		cp = linebuf;
225 		while (any(*cp, " \t"))
226 			cp++;
227 		t = atof(cp);
228 		while (any(*cp, ".0123456789"))
229 			cp++;
230 		while (any(*cp, " \t"))
231 			cp++;
232 		for (cp2 = cp; !any(*cp2, " \t\n"); cp2++)
233 			;
234 		ic = atoi(cp2);
235 		*cp2 = '\0';
236 		if (mflag && strchr(cp, ':'))
237 		    cp = strchr(cp, ':') + 1;
238 		hp = lookup(cp);
239 		if (hp == NULL) {
240 			if (!allflag)
241 				continue;
242 			hp = enter(cp);
243 		}
244 		hp->h_feetpages += t;
245 		if (ic)
246 			hp->h_count += ic;
247 		else
248 			hp->h_count++;
249 	}
250 }
251 
252 /*
253  * Sort the hashed entries by name or footage
254  * and print it all out.
255  */
256 static void
257 dumpit()
258 {
259 	struct hent **base;
260 	register struct hent *hp, **ap;
261 	register int hno, c, runs;
262 	float feet;
263 
264 	hp = hashtab[0];
265 	hno = 1;
266 	base = (struct hent **) calloc(sizeof hp, hcount);
267 	for (ap = base, c = hcount; c--; ap++) {
268 		while (hp == NULL)
269 			hp = hashtab[hno++];
270 		*ap = hp;
271 		hp = hp->h_link;
272 	}
273 	qsort(base, hcount, sizeof hp, qucmp);
274 	printf("  Login               pages/feet   runs    price\n");
275 	feet = 0.0;
276 	runs = 0;
277 	for (ap = base, c = hcount; c--; ap++) {
278 		hp = *ap;
279 		runs += hp->h_count;
280 		feet += hp->h_feetpages;
281 		printf("%-24s %7.2f %4d   $%6.2f\n", hp->h_name,
282 		    hp->h_feetpages, hp->h_count, hp->h_feetpages * price);
283 	}
284 	if (allflag) {
285 		printf("\n");
286 		printf("%-24s %7.2f %4d   $%6.2f\n", "total", feet,
287 		    runs, feet * price);
288 	}
289 }
290 
291 /*
292  * Rewrite the summary file with the summary information we have accumulated.
293  */
294 static void
295 rewrite()
296 {
297 	register struct hent *hp;
298 	register int i;
299 	register FILE *acctf;
300 
301 	if ((acctf = fopen(sumfile, "w")) == NULL) {
302 		warn("%s", sumfile);
303 		errs++;
304 		return;
305 	}
306 	for (i = 0; i < HSHSIZE; i++) {
307 		hp = hashtab[i];
308 		while (hp != NULL) {
309 			fprintf(acctf, "%7.2f\t%s\t%d\n", hp->h_feetpages,
310 			    hp->h_name, hp->h_count);
311 			hp = hp->h_link;
312 		}
313 	}
314 	fflush(acctf);
315 	if (ferror(acctf)) {
316 		warn("%s", sumfile);
317 		errs++;
318 	}
319 	fclose(acctf);
320 	if ((acctf = fopen(acctfile, "w")) == NULL)
321 		warn("%s", acctfile);
322 	else
323 		fclose(acctf);
324 }
325 
326 /*
327  * Hashing routines.
328  */
329 
330 /*
331  * Enter the name into the hash table and return the pointer allocated.
332  */
333 
334 static struct hent *
335 enter(name)
336 	char name[];
337 {
338 	register struct hent *hp;
339 	register int h;
340 
341 	if ((hp = lookup(name)) != NULL)
342 		return(hp);
343 	h = hash(name);
344 	hcount++;
345 	hp = (struct hent *) calloc(sizeof *hp, 1);
346 	hp->h_name = (char *) calloc(sizeof(char), strlen(name)+1);
347 	strcpy(hp->h_name, name);
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(name)
362 	char name[];
363 {
364 	register int h;
365 	register struct hent *hp;
366 
367 	h = hash(name);
368 	for (hp = hashtab[h]; hp != NULL; hp = hp->h_link)
369 		if (strcmp(hp->h_name, name) == 0)
370 			return(hp);
371 	return(NULL);
372 }
373 
374 /*
375  * Hash the passed name and return the index in
376  * the hash table to begin the search.
377  */
378 static int
379 hash(name)
380 	char name[];
381 {
382 	register int h;
383 	register char *cp;
384 
385 	for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
386 		;
387 	return((h & 0x7fffffff) % HSHSIZE);
388 }
389 
390 /*
391  * Other stuff
392  */
393 static int
394 any(ch, str)
395 	int ch;
396 	char str[];
397 {
398 	register int c = ch;
399 	register char *cp = str;
400 
401 	while (*cp)
402 		if (*cp++ == c)
403 			return(1);
404 	return(0);
405 }
406 
407 /*
408  * The qsort comparison routine.
409  * The comparison is ascii collating order
410  * or by feet of typesetter film, according to sort.
411  */
412 static int
413 qucmp(a, b)
414 	const void *a, *b;
415 {
416 	register struct hent *h1, *h2;
417 	register int r;
418 
419 	h1 = *(struct hent **)a;
420 	h2 = *(struct hent **)b;
421 	if (sort)
422 		r = h1->h_feetpages < h2->h_feetpages ?
423 		    -1 : h1->h_feetpages > h2->h_feetpages;
424 	else
425 		r = strcmp(h1->h_name, h2->h_name);
426 	return(reverse ? -r : r);
427 }
428 
429 /*
430  * Perform lookup for printer name or abbreviation --
431  */
432 static int
433 chkprinter(s)
434 	register char *s;
435 {
436 	int stat;
437 	struct printer myprinter, *pp = &myprinter;
438 
439 	init_printer(&myprinter);
440 	stat = getprintcap(s, pp);
441 	switch(stat) {
442 	case PCAPERR_OSERR:
443 		printf("pac: getprintcap: %s\n", pcaperr(stat));
444 		exit(3);
445 	case PCAPERR_NOTFOUND:
446 		return 0;
447 	case PCAPERR_TCLOOP:
448 		fatal(pp, "%s", pcaperr(stat));
449 	}
450 	acctfile = pp->acct_file;
451 	if (!pflag && pp->price100)
452 		price = pp->price100/10000.0;
453 	sumfile = (char *) calloc(sizeof(char), strlen(acctfile)+5);
454 	if (sumfile == NULL)
455 		errx(1, "calloc failed");
456 	strcpy(sumfile, acctfile);
457 	strcat(sumfile, "_sum");
458 	return(1);
459 }
460