2 * Copyright (c) 1990,1995 Regents of The University of Michigan.
3 * 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.
27 char *acc, *user, *host;
28 char buf[ 1024 ], *p, *end;
29 int cc, n, ipc = -1, fpc = -1;
32 fprintf( stderr, "Usage:\t%s accounting-file user host\n", av[ 0 ] );
41 * Explain n = !n ... Is there no beauty in truth?
43 while (( cc = read( 0, buf, sizeof( buf ))) > 0 ) {
44 if ( ipc < 0 && *buf == '*' ) {
45 /* find initial pagecount */
46 for ( p = buf, end = buf + cc; p < end; p++ ) {
47 if ( *p == '\n' || *p == '\r' ) {
52 fprintf( stderr, "Can't find initial page count!\n" );
57 ipc = atoi( buf + 1 );
63 /* find final pagecount */
64 for ( p = buf + cc - 1; p >= buf; p-- ) {
65 if ( *p != '\n' && *p != '\r' ) {
70 fprintf( stderr, "Can't find final page count!\n" );
74 for ( ; p >= buf; p-- ) {
75 if ( *p == '\n' || *p == '\r' ) {
89 #define max(x,y) ((x)>(y)?(x):(y))
93 if ( cc != 0 && write( 2, buf, cc ) != cc ) {
94 fprintf( stderr, "write 1: 2 %X %d\n", buf, cc );
105 fprintf( stderr, "Didn't find initial page count!\n" );
110 fprintf( stderr, "Didn't find final page count!\n" );
115 * Write accounting record.
117 if (( af = fopen( acc, "a" )) != NULL ) {
118 fprintf( af, "%7.2f\t%s:%s\n", (float)( fpc - ipc ), host, user );