NetBSD/usr.sbin/lpr/pac/pac.c

445 lines
9.9 KiB
C
Raw Normal View History

/* $NetBSD: pac.c,v 1.19 2003/08/07 11:25:32 agc Exp $ */
1993-03-21 12:45:37 +03:00
/*
1994-05-18 05:26:07 +04:00
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
*
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
The Regents of the University of California. All rights reserved.\n");
#if 0
1994-05-18 05:26:07 +04:00
static char sccsid[] = "@(#)pac.c 8.1 (Berkeley) 6/6/93";
#else
__RCSID("$NetBSD: pac.c,v 1.19 2003/08/07 11:25:32 agc Exp $");
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
/*
* Do Printer accounting summary.
* Currently, usage is
* pac [-Pprinter] [-pprice] [-s] [-r] [-c] [-m] [user ...]
* to print the usage information for the named people.
*/
1994-05-18 05:26:07 +04:00
#include <sys/param.h>
#include <dirent.h>
1993-03-21 12:45:37 +03:00
#include <stdio.h>
#include <stdlib.h>
1994-05-18 05:26:07 +04:00
#include <string.h>
#include <unistd.h>
#include <err.h>
1994-05-18 05:26:07 +04:00
#include "lp.h"
1993-03-21 12:45:37 +03:00
#include "lp.local.h"
1994-05-18 05:26:07 +04:00
static char *acctfile; /* accounting file (input data) */
static int allflag = 1; /* Get stats on everybody */
static int errs;
static int hcount; /* Count of hash entries */
static int mflag = 0; /* disregard machine names */
static int pflag = 0; /* 1 if -p on cmd line */
static float price = 0.02; /* cost per page (or what ever) */
static long price100; /* per-page cost in 100th of a cent */
static int reverse; /* Reverse sort order */
static int sort; /* Sort by cost */
static char *sumfile; /* summary file */
static int summarize; /* Compress accounting file */
1993-03-21 12:45:37 +03:00
/*
* Grossness follows:
* Names to be accumulated are hashed into the following
* table.
*/
#define HSHSIZE 97 /* Number of hash buckets */
struct hent {
struct hent *h_link; /* Forward hash link */
char *h_name; /* Name of this user */
float h_feetpages; /* Feet or pages of paper */
int h_count; /* Number of runs */
};
1994-05-18 05:26:07 +04:00
static struct hent *hashtab[HSHSIZE]; /* Hash table proper */
1993-03-21 12:45:37 +03:00
static void account(FILE *);
static int chkprinter(const char *);
static void dumpit(void);
static int hash(const char *);
static struct hent *enter(const char *);
static struct hent *lookup(const char *);
static int qucmp(const void *, const void *);
static void rewrite(void);
static void usage(void);
int main(int, char * const []);
1993-03-21 12:45:37 +03:00
1996-03-21 20:56:15 +03:00
int
main(int argc, char *const argv[])
1993-03-21 12:45:37 +03:00
{
FILE *acct;
int opt;
while ((opt = getopt(argc, argv, "P:p:scmr")) != -1) {
switch(opt) {
case 'P':
/*
* Printer name.
*/
printer = optarg;
continue;
case 'p':
/*
* get the price.
*/
price = atof(optarg);
pflag = 1;
continue;
case 's':
/*
* Summarize and compress accounting file.
*/
summarize++;
continue;
case 'c':
/*
* Sort by cost.
*/
sort++;
continue;
case 'm':
/*
* disregard machine names for each user
*/
mflag = 1;
continue;
case 'r':
/*
* Reverse sorting order.
*/
reverse++;
continue;
default:
usage();
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
}
}
argc -= optind;
argv += optind;
/*
* If there are any arguments left, they're names of users
* we want to print info for. In that case, put them in the hash
* table and unset allflag.
*/
for( ; argc > 0; argc--, argv++) {
(void)enter(*argv);
1993-03-21 12:45:37 +03:00
allflag = 0;
}
1993-03-21 12:45:37 +03:00
if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
printer = DEFLP;
if (!chkprinter(printer)) {
printf("pac: unknown printer %s\n", printer);
exit(2);
}
if ((acct = fopen(acctfile, "r")) == NULL)
err(1, "%s", acctfile);
1993-03-21 12:45:37 +03:00
account(acct);
fclose(acct);
if ((acct = fopen(sumfile, "r")) != NULL) {
account(acct);
fclose(acct);
}
if (summarize)
rewrite();
else
dumpit();
exit(errs);
}
/*
* Read the entire accounting file, accumulating statistics
* for the users that we have in the hash table. If allflag
* is set, then just gather the facts on everyone.
* Note that we must accomodate both the active and summary file
* formats here.
* Format of accounting file is
* feet_per_page [runs_count] [hostname:]username
* Some software relies on whitespace between runs_count and hostname:username
* being optional (such as Ghostscript's unix-lpr.sh).
*
1993-03-21 12:45:37 +03:00
* Host names are ignored if the -m flag is present.
*/
1994-05-18 05:26:07 +04:00
static void
account(FILE *acct)
1993-03-21 12:45:37 +03:00
{
char who[BUFSIZ];
1993-03-21 12:45:37 +03:00
char linebuf[BUFSIZ];
float t;
char *cp, *cp2;
struct hent *hp;
int ic;
1993-03-21 12:45:37 +03:00
while (fgets(linebuf, BUFSIZ, acct) != NULL) {
2003-05-17 18:56:40 +04:00
/* XXX sizeof(who) == 1024 */
if (sscanf(linebuf, "%f %d%1023s", &t, &ic, who) == 0) {
sscanf(linebuf, "%f %1023s", &t, who);
ic = 1;
}
/* if -m was specified, don't use the hostname part */
if (mflag && (cp2 = strchr(who, ':')))
cp = cp2 + 1;
else
cp = who;
1993-03-21 12:45:37 +03:00
hp = lookup(cp);
1994-05-18 05:26:07 +04:00
if (hp == NULL) {
1993-03-21 12:45:37 +03:00
if (!allflag)
continue;
hp = enter(cp);
}
hp->h_feetpages += t;
if (ic)
hp->h_count += ic;
else
hp->h_count++;
}
}
/*
* Sort the hashed entries by name or footage
* and print it all out.
*/
1994-05-18 05:26:07 +04:00
static void
dumpit(void)
1993-03-21 12:45:37 +03:00
{
struct hent **base;
struct hent *hp, **ap;
int hno, c, runs;
1993-03-21 12:45:37 +03:00
float feet;
hp = hashtab[0];
hno = 1;
base = (struct hent **) calloc(sizeof hp, hcount);
if (base == NULL)
err(1, "calloc");
1993-03-21 12:45:37 +03:00
for (ap = base, c = hcount; c--; ap++) {
1994-05-18 05:26:07 +04:00
while (hp == NULL)
1993-03-21 12:45:37 +03:00
hp = hashtab[hno++];
*ap = hp;
hp = hp->h_link;
}
qsort(base, hcount, sizeof hp, qucmp);
printf(" pages/feet runs price %s\n",
(mflag ? "login" : "host name and login"));
printf(" ---------- ---- -------- ----------------------\n");
1993-03-21 12:45:37 +03:00
feet = 0.0;
runs = 0;
for (ap = base, c = hcount; c--; ap++) {
hp = *ap;
runs += hp->h_count;
feet += hp->h_feetpages;
printf(" %7.2f %4d $%7.2f %s\n",
hp->h_feetpages, hp->h_count,
hp->h_feetpages * price * hp->h_count,
hp->h_name);
1993-03-21 12:45:37 +03:00
}
if (allflag) {
printf(" ---------- ---- -------- ----------------------\n");
printf("Sum:%7.2f %4d $%7.2f\n", feet, runs,
feet * price * runs);
1993-03-21 12:45:37 +03:00
}
}
/*
* Rewrite the summary file with the summary information we have accumulated.
*/
1994-05-18 05:26:07 +04:00
static void
rewrite(void)
1993-03-21 12:45:37 +03:00
{
struct hent *hp;
int i;
FILE *acctf;
1993-03-21 12:45:37 +03:00
if ((acctf = fopen(sumfile, "w")) == NULL) {
warn("%s", sumfile);
1993-03-21 12:45:37 +03:00
errs++;
return;
}
for (i = 0; i < HSHSIZE; i++) {
hp = hashtab[i];
while (hp != NULL) {
fprintf(acctf, "%7.2f\t%s\t%d\n", hp->h_feetpages,
hp->h_name, hp->h_count);
hp = hp->h_link;
}
}
fflush(acctf);
if (ferror(acctf)) {
warn("%s", sumfile);
1993-03-21 12:45:37 +03:00
errs++;
}
fclose(acctf);
if ((acctf = fopen(acctfile, "w")) == NULL)
warn("%s", acctfile);
1993-03-21 12:45:37 +03:00
else
fclose(acctf);
}
/*
* Hashing routines.
*/
/*
* Enter the name into the hash table and return the pointer allocated.
*/
1994-05-18 05:26:07 +04:00
static struct hent *
enter(const char *name)
1993-03-21 12:45:37 +03:00
{
struct hent *hp;
int h;
1993-03-21 12:45:37 +03:00
1994-05-18 05:26:07 +04:00
if ((hp = lookup(name)) != NULL)
1993-03-21 12:45:37 +03:00
return(hp);
h = hash(name);
hcount++;
hp = (struct hent *) calloc(sizeof *hp, 1);
if (hp == NULL)
err(1, "calloc");
hp->h_name = strdup(name);
if (hp->h_name == NULL)
err(1, "malloc");
1993-03-21 12:45:37 +03:00
hp->h_feetpages = 0.0;
hp->h_count = 0;
hp->h_link = hashtab[h];
hashtab[h] = hp;
return(hp);
}
/*
* Lookup a name in the hash table and return a pointer
* to it.
*/
1994-05-18 05:26:07 +04:00
static struct hent *
lookup(const char *name)
1993-03-21 12:45:37 +03:00
{
int h;
struct hent *hp;
1993-03-21 12:45:37 +03:00
h = hash(name);
1994-05-18 05:26:07 +04:00
for (hp = hashtab[h]; hp != NULL; hp = hp->h_link)
1993-03-21 12:45:37 +03:00
if (strcmp(hp->h_name, name) == 0)
return(hp);
1994-05-18 05:26:07 +04:00
return(NULL);
1993-03-21 12:45:37 +03:00
}
/*
* Hash the passed name and return the index in
* the hash table to begin the search.
*/
1994-05-18 05:26:07 +04:00
static int
hash(const char *name)
1993-03-21 12:45:37 +03:00
{
int h;
const char *cp;
1993-03-21 12:45:37 +03:00
for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
;
return((h & 0x7fffffff) % HSHSIZE);
}
/*
* The qsort comparison routine.
* The comparison is ascii collating order
* or by feet of typesetter film, according to sort.
*/
1994-05-18 05:26:07 +04:00
static int
qucmp(const void *a, const void *b)
1993-03-21 12:45:37 +03:00
{
struct hent *h1, *h2;
int r;
1993-03-21 12:45:37 +03:00
1994-05-18 05:26:07 +04:00
h1 = *(struct hent **)a;
h2 = *(struct hent **)b;
1993-03-21 12:45:37 +03:00
if (sort)
1994-05-18 05:26:07 +04:00
r = h1->h_feetpages < h2->h_feetpages ?
-1 : h1->h_feetpages > h2->h_feetpages;
1993-03-21 12:45:37 +03:00
else
r = strcmp(h1->h_name, h2->h_name);
return(reverse ? -r : r);
}
/*
* Perform lookup for printer name or abbreviation --
*/
1994-05-18 05:26:07 +04:00
static int
chkprinter(const char *s)
1993-03-21 12:45:37 +03:00
{
int stat;
1994-05-18 05:26:07 +04:00
if ((stat = cgetent(&bp, printcapdb, s)) == -2) {
1993-03-21 12:45:37 +03:00
printf("pac: can't open printer description file\n");
exit(3);
1994-05-18 05:26:07 +04:00
} else if (stat == -1)
1993-03-21 12:45:37 +03:00
return(0);
1994-05-18 05:26:07 +04:00
else if (stat == -3)
fatal("potential reference loop detected in printcap file");
if (cgetstr(bp, "af", &acctfile) == -1) {
1993-03-21 12:45:37 +03:00
printf("accounting not enabled for printer %s\n", printer);
exit(2);
}
1994-05-18 05:26:07 +04:00
if (!pflag && (cgetnum(bp, "pc", &price100) == 0))
1993-03-21 12:45:37 +03:00
price = price100/10000.0;
2003-07-13 18:06:06 +04:00
asprintf(&sumfile, "%s_sum", acctfile);
if (sumfile == NULL)
err(1, "pac");
1993-03-21 12:45:37 +03:00
return(1);
}
static void
usage(void)
{
fprintf(stderr,
"usage: pac [-Pprinter] [-pprice] [-s] [-c] [-r] [-m] [user ...]\n");
exit(1);
}