#include "lp.cdefs.h"
#include <sys/param.h>
#include <dirent.h>
#include <err.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "lp.h"
#include "lp.local.h"
static char *acctfile;
static int allflag = 1;
static int errs;
static size_t hcount;
static int mflag = 0;
static int pflag = 0;
static float price = 0.02;
static int reverse;
static int sort;
static char *sumfile;
static int summarize;
uid_t uid, euid;
#define HSHSIZE 97
struct hent {
struct hent *h_link;
char *h_name;
float h_feetpages;
int h_count;
};
static struct hent *hashtab[HSHSIZE];
int main(int argc, char **_argv);
static void account(FILE *_acctf);
static int any(int _ch, const char _str[]);
static int chkprinter(const char *_ptrname);
static void dumpit(void);
static int hash(const char _name[]);
static struct hent *enter(const char _name[]);
static struct hent *lookup(const char _name[]);
static int qucmp(const void *_a, const void *_b);
static void rewrite(void);
static void usage(void);
int
main(int argc, char **argv)
{
FILE *acctf;
const char *cp, *printer;
printer = NULL;
euid = geteuid();
uid = getuid();
while (--argc) {
cp = *++argv;
if (*cp++ == '-') {
switch(*cp++) {
case 'P':
printer = cp;
continue;
case 'p':
price = atof(cp);
pflag = 1;
continue;
case 's':
summarize++;
continue;
case 'c':
sort++;
continue;
case 'm':
mflag = 1;
continue;
case 'r':
reverse++;
continue;
default:
usage();
}
}
(void) enter(--cp);
allflag = 0;
}
if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
printer = DEFLP;
if (!chkprinter(printer)) {
printf("pac: unknown printer %s\n", printer);
exit(2);
}
if ((acctf = fopen(acctfile, "r")) == NULL) {
perror(acctfile);
exit(1);
}
account(acctf);
fclose(acctf);
if ((acctf = fopen(sumfile, "r")) != NULL) {
account(acctf);
fclose(acctf);
}
if (summarize)
rewrite();
else
dumpit();
exit(errs);
}
static void
usage(void)
{
fprintf(stderr,
"usage: pac [-Pprinter] [-pprice] [-s] [-c] [-r] [-m] [user ...]\n");
exit(1);
}
static void
account(FILE *acctf)
{
char linebuf[BUFSIZ];
double t;
register char *cp, *cp2;
register struct hent *hp;
register int ic;
while (fgets(linebuf, BUFSIZ, acctf) != NULL) {
cp = linebuf;
while (any(*cp, " \t"))
cp++;
t = atof(cp);
while (any(*cp, ".0123456789"))
cp++;
while (any(*cp, " \t"))
cp++;
for (cp2 = cp; !any(*cp2, " \t\n"); cp2++)
;
ic = atoi(cp2);
*cp2 = '\0';
if (mflag && strchr(cp, ':'))
cp = strchr(cp, ':') + 1;
hp = lookup(cp);
if (hp == NULL) {
if (!allflag)
continue;
hp = enter(cp);
}
hp->h_feetpages += t;
if (ic)
hp->h_count += ic;
else
hp->h_count++;
}
}
static void
dumpit(void)
{
struct hent **base;
register struct hent *hp, **ap;
register int hno, runs;
size_t c;
float feet;
hp = hashtab[0];
hno = 1;
base = (struct hent **) calloc(hcount, sizeof(hp));
for (ap = base, c = hcount; c--; ap++) {
while (hp == NULL)
hp = hashtab[hno++];
*ap = hp;
hp = hp->h_link;
}
qsort(base, hcount, sizeof hp, qucmp);
printf(" Login pages/feet runs price\n");
feet = 0.0;
runs = 0;
for (ap = base, c = hcount; c--; ap++) {
hp = *ap;
runs += hp->h_count;
feet += hp->h_feetpages;
printf("%-24s %7.2f %4d $%6.2f\n", hp->h_name,
hp->h_feetpages, hp->h_count, hp->h_feetpages * price);
}
if (allflag) {
printf("\n");
printf("%-24s %7.2f %4d $%6.2f\n", "total", feet,
runs, feet * price);
}
}
static void
rewrite(void)
{
register struct hent *hp;
register int i;
FILE *acctf;
if ((acctf = fopen(sumfile, "w")) == NULL) {
warn("%s", sumfile);
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);
errs++;
}
fclose(acctf);
if ((acctf = fopen(acctfile, "w")) == NULL)
warn("%s", acctfile);
else
fclose(acctf);
}
static struct hent *
enter(const char name[])
{
register struct hent *hp;
register int h;
if ((hp = lookup(name)) != NULL)
return(hp);
h = hash(name);
hcount++;
hp = (struct hent *) calloc(1, sizeof(*hp));
hp->h_name = strdup(name);
hp->h_feetpages = 0.0;
hp->h_count = 0;
hp->h_link = hashtab[h];
hashtab[h] = hp;
return(hp);
}
static struct hent *
lookup(const char name[])
{
register int h;
register struct hent *hp;
h = hash(name);
for (hp = hashtab[h]; hp != NULL; hp = hp->h_link)
if (strcmp(hp->h_name, name) == 0)
return(hp);
return(NULL);
}
static int
hash(const char name[])
{
register int h;
register const char *cp;
for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
;
return((h & 0x7fffffff) % HSHSIZE);
}
static int
any(int ch, const char str[])
{
register int c = ch;
register const char *cp = str;
while (*cp)
if (*cp++ == c)
return(1);
return(0);
}
static int
qucmp(const void *a, const void *b)
{
register const struct hent *h1, *h2;
register int r;
h1 = *(const struct hent * const *)a;
h2 = *(const struct hent * const *)b;
if (sort)
r = h1->h_feetpages < h2->h_feetpages ?
-1 : h1->h_feetpages > h2->h_feetpages;
else
r = strcmp(h1->h_name, h2->h_name);
return(reverse ? -r : r);
}
static int
chkprinter(const char *ptrname)
{
int stat;
struct printer myprinter, *pp = &myprinter;
init_printer(&myprinter);
stat = getprintcap(ptrname, pp);
switch(stat) {
case PCAPERR_OSERR:
printf("pac: getprintcap: %s\n", pcaperr(stat));
exit(3);
case PCAPERR_NOTFOUND:
return 0;
case PCAPERR_TCLOOP:
fatal(pp, "%s", pcaperr(stat));
}
if ((acctfile = pp->acct_file) == NULL)
errx(3, "accounting not enabled for printer %s", ptrname);
if (!pflag && pp->price100)
price = pp->price100/10000.0;
asprintf(&sumfile, "%s_sum", acctfile);
if (sumfile == NULL)
errx(1, "asprintf failed");
return(1);
}