Some examples for printer accounting that I use with samba. lp-acct is
[samba.git] / examples / printer-accounting / hp5-redir
1 #!/usr/bin/perl
2 #
3 # $Source: /data/src/mirror/cvs/samba/examples/printer-accounting/hp5-redir,v $
4 # $Id: hp5-redir,v 1.1 1996/07/23 03:30:56 samba-bugs Exp $
5 #
6 # 0 == stdin  == docuement
7 # 1 == stdout == printer
8 # 2 == stderr == logging
9 #
10 # With redirection to another valid /etc/printcap entry
11 #
12
13 umask(002);
14
15 # -w132 -l66 -i0 -n pkelly -h master.fcp.oypi.com /var/log/lp-acct
16 require "getopts.pl";
17 &Getopts("w:l:i:n:h:");
18
19 chomp($date = `date '+%Y-%m-%d.%T'`);
20
21 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
22         $atime,$mtime,$ctime,$blksize,$blocks)
23  = stat(STDIN);
24
25 # send to the real printer now.
26 open(P, "|lpr -Pmgmt0") || die "Can't print to hp5-real ($!)\n";
27 $cnt = 0;
28 while (sysread(STDIN, $buf, 10240)) {
29         print P $buf;
30         # this is ugly, but it gives the approx in pages.  We
31         # don't print graphics, so ...  There must be a better way :)
32         $cnt += ($buf =~ /^L/g);
33 }
34 close(P);
35
36 $acct = shift;
37 if (open(ACCT, ">>$acct")) {
38         print ACCT "$date $opt_n $opt_h $size $cnt\n";
39         close(ACCT);
40 } else {
41         warn "Err: Can't account for it ($!)\n";
42         warn "Log: $date $opt_n $opt_h $size $cnt\n";
43 }