2 * $Id: psa.c,v 1.5 2009-10-13 22:55:37 didg Exp $
4 * Copyright (c) 1990,1995 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
7 * PostScript Accounting, psa.
9 * psa is invoked by psf, as output for a communication program. The
10 * communication program is expected to send a small program before and
11 * after each job, which causes the page count to be emitted in a well
12 * known format. psa reads its input, looking for page counts and other
13 * interesting data. Any data that it doesn't understand, it emits to
14 * stderr, the lpd log file. Data that it does understand may be written
15 * to a status file or logged. Once all input has been received, psa
16 * subtracts the beginning and end page counts, and log an accounting
17 * record in the accounting file.
22 #endif /* HAVE_CONFIG_H */
26 #endif /* HAVE_UNISTD_H */
31 int main( int ac, char **av)
34 char *acc, *user, *host;
35 char buf[ 1024 ], *p, *end;
36 int cc, n, ipc = -1, fpc = -1;
39 fprintf( stderr, "Usage:\t%s accounting-file user host\n", av[ 0 ] );
48 * Explain n = !n ... Is there no beauty in truth?
50 while (( cc = read( 0, buf, sizeof( buf ))) > 0 ) {
51 if ( ipc < 0 && *buf == '*' ) {
52 /* find initial pagecount */
53 for ( p = buf, end = buf + cc; p < end; p++ ) {
54 if ( *p == '\n' || *p == '\r' ) {
59 fprintf( stderr, "Can't find initial page count!\n" );
64 ipc = atoi( buf + 1 );
70 /* find final pagecount */
71 for ( p = buf + cc - 1; p >= buf; p-- ) {
72 if ( *p != '\n' && *p != '\r' ) {
77 fprintf( stderr, "Can't find final page count!\n" );
81 for ( ; p >= buf; p-- ) {
82 if ( *p == '\n' || *p == '\r' ) {
96 #define max(x,y) ((x)>(y)?(x):(y))
100 if ( cc != 0 && write( 2, buf, cc ) != cc ) {
101 fprintf( stderr, "write 1: 2 %p %d\n", buf, cc );
112 fprintf( stderr, "Didn't find initial page count!\n" );
117 fprintf( stderr, "Didn't find final page count!\n" );
122 * Write accounting record.
124 if (( af = fopen( acc, "a" )) != NULL ) {
125 fprintf( af, "%7.2f\t%s:%s\n", (float)( fpc - ipc ), host, user );