Deleted Added
full compact
pac.c (73028) pac.c (78146)
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:

--- 29 unchanged lines hidden (view full) ---

38 The Regents of the University of California. All rights reserved.\n";
39#endif /* not lint */
40
41#ifndef lint
42#if 0
43static char sccsid[] = "@(#)pac.c 8.1 (Berkeley) 6/6/93";
44#endif
45static const char rcsid[] =
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:

--- 29 unchanged lines hidden (view full) ---

38 The Regents of the University of California. All rights reserved.\n";
39#endif /* not lint */
40
41#ifndef lint
42#if 0
43static char sccsid[] = "@(#)pac.c 8.1 (Berkeley) 6/6/93";
44#endif
45static const char rcsid[] =
46 "$FreeBSD: head/usr.sbin/lpr/pac/pac.c 73028 2001-02-25 13:50:29Z dwmalone $";
46 "$FreeBSD: head/usr.sbin/lpr/pac/pac.c 78146 2001-06-12 16:38:20Z gad $";
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 */

--- 36 unchanged lines hidden (view full) ---

91 struct hent *h_link; /* Forward hash link */
92 char *h_name; /* Name of this user */
93 float h_feetpages; /* Feet or pages of paper */
94 int h_count; /* Number of runs */
95};
96
97static struct hent *hashtab[HSHSIZE]; /* Hash table proper */
98
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 */

--- 36 unchanged lines hidden (view full) ---

91 struct hent *h_link; /* Forward hash link */
92 char *h_name; /* Name of this user */
93 float h_feetpages; /* Feet or pages of paper */
94 int h_count; /* Number of runs */
95};
96
97static struct hent *hashtab[HSHSIZE]; /* Hash table proper */
98
99int main __P((int, char **));
100static void account __P((FILE *));
101static int any __P((int, char []));
102static int chkprinter __P((char *));
103static void dumpit __P((void));
104static int hash __P((char []));
105static struct hent *enter __P((char []));
106static struct hent *lookup __P((char []));
107static int qucmp __P((const void *, const void *));
108static void rewrite __P((void));
109static void usage __P((void));
99int main(int argc, char **_argv);
100static void account(FILE *_acct);
101static int any(int _ch, const char _str[]);
102static int chkprinter(const char *_ptrname);
103static void dumpit(void);
104static int hash(const char _name[]);
105static struct hent *enter(const char _name[]);
106static struct hent *lookup(const char _name[]);
107static int qucmp(const void *_a, const void *_b);
108static void rewrite(void);
109static void usage(void);
110
111int
110
111int
112main(argc, argv)
113 int argc;
114 char **argv;
112main(int argc, char **argv)
115{
116 FILE *acct;
113{
114 FILE *acct;
117 char *cp, *printer;
115 const char *cp, *printer;
118
119 printer = NULL;
120 euid = geteuid(); /* these aren't used in pac(1) */
121 uid = getuid();
122 while (--argc) {
123 cp = *++argv;
124 if (*cp++ == '-') {
125 switch(*cp++) {

--- 67 unchanged lines hidden (view full) ---

193 if (summarize)
194 rewrite();
195 else
196 dumpit();
197 exit(errs);
198}
199
200static void
116
117 printer = NULL;
118 euid = geteuid(); /* these aren't used in pac(1) */
119 uid = getuid();
120 while (--argc) {
121 cp = *++argv;
122 if (*cp++ == '-') {
123 switch(*cp++) {

--- 67 unchanged lines hidden (view full) ---

191 if (summarize)
192 rewrite();
193 else
194 dumpit();
195 exit(errs);
196}
197
198static void
201usage()
199usage(void)
202{
203 fprintf(stderr,
204 "usage: pac [-Pprinter] [-pprice] [-s] [-c] [-r] [-m] [user ...]\n");
205 exit(1);
206}
207
208/*
209 * Read the entire accounting file, accumulating statistics
210 * for the users that we have in the hash table. If allflag
211 * is set, then just gather the facts on everyone.
212 * Note that we must accomodate both the active and summary file
213 * formats here.
214 * Host names are ignored if the -m flag is present.
215 */
216static void
200{
201 fprintf(stderr,
202 "usage: pac [-Pprinter] [-pprice] [-s] [-c] [-r] [-m] [user ...]\n");
203 exit(1);
204}
205
206/*
207 * Read the entire accounting file, accumulating statistics
208 * for the users that we have in the hash table. If allflag
209 * is set, then just gather the facts on everyone.
210 * Note that we must accomodate both the active and summary file
211 * formats here.
212 * Host names are ignored if the -m flag is present.
213 */
214static void
217account(acct)
218 register FILE *acct;
215account(FILE *acct)
219{
220 char linebuf[BUFSIZ];
221 double t;
222 register char *cp, *cp2;
223 register struct hent *hp;
224 register int ic;
225
226 while (fgets(linebuf, BUFSIZ, acct) != NULL) {

--- 25 unchanged lines hidden (view full) ---

252 }
253}
254
255/*
256 * Sort the hashed entries by name or footage
257 * and print it all out.
258 */
259static void
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) {

--- 25 unchanged lines hidden (view full) ---

249 }
250}
251
252/*
253 * Sort the hashed entries by name or footage
254 * and print it all out.
255 */
256static void
260dumpit()
257dumpit(void)
261{
262 struct hent **base;
263 register struct hent *hp, **ap;
264 register int hno, c, runs;
265 float feet;
266
267 hp = hashtab[0];
268 hno = 1;

--- 21 unchanged lines hidden (view full) ---

290 runs, feet * price);
291 }
292}
293
294/*
295 * Rewrite the summary file with the summary information we have accumulated.
296 */
297static void
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;

--- 21 unchanged lines hidden (view full) ---

287 runs, feet * price);
288 }
289}
290
291/*
292 * Rewrite the summary file with the summary information we have accumulated.
293 */
294static void
298rewrite()
295rewrite(void)
299{
300 register struct hent *hp;
301 register int i;
302 register FILE *acctf;
303
304 if ((acctf = fopen(sumfile, "w")) == NULL) {
305 warn("%s", sumfile);
306 errs++;

--- 23 unchanged lines hidden (view full) ---

330 * Hashing routines.
331 */
332
333/*
334 * Enter the name into the hash table and return the pointer allocated.
335 */
336
337static struct hent *
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++;

--- 23 unchanged lines hidden (view full) ---

327 * Hashing routines.
328 */
329
330/*
331 * Enter the name into the hash table and return the pointer allocated.
332 */
333
334static struct hent *
338enter(name)
339 char name[];
335enter(const char name[])
340{
341 register struct hent *hp;
342 register int h;
343
344 if ((hp = lookup(name)) != NULL)
345 return(hp);
346 h = hash(name);
347 hcount++;

--- 8 unchanged lines hidden (view full) ---

356}
357
358/*
359 * Lookup a name in the hash table and return a pointer
360 * to it.
361 */
362
363static struct hent *
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++;

--- 8 unchanged lines hidden (view full) ---

352}
353
354/*
355 * Lookup a name in the hash table and return a pointer
356 * to it.
357 */
358
359static struct hent *
364lookup(name)
365 char name[];
360lookup(const char name[])
366{
367 register int h;
368 register struct hent *hp;
369
370 h = hash(name);
371 for (hp = hashtab[h]; hp != NULL; hp = hp->h_link)
372 if (strcmp(hp->h_name, name) == 0)
373 return(hp);
374 return(NULL);
375}
376
377/*
378 * Hash the passed name and return the index in
379 * the hash table to begin the search.
380 */
381static int
361{
362 register int h;
363 register struct hent *hp;
364
365 h = hash(name);
366 for (hp = hashtab[h]; hp != NULL; hp = hp->h_link)
367 if (strcmp(hp->h_name, name) == 0)
368 return(hp);
369 return(NULL);
370}
371
372/*
373 * Hash the passed name and return the index in
374 * the hash table to begin the search.
375 */
376static int
382hash(name)
383 char name[];
377hash(const char name[])
384{
385 register int h;
378{
379 register int h;
386 register char *cp;
380 register const char *cp;
387
388 for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
389 ;
390 return((h & 0x7fffffff) % HSHSIZE);
391}
392
393/*
394 * Other stuff
395 */
396static int
381
382 for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
383 ;
384 return((h & 0x7fffffff) % HSHSIZE);
385}
386
387/*
388 * Other stuff
389 */
390static int
397any(ch, str)
398 int ch;
399 char str[];
391any(int ch, const char str[])
400{
401 register int c = ch;
392{
393 register int c = ch;
402 register char *cp = str;
394 register const char *cp = str;
403
404 while (*cp)
405 if (*cp++ == c)
406 return(1);
407 return(0);
408}
409
410/*
411 * The qsort comparison routine.
412 * The comparison is ascii collating order
413 * or by feet of typesetter film, according to sort.
414 */
415static int
395
396 while (*cp)
397 if (*cp++ == c)
398 return(1);
399 return(0);
400}
401
402/*
403 * The qsort comparison routine.
404 * The comparison is ascii collating order
405 * or by feet of typesetter film, according to sort.
406 */
407static int
416qucmp(a, b)
417 const void *a, *b;
408qucmp(const void *a, const void *b)
418{
409{
419 register struct hent *h1, *h2;
410 register const struct hent *h1, *h2;
420 register int r;
421
411 register int r;
412
422 h1 = *(struct hent **)a;
423 h2 = *(struct hent **)b;
413 h1 = *(const struct hent **)a;
414 h2 = *(const struct hent **)b;
424 if (sort)
425 r = h1->h_feetpages < h2->h_feetpages ?
426 -1 : h1->h_feetpages > h2->h_feetpages;
427 else
428 r = strcmp(h1->h_name, h2->h_name);
429 return(reverse ? -r : r);
430}
431
432/*
433 * Perform lookup for printer name or abbreviation --
434 */
435static int
415 if (sort)
416 r = h1->h_feetpages < h2->h_feetpages ?
417 -1 : h1->h_feetpages > h2->h_feetpages;
418 else
419 r = strcmp(h1->h_name, h2->h_name);
420 return(reverse ? -r : r);
421}
422
423/*
424 * Perform lookup for printer name or abbreviation --
425 */
426static int
436chkprinter(s)
437 register char *s;
427chkprinter(const char *ptrname)
438{
439 int stat;
440 struct printer myprinter, *pp = &myprinter;
441
442 init_printer(&myprinter);
428{
429 int stat;
430 struct printer myprinter, *pp = &myprinter;
431
432 init_printer(&myprinter);
443 stat = getprintcap(s, pp);
433 stat = getprintcap(ptrname, pp);
444 switch(stat) {
445 case PCAPERR_OSERR:
446 printf("pac: getprintcap: %s\n", pcaperr(stat));
447 exit(3);
448 case PCAPERR_NOTFOUND:
449 return 0;
450 case PCAPERR_TCLOOP:
451 fatal(pp, "%s", pcaperr(stat));
452 }
453 if ((acctfile = pp->acct_file) == NULL)
434 switch(stat) {
435 case PCAPERR_OSERR:
436 printf("pac: getprintcap: %s\n", pcaperr(stat));
437 exit(3);
438 case PCAPERR_NOTFOUND:
439 return 0;
440 case PCAPERR_TCLOOP:
441 fatal(pp, "%s", pcaperr(stat));
442 }
443 if ((acctfile = pp->acct_file) == NULL)
454 errx(3, "accounting not enabled for printer %s", s);
444 errx(3, "accounting not enabled for printer %s", ptrname);
455 if (!pflag && pp->price100)
456 price = pp->price100/10000.0;
457 sumfile = (char *) calloc(sizeof(char), strlen(acctfile)+5);
458 if (sumfile == NULL)
459 errx(1, "calloc failed");
460 strcpy(sumfile, acctfile);
461 strcat(sumfile, "_sum");
462 return(1);
463}
445 if (!pflag && pp->price100)
446 price = pp->price100/10000.0;
447 sumfile = (char *) calloc(sizeof(char), strlen(acctfile)+5);
448 if (sumfile == NULL)
449 errx(1, "calloc failed");
450 strcpy(sumfile, acctfile);
451 strcat(sumfile, "_sum");
452 return(1);
453}