2017-11-20 19:49:47 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1994-05-26 05:23:31 +00:00
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* 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.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-26 05:23:31 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-09-24 06:48:24 +00:00
|
|
|
static const char copyright[] =
|
1994-05-26 05:23:31 +00:00
|
|
|
"@(#) Copyright (c) 1983, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
1997-09-24 06:48:24 +00:00
|
|
|
#if 0
|
2003-07-15 08:48:30 +00:00
|
|
|
#ifndef lint
|
1994-05-26 05:23:31 +00:00
|
|
|
static char sccsid[] = "@(#)pac.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#endif /* not lint */
|
2003-07-15 08:48:30 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 05:23:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Do Printer accounting summary.
|
|
|
|
* Currently, usage is
|
|
|
|
* pac [-Pprinter] [-pprice] [-s] [-r] [-c] [-m] [user ...]
|
|
|
|
* to print the usage information for the named people.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#include <dirent.h>
|
1998-09-11 18:49:33 +00:00
|
|
|
#include <err.h>
|
1994-05-26 05:23:31 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
1998-09-11 18:49:33 +00:00
|
|
|
#include <unistd.h>
|
1994-05-26 05:23:31 +00:00
|
|
|
#include "lp.h"
|
|
|
|
#include "lp.local.h"
|
|
|
|
|
|
|
|
static char *acctfile; /* accounting file (input data) */
|
|
|
|
static int allflag = 1; /* Get stats on everybody */
|
|
|
|
static int errs;
|
2001-07-15 01:28:56 +00:00
|
|
|
static size_t hcount; /* Count of hash entries */
|
1994-05-26 05:23:31 +00:00
|
|
|
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 int reverse; /* Reverse sort order */
|
|
|
|
static int sort; /* Sort by cost */
|
|
|
|
static char *sumfile; /* summary file */
|
|
|
|
static int summarize; /* Compress accounting file */
|
|
|
|
|
1997-07-23 00:49:46 +00:00
|
|
|
uid_t uid, euid;
|
|
|
|
|
1994-05-26 05:23:31 +00: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 */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct hent *hashtab[HSHSIZE]; /* Hash table proper */
|
|
|
|
|
2001-06-12 16:38:20 +00:00
|
|
|
int main(int argc, char **_argv);
|
2001-07-15 01:28:56 +00:00
|
|
|
static void account(FILE *_acctf);
|
2001-06-12 16:38:20 +00:00
|
|
|
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);
|
1994-05-26 05:23:31 +00:00
|
|
|
|
1996-10-27 03:06:52 +00:00
|
|
|
int
|
2001-06-12 16:38:20 +00:00
|
|
|
main(int argc, char **argv)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
2001-07-15 01:28:56 +00:00
|
|
|
FILE *acctf;
|
2001-06-12 16:38:20 +00:00
|
|
|
const char *cp, *printer;
|
1994-05-26 05:23:31 +00:00
|
|
|
|
1997-12-02 20:46:22 +00:00
|
|
|
printer = NULL;
|
1997-07-23 00:49:46 +00:00
|
|
|
euid = geteuid(); /* these aren't used in pac(1) */
|
|
|
|
uid = getuid();
|
1994-05-26 05:23:31 +00:00
|
|
|
while (--argc) {
|
|
|
|
cp = *++argv;
|
|
|
|
if (*cp++ == '-') {
|
|
|
|
switch(*cp++) {
|
|
|
|
case 'P':
|
|
|
|
/*
|
|
|
|
* Printer name.
|
|
|
|
*/
|
|
|
|
printer = cp;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
/*
|
|
|
|
* get the price.
|
|
|
|
*/
|
|
|
|
price = atof(cp);
|
|
|
|
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:
|
1997-09-24 06:48:24 +00:00
|
|
|
usage();
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
(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);
|
|
|
|
}
|
|
|
|
|
2001-07-15 01:28:56 +00:00
|
|
|
if ((acctf = fopen(acctfile, "r")) == NULL) {
|
1994-05-26 05:23:31 +00:00
|
|
|
perror(acctfile);
|
|
|
|
exit(1);
|
|
|
|
}
|
2001-07-15 01:28:56 +00:00
|
|
|
account(acctf);
|
|
|
|
fclose(acctf);
|
|
|
|
if ((acctf = fopen(sumfile, "r")) != NULL) {
|
|
|
|
account(acctf);
|
|
|
|
fclose(acctf);
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
if (summarize)
|
|
|
|
rewrite();
|
|
|
|
else
|
|
|
|
dumpit();
|
|
|
|
exit(errs);
|
|
|
|
}
|
|
|
|
|
1997-09-24 06:48:24 +00:00
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
usage(void)
|
1997-09-24 06:48:24 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"usage: pac [-Pprinter] [-pprice] [-s] [-c] [-r] [-m] [user ...]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1994-05-26 05:23:31 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
2011-12-30 10:58:14 +00:00
|
|
|
* Note that we must accommodate both the active and summary file
|
1994-05-26 05:23:31 +00:00
|
|
|
* formats here.
|
|
|
|
* Host names are ignored if the -m flag is present.
|
|
|
|
*/
|
|
|
|
static void
|
2001-07-15 01:28:56 +00:00
|
|
|
account(FILE *acctf)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
char linebuf[BUFSIZ];
|
|
|
|
double t;
|
|
|
|
register char *cp, *cp2;
|
|
|
|
register struct hent *hp;
|
|
|
|
register int ic;
|
|
|
|
|
2001-07-15 01:28:56 +00:00
|
|
|
while (fgets(linebuf, BUFSIZ, acctf) != NULL) {
|
1994-05-26 05:23:31 +00:00
|
|
|
cp = linebuf;
|
1995-07-17 13:14:01 +00:00
|
|
|
while (any(*cp, " \t"))
|
1994-05-26 05:23:31 +00:00
|
|
|
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';
|
1997-07-23 20:53:41 +00:00
|
|
|
if (mflag && strchr(cp, ':'))
|
|
|
|
cp = strchr(cp, ':') + 1;
|
1994-05-26 05:23:31 +00:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sort the hashed entries by name or footage
|
|
|
|
* and print it all out.
|
|
|
|
*/
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
dumpit(void)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
struct hent **base;
|
|
|
|
register struct hent *hp, **ap;
|
2001-07-15 01:28:56 +00:00
|
|
|
register int hno, runs;
|
|
|
|
size_t c;
|
1994-05-26 05:23:31 +00:00
|
|
|
float feet;
|
|
|
|
|
|
|
|
hp = hashtab[0];
|
|
|
|
hno = 1;
|
|
|
|
base = (struct hent **) calloc(sizeof hp, hcount);
|
|
|
|
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");
|
1995-05-30 03:57:47 +00:00
|
|
|
printf("%-24s %7.2f %4d $%6.2f\n", "total", feet,
|
1994-05-26 05:23:31 +00:00
|
|
|
runs, feet * price);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rewrite the summary file with the summary information we have accumulated.
|
|
|
|
*/
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
rewrite(void)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
register struct hent *hp;
|
|
|
|
register int i;
|
2001-07-15 01:28:56 +00:00
|
|
|
FILE *acctf;
|
1994-05-26 05:23:31 +00:00
|
|
|
|
|
|
|
if ((acctf = fopen(sumfile, "w")) == NULL) {
|
1997-09-24 06:48:24 +00:00
|
|
|
warn("%s", sumfile);
|
1994-05-26 05:23:31 +00: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)) {
|
1997-09-24 06:48:24 +00:00
|
|
|
warn("%s", sumfile);
|
1994-05-26 05:23:31 +00:00
|
|
|
errs++;
|
|
|
|
}
|
|
|
|
fclose(acctf);
|
|
|
|
if ((acctf = fopen(acctfile, "w")) == NULL)
|
1997-09-24 06:48:24 +00:00
|
|
|
warn("%s", acctfile);
|
1994-05-26 05:23:31 +00:00
|
|
|
else
|
|
|
|
fclose(acctf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hashing routines.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enter the name into the hash table and return the pointer allocated.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct hent *
|
2001-06-12 16:38:20 +00:00
|
|
|
enter(const char name[])
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
register struct hent *hp;
|
|
|
|
register int h;
|
|
|
|
|
|
|
|
if ((hp = lookup(name)) != NULL)
|
|
|
|
return(hp);
|
|
|
|
h = hash(name);
|
|
|
|
hcount++;
|
2001-07-15 01:28:56 +00:00
|
|
|
hp = (struct hent *) calloc(sizeof *hp, (size_t)1);
|
1994-05-26 05:23:31 +00:00
|
|
|
hp->h_name = (char *) calloc(sizeof(char), strlen(name)+1);
|
|
|
|
strcpy(hp->h_name, name);
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct hent *
|
2001-06-12 16:38:20 +00:00
|
|
|
lookup(const char name[])
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hash the passed name and return the index in
|
|
|
|
* the hash table to begin the search.
|
|
|
|
*/
|
|
|
|
static int
|
2001-06-12 16:38:20 +00:00
|
|
|
hash(const char name[])
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
register int h;
|
2001-06-12 16:38:20 +00:00
|
|
|
register const char *cp;
|
1994-05-26 05:23:31 +00:00
|
|
|
|
|
|
|
for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
|
|
|
|
;
|
|
|
|
return((h & 0x7fffffff) % HSHSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Other stuff
|
|
|
|
*/
|
|
|
|
static int
|
2001-06-12 16:38:20 +00:00
|
|
|
any(int ch, const char str[])
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
register int c = ch;
|
2001-06-12 16:38:20 +00:00
|
|
|
register const char *cp = str;
|
1994-05-26 05:23:31 +00:00
|
|
|
|
|
|
|
while (*cp)
|
|
|
|
if (*cp++ == c)
|
|
|
|
return(1);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The qsort comparison routine.
|
|
|
|
* The comparison is ascii collating order
|
|
|
|
* or by feet of typesetter film, according to sort.
|
|
|
|
*/
|
|
|
|
static int
|
2001-06-12 16:38:20 +00:00
|
|
|
qucmp(const void *a, const void *b)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
2001-06-12 16:38:20 +00:00
|
|
|
register const struct hent *h1, *h2;
|
1994-05-26 05:23:31 +00:00
|
|
|
register int r;
|
|
|
|
|
2002-04-22 23:28:42 +00:00
|
|
|
h1 = *(const struct hent * const *)a;
|
|
|
|
h2 = *(const struct hent * const *)b;
|
1994-05-26 05:23:31 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform lookup for printer name or abbreviation --
|
|
|
|
*/
|
|
|
|
static int
|
2001-06-12 16:38:20 +00:00
|
|
|
chkprinter(const char *ptrname)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
int stat;
|
1997-12-02 20:46:22 +00:00
|
|
|
struct printer myprinter, *pp = &myprinter;
|
1994-05-26 05:23:31 +00:00
|
|
|
|
1997-12-02 20:46:22 +00:00
|
|
|
init_printer(&myprinter);
|
2001-06-12 16:38:20 +00:00
|
|
|
stat = getprintcap(ptrname, pp);
|
1997-12-02 20:46:22 +00:00
|
|
|
switch(stat) {
|
|
|
|
case PCAPERR_OSERR:
|
|
|
|
printf("pac: getprintcap: %s\n", pcaperr(stat));
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(3);
|
1997-12-02 20:46:22 +00:00
|
|
|
case PCAPERR_NOTFOUND:
|
|
|
|
return 0;
|
|
|
|
case PCAPERR_TCLOOP:
|
|
|
|
fatal(pp, "%s", pcaperr(stat));
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
2001-02-25 13:50:29 +00:00
|
|
|
if ((acctfile = pp->acct_file) == NULL)
|
2001-06-12 16:38:20 +00:00
|
|
|
errx(3, "accounting not enabled for printer %s", ptrname);
|
1997-12-02 20:46:22 +00:00
|
|
|
if (!pflag && pp->price100)
|
|
|
|
price = pp->price100/10000.0;
|
1994-05-26 05:23:31 +00:00
|
|
|
sumfile = (char *) calloc(sizeof(char), strlen(acctfile)+5);
|
1997-09-24 06:48:24 +00:00
|
|
|
if (sumfile == NULL)
|
|
|
|
errx(1, "calloc failed");
|
1994-05-26 05:23:31 +00:00
|
|
|
strcpy(sumfile, acctfile);
|
|
|
|
strcat(sumfile, "_sum");
|
|
|
|
return(1);
|
|
|
|
}
|