xref: /freebsd/usr.sbin/lpr/pac/pac.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
4dea673e9SRodney W. Grimes  * Copyright (c) 1983, 1993
5dea673e9SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6dea673e9SRodney W. Grimes  *
7dea673e9SRodney W. Grimes  *
8dea673e9SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9dea673e9SRodney W. Grimes  * modification, are permitted provided that the following conditions
10dea673e9SRodney W. Grimes  * are met:
11dea673e9SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13dea673e9SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15dea673e9SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
17dea673e9SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18dea673e9SRodney W. Grimes  *    without specific prior written permission.
19dea673e9SRodney W. Grimes  *
20dea673e9SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21dea673e9SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22dea673e9SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23dea673e9SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24dea673e9SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25dea673e9SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26dea673e9SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27dea673e9SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28dea673e9SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29dea673e9SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30dea673e9SRodney W. Grimes  * SUCH DAMAGE.
31dea673e9SRodney W. Grimes  */
32dea673e9SRodney W. Grimes 
338e36ed92SGarance A Drosehn #include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
34dea673e9SRodney W. Grimes /*
35dea673e9SRodney W. Grimes  * Do Printer accounting summary.
36dea673e9SRodney W. Grimes  * Currently, usage is
37dea673e9SRodney W. Grimes  *	pac [-Pprinter] [-pprice] [-s] [-r] [-c] [-m] [user ...]
38dea673e9SRodney W. Grimes  * to print the usage information for the named people.
39dea673e9SRodney W. Grimes  */
40dea673e9SRodney W. Grimes 
41dea673e9SRodney W. Grimes #include <sys/param.h>
42dea673e9SRodney W. Grimes 
43dea673e9SRodney W. Grimes #include <dirent.h>
4490cf373dSGarrett Wollman #include <err.h>
45dea673e9SRodney W. Grimes #include <stdlib.h>
46dea673e9SRodney W. Grimes #include <stdio.h>
47dea673e9SRodney W. Grimes #include <string.h>
4890cf373dSGarrett Wollman #include <unistd.h>
49dea673e9SRodney W. Grimes #include "lp.h"
50dea673e9SRodney W. Grimes #include "lp.local.h"
51dea673e9SRodney W. Grimes 
52dea673e9SRodney W. Grimes static char	*acctfile;	/* accounting file (input data) */
53dea673e9SRodney W. Grimes static int	 allflag = 1;	/* Get stats on everybody */
54dea673e9SRodney W. Grimes static int	 errs;
55961d3645SGarance A Drosehn static size_t	 hcount;	/* Count of hash entries */
56dea673e9SRodney W. Grimes static int	 mflag = 0;	/* disregard machine names */
57dea673e9SRodney W. Grimes static int	 pflag = 0;	/* 1 if -p on cmd line */
58dea673e9SRodney W. Grimes static float	 price = 0.02;	/* cost per page (or what ever) */
59dea673e9SRodney W. Grimes static int	 reverse;	/* Reverse sort order */
60dea673e9SRodney W. Grimes static int	 sort;		/* Sort by cost */
61dea673e9SRodney W. Grimes static char	*sumfile;	/* summary file */
62dea673e9SRodney W. Grimes static int	 summarize;	/* Compress accounting file */
63dea673e9SRodney W. Grimes 
64360d4ad5SWarner Losh uid_t	uid, euid;
65360d4ad5SWarner Losh 
66dea673e9SRodney W. Grimes /*
67dea673e9SRodney W. Grimes  * Grossness follows:
68dea673e9SRodney W. Grimes  *  Names to be accumulated are hashed into the following
69dea673e9SRodney W. Grimes  *  table.
70dea673e9SRodney W. Grimes  */
71dea673e9SRodney W. Grimes 
72dea673e9SRodney W. Grimes #define	HSHSIZE	97			/* Number of hash buckets */
73dea673e9SRodney W. Grimes 
74dea673e9SRodney W. Grimes struct hent {
75dea673e9SRodney W. Grimes 	struct	hent *h_link;		/* Forward hash link */
76dea673e9SRodney W. Grimes 	char	*h_name;		/* Name of this user */
77dea673e9SRodney W. Grimes 	float	h_feetpages;		/* Feet or pages of paper */
78dea673e9SRodney W. Grimes 	int	h_count;		/* Number of runs */
79dea673e9SRodney W. Grimes };
80dea673e9SRodney W. Grimes 
81dea673e9SRodney W. Grimes static struct	hent	*hashtab[HSHSIZE];	/* Hash table proper */
82dea673e9SRodney W. Grimes 
83ba7a1ad7SGarance A Drosehn int		 main(int argc, char **_argv);
84961d3645SGarance A Drosehn static void	 account(FILE *_acctf);
85ba7a1ad7SGarance A Drosehn static int	 any(int _ch, const char _str[]);
86ba7a1ad7SGarance A Drosehn static int	 chkprinter(const char *_ptrname);
87ba7a1ad7SGarance A Drosehn static void	 dumpit(void);
88ba7a1ad7SGarance A Drosehn static int	 hash(const char _name[]);
89ba7a1ad7SGarance A Drosehn static struct hent 	*enter(const char _name[]);
90ba7a1ad7SGarance A Drosehn static struct hent 	*lookup(const char _name[]);
91ba7a1ad7SGarance A Drosehn static int	 qucmp(const void *_a, const void *_b);
92ba7a1ad7SGarance A Drosehn static void	 rewrite(void);
93ba7a1ad7SGarance A Drosehn static void	 usage(void);
94dea673e9SRodney W. Grimes 
95bc407914SWarner Losh int
main(int argc,char ** argv)96ba7a1ad7SGarance A Drosehn main(int argc, char **argv)
97dea673e9SRodney W. Grimes {
98961d3645SGarance A Drosehn 	FILE *acctf;
99ba7a1ad7SGarance A Drosehn 	const char *cp, *printer;
100dea673e9SRodney W. Grimes 
1014a1a0dbeSGarrett Wollman 	printer = NULL;
102360d4ad5SWarner Losh 	euid = geteuid();	/* these aren't used in pac(1) */
103360d4ad5SWarner Losh 	uid = getuid();
104dea673e9SRodney W. Grimes 	while (--argc) {
105dea673e9SRodney W. Grimes 		cp = *++argv;
106dea673e9SRodney W. Grimes 		if (*cp++ == '-') {
107dea673e9SRodney W. Grimes 			switch(*cp++) {
108dea673e9SRodney W. Grimes 			case 'P':
109dea673e9SRodney W. Grimes 				/*
110dea673e9SRodney W. Grimes 				 * Printer name.
111dea673e9SRodney W. Grimes 				 */
112dea673e9SRodney W. Grimes 				printer = cp;
113dea673e9SRodney W. Grimes 				continue;
114dea673e9SRodney W. Grimes 
115dea673e9SRodney W. Grimes 			case 'p':
116dea673e9SRodney W. Grimes 				/*
117dea673e9SRodney W. Grimes 				 * get the price.
118dea673e9SRodney W. Grimes 				 */
119dea673e9SRodney W. Grimes 				price = atof(cp);
120dea673e9SRodney W. Grimes 				pflag = 1;
121dea673e9SRodney W. Grimes 				continue;
122dea673e9SRodney W. Grimes 
123dea673e9SRodney W. Grimes 			case 's':
124dea673e9SRodney W. Grimes 				/*
125dea673e9SRodney W. Grimes 				 * Summarize and compress accounting file.
126dea673e9SRodney W. Grimes 				 */
127dea673e9SRodney W. Grimes 				summarize++;
128dea673e9SRodney W. Grimes 				continue;
129dea673e9SRodney W. Grimes 
130dea673e9SRodney W. Grimes 			case 'c':
131dea673e9SRodney W. Grimes 				/*
132dea673e9SRodney W. Grimes 				 * Sort by cost.
133dea673e9SRodney W. Grimes 				 */
134dea673e9SRodney W. Grimes 				sort++;
135dea673e9SRodney W. Grimes 				continue;
136dea673e9SRodney W. Grimes 
137dea673e9SRodney W. Grimes 			case 'm':
138dea673e9SRodney W. Grimes 				/*
139dea673e9SRodney W. Grimes 				 * disregard machine names for each user
140dea673e9SRodney W. Grimes 				 */
141dea673e9SRodney W. Grimes 				mflag = 1;
142dea673e9SRodney W. Grimes 				continue;
143dea673e9SRodney W. Grimes 
144dea673e9SRodney W. Grimes 			case 'r':
145dea673e9SRodney W. Grimes 				/*
146dea673e9SRodney W. Grimes 				 * Reverse sorting order.
147dea673e9SRodney W. Grimes 				 */
148dea673e9SRodney W. Grimes 				reverse++;
149dea673e9SRodney W. Grimes 				continue;
150dea673e9SRodney W. Grimes 
151dea673e9SRodney W. Grimes 			default:
1529b3fe531SPhilippe Charnier 				usage();
153dea673e9SRodney W. Grimes 			}
154dea673e9SRodney W. Grimes 		}
155dea673e9SRodney W. Grimes 		(void) enter(--cp);
156dea673e9SRodney W. Grimes 		allflag = 0;
157dea673e9SRodney W. Grimes 	}
158dea673e9SRodney W. Grimes 	if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
159dea673e9SRodney W. Grimes 		printer = DEFLP;
160dea673e9SRodney W. Grimes 	if (!chkprinter(printer)) {
161dea673e9SRodney W. Grimes 		printf("pac: unknown printer %s\n", printer);
162dea673e9SRodney W. Grimes 		exit(2);
163dea673e9SRodney W. Grimes 	}
164dea673e9SRodney W. Grimes 
165961d3645SGarance A Drosehn 	if ((acctf = fopen(acctfile, "r")) == NULL) {
166dea673e9SRodney W. Grimes 		perror(acctfile);
167dea673e9SRodney W. Grimes 		exit(1);
168dea673e9SRodney W. Grimes 	}
169961d3645SGarance A Drosehn 	account(acctf);
170961d3645SGarance A Drosehn 	fclose(acctf);
171961d3645SGarance A Drosehn 	if ((acctf = fopen(sumfile, "r")) != NULL) {
172961d3645SGarance A Drosehn 		account(acctf);
173961d3645SGarance A Drosehn 		fclose(acctf);
174dea673e9SRodney W. Grimes 	}
175dea673e9SRodney W. Grimes 	if (summarize)
176dea673e9SRodney W. Grimes 		rewrite();
177dea673e9SRodney W. Grimes 	else
178dea673e9SRodney W. Grimes 		dumpit();
179dea673e9SRodney W. Grimes 	exit(errs);
180dea673e9SRodney W. Grimes }
181dea673e9SRodney W. Grimes 
1829b3fe531SPhilippe Charnier static void
usage(void)183ba7a1ad7SGarance A Drosehn usage(void)
1849b3fe531SPhilippe Charnier {
1859b3fe531SPhilippe Charnier 	fprintf(stderr,
1869b3fe531SPhilippe Charnier 	"usage: pac [-Pprinter] [-pprice] [-s] [-c] [-r] [-m] [user ...]\n");
1879b3fe531SPhilippe Charnier 	exit(1);
1889b3fe531SPhilippe Charnier }
1899b3fe531SPhilippe Charnier 
190dea673e9SRodney W. Grimes /*
191dea673e9SRodney W. Grimes  * Read the entire accounting file, accumulating statistics
192dea673e9SRodney W. Grimes  * for the users that we have in the hash table.  If allflag
193dea673e9SRodney W. Grimes  * is set, then just gather the facts on everyone.
1943df5ecacSUlrich Spörlein  * Note that we must accommodate both the active and summary file
195dea673e9SRodney W. Grimes  * formats here.
196dea673e9SRodney W. Grimes  * Host names are ignored if the -m flag is present.
197dea673e9SRodney W. Grimes  */
198dea673e9SRodney W. Grimes static void
account(FILE * acctf)199961d3645SGarance A Drosehn account(FILE *acctf)
200dea673e9SRodney W. Grimes {
201dea673e9SRodney W. Grimes 	char linebuf[BUFSIZ];
202dea673e9SRodney W. Grimes 	double t;
203dea673e9SRodney W. Grimes 	register char *cp, *cp2;
204dea673e9SRodney W. Grimes 	register struct hent *hp;
205dea673e9SRodney W. Grimes 	register int ic;
206dea673e9SRodney W. Grimes 
207961d3645SGarance A Drosehn 	while (fgets(linebuf, BUFSIZ, acctf) != NULL) {
208dea673e9SRodney W. Grimes 		cp = linebuf;
2096689ae4bSPeter Wemm 		while (any(*cp, " \t"))
210dea673e9SRodney W. Grimes 			cp++;
211dea673e9SRodney W. Grimes 		t = atof(cp);
212dea673e9SRodney W. Grimes 		while (any(*cp, ".0123456789"))
213dea673e9SRodney W. Grimes 			cp++;
214dea673e9SRodney W. Grimes 		while (any(*cp, " \t"))
215dea673e9SRodney W. Grimes 			cp++;
216dea673e9SRodney W. Grimes 		for (cp2 = cp; !any(*cp2, " \t\n"); cp2++)
217dea673e9SRodney W. Grimes 			;
218dea673e9SRodney W. Grimes 		ic = atoi(cp2);
219dea673e9SRodney W. Grimes 		*cp2 = '\0';
220f8eb25daSWarner Losh 		if (mflag && strchr(cp, ':'))
221f8eb25daSWarner Losh 		    cp = strchr(cp, ':') + 1;
222dea673e9SRodney W. Grimes 		hp = lookup(cp);
223dea673e9SRodney W. Grimes 		if (hp == NULL) {
224dea673e9SRodney W. Grimes 			if (!allflag)
225dea673e9SRodney W. Grimes 				continue;
226dea673e9SRodney W. Grimes 			hp = enter(cp);
227dea673e9SRodney W. Grimes 		}
228dea673e9SRodney W. Grimes 		hp->h_feetpages += t;
229dea673e9SRodney W. Grimes 		if (ic)
230dea673e9SRodney W. Grimes 			hp->h_count += ic;
231dea673e9SRodney W. Grimes 		else
232dea673e9SRodney W. Grimes 			hp->h_count++;
233dea673e9SRodney W. Grimes 	}
234dea673e9SRodney W. Grimes }
235dea673e9SRodney W. Grimes 
236dea673e9SRodney W. Grimes /*
237dea673e9SRodney W. Grimes  * Sort the hashed entries by name or footage
238dea673e9SRodney W. Grimes  * and print it all out.
239dea673e9SRodney W. Grimes  */
240dea673e9SRodney W. Grimes static void
dumpit(void)241ba7a1ad7SGarance A Drosehn dumpit(void)
242dea673e9SRodney W. Grimes {
243dea673e9SRodney W. Grimes 	struct hent **base;
244dea673e9SRodney W. Grimes 	register struct hent *hp, **ap;
245961d3645SGarance A Drosehn 	register int hno, runs;
246961d3645SGarance A Drosehn 	size_t c;
247dea673e9SRodney W. Grimes 	float feet;
248dea673e9SRodney W. Grimes 
249dea673e9SRodney W. Grimes 	hp = hashtab[0];
250dea673e9SRodney W. Grimes 	hno = 1;
251dea673e9SRodney W. Grimes 	base = (struct hent **) calloc(sizeof hp, hcount);
252dea673e9SRodney W. Grimes 	for (ap = base, c = hcount; c--; ap++) {
253dea673e9SRodney W. Grimes 		while (hp == NULL)
254dea673e9SRodney W. Grimes 			hp = hashtab[hno++];
255dea673e9SRodney W. Grimes 		*ap = hp;
256dea673e9SRodney W. Grimes 		hp = hp->h_link;
257dea673e9SRodney W. Grimes 	}
258dea673e9SRodney W. Grimes 	qsort(base, hcount, sizeof hp, qucmp);
259dea673e9SRodney W. Grimes 	printf("  Login               pages/feet   runs    price\n");
260dea673e9SRodney W. Grimes 	feet = 0.0;
261dea673e9SRodney W. Grimes 	runs = 0;
262dea673e9SRodney W. Grimes 	for (ap = base, c = hcount; c--; ap++) {
263dea673e9SRodney W. Grimes 		hp = *ap;
264dea673e9SRodney W. Grimes 		runs += hp->h_count;
265dea673e9SRodney W. Grimes 		feet += hp->h_feetpages;
266dea673e9SRodney W. Grimes 		printf("%-24s %7.2f %4d   $%6.2f\n", hp->h_name,
267dea673e9SRodney W. Grimes 		    hp->h_feetpages, hp->h_count, hp->h_feetpages * price);
268dea673e9SRodney W. Grimes 	}
269dea673e9SRodney W. Grimes 	if (allflag) {
270dea673e9SRodney W. Grimes 		printf("\n");
271dea673e9SRodney W. Grimes 		printf("%-24s %7.2f %4d   $%6.2f\n", "total", feet,
272dea673e9SRodney W. Grimes 		    runs, feet * price);
273dea673e9SRodney W. Grimes 	}
274dea673e9SRodney W. Grimes }
275dea673e9SRodney W. Grimes 
276dea673e9SRodney W. Grimes /*
277dea673e9SRodney W. Grimes  * Rewrite the summary file with the summary information we have accumulated.
278dea673e9SRodney W. Grimes  */
279dea673e9SRodney W. Grimes static void
rewrite(void)280ba7a1ad7SGarance A Drosehn rewrite(void)
281dea673e9SRodney W. Grimes {
282dea673e9SRodney W. Grimes 	register struct hent *hp;
283dea673e9SRodney W. Grimes 	register int i;
284961d3645SGarance A Drosehn 	FILE *acctf;
285dea673e9SRodney W. Grimes 
286dea673e9SRodney W. Grimes 	if ((acctf = fopen(sumfile, "w")) == NULL) {
2879b3fe531SPhilippe Charnier 		warn("%s", sumfile);
288dea673e9SRodney W. Grimes 		errs++;
289dea673e9SRodney W. Grimes 		return;
290dea673e9SRodney W. Grimes 	}
291dea673e9SRodney W. Grimes 	for (i = 0; i < HSHSIZE; i++) {
292dea673e9SRodney W. Grimes 		hp = hashtab[i];
293dea673e9SRodney W. Grimes 		while (hp != NULL) {
294dea673e9SRodney W. Grimes 			fprintf(acctf, "%7.2f\t%s\t%d\n", hp->h_feetpages,
295dea673e9SRodney W. Grimes 			    hp->h_name, hp->h_count);
296dea673e9SRodney W. Grimes 			hp = hp->h_link;
297dea673e9SRodney W. Grimes 		}
298dea673e9SRodney W. Grimes 	}
299dea673e9SRodney W. Grimes 	fflush(acctf);
300dea673e9SRodney W. Grimes 	if (ferror(acctf)) {
3019b3fe531SPhilippe Charnier 		warn("%s", sumfile);
302dea673e9SRodney W. Grimes 		errs++;
303dea673e9SRodney W. Grimes 	}
304dea673e9SRodney W. Grimes 	fclose(acctf);
305dea673e9SRodney W. Grimes 	if ((acctf = fopen(acctfile, "w")) == NULL)
3069b3fe531SPhilippe Charnier 		warn("%s", acctfile);
307dea673e9SRodney W. Grimes 	else
308dea673e9SRodney W. Grimes 		fclose(acctf);
309dea673e9SRodney W. Grimes }
310dea673e9SRodney W. Grimes 
311dea673e9SRodney W. Grimes /*
312dea673e9SRodney W. Grimes  * Hashing routines.
313dea673e9SRodney W. Grimes  */
314dea673e9SRodney W. Grimes 
315dea673e9SRodney W. Grimes /*
316dea673e9SRodney W. Grimes  * Enter the name into the hash table and return the pointer allocated.
317dea673e9SRodney W. Grimes  */
318dea673e9SRodney W. Grimes 
319dea673e9SRodney W. Grimes static struct hent *
enter(const char name[])320ba7a1ad7SGarance A Drosehn enter(const char name[])
321dea673e9SRodney W. Grimes {
322dea673e9SRodney W. Grimes 	register struct hent *hp;
323dea673e9SRodney W. Grimes 	register int h;
324dea673e9SRodney W. Grimes 
325dea673e9SRodney W. Grimes 	if ((hp = lookup(name)) != NULL)
326dea673e9SRodney W. Grimes 		return(hp);
327dea673e9SRodney W. Grimes 	h = hash(name);
328dea673e9SRodney W. Grimes 	hcount++;
329961d3645SGarance A Drosehn 	hp = (struct hent *) calloc(sizeof *hp, (size_t)1);
330dea673e9SRodney W. Grimes 	hp->h_name = (char *) calloc(sizeof(char), strlen(name)+1);
331dea673e9SRodney W. Grimes 	strcpy(hp->h_name, name);
332dea673e9SRodney W. Grimes 	hp->h_feetpages = 0.0;
333dea673e9SRodney W. Grimes 	hp->h_count = 0;
334dea673e9SRodney W. Grimes 	hp->h_link = hashtab[h];
335dea673e9SRodney W. Grimes 	hashtab[h] = hp;
336dea673e9SRodney W. Grimes 	return(hp);
337dea673e9SRodney W. Grimes }
338dea673e9SRodney W. Grimes 
339dea673e9SRodney W. Grimes /*
340dea673e9SRodney W. Grimes  * Lookup a name in the hash table and return a pointer
341dea673e9SRodney W. Grimes  * to it.
342dea673e9SRodney W. Grimes  */
343dea673e9SRodney W. Grimes 
344dea673e9SRodney W. Grimes static struct hent *
lookup(const char name[])345ba7a1ad7SGarance A Drosehn lookup(const char name[])
346dea673e9SRodney W. Grimes {
347dea673e9SRodney W. Grimes 	register int h;
348dea673e9SRodney W. Grimes 	register struct hent *hp;
349dea673e9SRodney W. Grimes 
350dea673e9SRodney W. Grimes 	h = hash(name);
351dea673e9SRodney W. Grimes 	for (hp = hashtab[h]; hp != NULL; hp = hp->h_link)
352dea673e9SRodney W. Grimes 		if (strcmp(hp->h_name, name) == 0)
353dea673e9SRodney W. Grimes 			return(hp);
354dea673e9SRodney W. Grimes 	return(NULL);
355dea673e9SRodney W. Grimes }
356dea673e9SRodney W. Grimes 
357dea673e9SRodney W. Grimes /*
358dea673e9SRodney W. Grimes  * Hash the passed name and return the index in
359dea673e9SRodney W. Grimes  * the hash table to begin the search.
360dea673e9SRodney W. Grimes  */
361dea673e9SRodney W. Grimes static int
hash(const char name[])362ba7a1ad7SGarance A Drosehn hash(const char name[])
363dea673e9SRodney W. Grimes {
364dea673e9SRodney W. Grimes 	register int h;
365ba7a1ad7SGarance A Drosehn 	register const char *cp;
366dea673e9SRodney W. Grimes 
367dea673e9SRodney W. Grimes 	for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
368dea673e9SRodney W. Grimes 		;
369dea673e9SRodney W. Grimes 	return((h & 0x7fffffff) % HSHSIZE);
370dea673e9SRodney W. Grimes }
371dea673e9SRodney W. Grimes 
372dea673e9SRodney W. Grimes /*
373dea673e9SRodney W. Grimes  * Other stuff
374dea673e9SRodney W. Grimes  */
375dea673e9SRodney W. Grimes static int
any(int ch,const char str[])376ba7a1ad7SGarance A Drosehn any(int ch, const char str[])
377dea673e9SRodney W. Grimes {
378dea673e9SRodney W. Grimes 	register int c = ch;
379ba7a1ad7SGarance A Drosehn 	register const char *cp = str;
380dea673e9SRodney W. Grimes 
381dea673e9SRodney W. Grimes 	while (*cp)
382dea673e9SRodney W. Grimes 		if (*cp++ == c)
383dea673e9SRodney W. Grimes 			return(1);
384dea673e9SRodney W. Grimes 	return(0);
385dea673e9SRodney W. Grimes }
386dea673e9SRodney W. Grimes 
387dea673e9SRodney W. Grimes /*
388dea673e9SRodney W. Grimes  * The qsort comparison routine.
389dea673e9SRodney W. Grimes  * The comparison is ascii collating order
390dea673e9SRodney W. Grimes  * or by feet of typesetter film, according to sort.
391dea673e9SRodney W. Grimes  */
392dea673e9SRodney W. Grimes static int
qucmp(const void * a,const void * b)393ba7a1ad7SGarance A Drosehn qucmp(const void *a, const void *b)
394dea673e9SRodney W. Grimes {
395ba7a1ad7SGarance A Drosehn 	register const struct hent *h1, *h2;
396dea673e9SRodney W. Grimes 	register int r;
397dea673e9SRodney W. Grimes 
398f0baf665SGarance A Drosehn 	h1 = *(const struct hent * const *)a;
399f0baf665SGarance A Drosehn 	h2 = *(const struct hent * const *)b;
400dea673e9SRodney W. Grimes 	if (sort)
401dea673e9SRodney W. Grimes 		r = h1->h_feetpages < h2->h_feetpages ?
402dea673e9SRodney W. Grimes 		    -1 : h1->h_feetpages > h2->h_feetpages;
403dea673e9SRodney W. Grimes 	else
404dea673e9SRodney W. Grimes 		r = strcmp(h1->h_name, h2->h_name);
405dea673e9SRodney W. Grimes 	return(reverse ? -r : r);
406dea673e9SRodney W. Grimes }
407dea673e9SRodney W. Grimes 
408dea673e9SRodney W. Grimes /*
409dea673e9SRodney W. Grimes  * Perform lookup for printer name or abbreviation --
410dea673e9SRodney W. Grimes  */
411dea673e9SRodney W. Grimes static int
chkprinter(const char * ptrname)412ba7a1ad7SGarance A Drosehn chkprinter(const char *ptrname)
413dea673e9SRodney W. Grimes {
414dea673e9SRodney W. Grimes 	int stat;
4154a1a0dbeSGarrett Wollman 	struct printer myprinter, *pp = &myprinter;
416dea673e9SRodney W. Grimes 
4174a1a0dbeSGarrett Wollman 	init_printer(&myprinter);
418ba7a1ad7SGarance A Drosehn 	stat = getprintcap(ptrname, pp);
4194a1a0dbeSGarrett Wollman 	switch(stat) {
4204a1a0dbeSGarrett Wollman 	case PCAPERR_OSERR:
4214a1a0dbeSGarrett Wollman 		printf("pac: getprintcap: %s\n", pcaperr(stat));
422dea673e9SRodney W. Grimes 		exit(3);
4234a1a0dbeSGarrett Wollman 	case PCAPERR_NOTFOUND:
4244a1a0dbeSGarrett Wollman 		return 0;
4254a1a0dbeSGarrett Wollman 	case PCAPERR_TCLOOP:
4264a1a0dbeSGarrett Wollman 		fatal(pp, "%s", pcaperr(stat));
427dea673e9SRodney W. Grimes 	}
42867ba6825SDavid Malone 	if ((acctfile = pp->acct_file) == NULL)
429ba7a1ad7SGarance A Drosehn 		errx(3, "accounting not enabled for printer %s", ptrname);
4304a1a0dbeSGarrett Wollman 	if (!pflag && pp->price100)
4314a1a0dbeSGarrett Wollman 		price = pp->price100/10000.0;
432dea673e9SRodney W. Grimes 	sumfile = (char *) calloc(sizeof(char), strlen(acctfile)+5);
4339b3fe531SPhilippe Charnier 	if (sumfile == NULL)
4349b3fe531SPhilippe Charnier 		errx(1, "calloc failed");
435dea673e9SRodney W. Grimes 	strcpy(sumfile, acctfile);
436dea673e9SRodney W. Grimes 	strcat(sumfile, "_sum");
437dea673e9SRodney W. Grimes 	return(1);
438dea673e9SRodney W. Grimes }
439