source: trunk/athena/bin/lpr/filters/ln03filter.c @ 48

Revision 48, 2.9 KB checked in by jtkohl, 38 years ago (diff)
new version from Mike Gretzinger
Line 
1#include <sys/file.h>
2#include <stdio.h>
3
4main(argc, argv)
5char *argv[];
6{
7    char *login;
8    char *host;
9    char *accfile;
10    register int eop = 0;
11    int n;
12    unsigned char buf[2048];
13
14    parse_args(argc, argv, &login, &host, &accfile);
15    n = fread(buf, 1, 2048, stdin);
16    if (is_graphics_file(buf, n))
17        {
18            while (n > 0)
19                {
20                    eop = find_eop(buf, &n);
21                    fwrite(buf, 1, n, stdout);
22                    if (eop) break;
23                    n = fread(buf, 1, 2048, stdin);
24                }
25        }
26    else
27        {
28            output_lossage_sheet(login, host);
29        }
30    fflush(stdout);
31/*    do_accounting(accfile, login, host, eop); */ /* Lossage: see below */
32    exit(0);
33}
34
35
36parse_args(argc, argv, login, host, accfile)
37int argc;
38register char *argv[];
39char **login;
40char **host;
41char **accfile;
42{
43    argv++;
44    while (argv[0][0] == '-' && argv[0][1] != 'n') argv++;
45    argv++;
46    *login = *argv++;
47    argv++;
48    *host = *argv++;
49    *accfile = *argv;
50}
51
52
53is_graphics_file(buf, n)
54unsigned char *buf;
55register int n;
56{
57    register unsigned char *bp;
58    register int i;
59   
60    bp = buf;
61    for (i = 0; i < n; i++)
62        {
63            if (bp[i] == 27 && bp[++i] == 'P' && bp[i+6] == 'q') return(1);
64        }
65    return(0);
66}
67
68find_eop(buf, n)
69unsigned char *buf;
70int *n;
71{
72    register int i = *n;
73    register unsigned char *bp = buf;
74   
75    while (--i >= 0) if (*bp++ == '\f') {
76        *bp++ = '\033';
77        *bp++ = 'c';
78        *n = bp - buf;
79        return (1);
80    }
81    return(0);
82}
83
84#define IDENT "\033c\033[20d\033[30`%s:%s\n"
85#define TIME "\033[25d\033[27`%s\n"
86#define LOSSAGE "\033[30d\033[20`THIS PRINTER IS FOR GRAPHICS OUTPUT ONLY\f"
87
88output_lossage_sheet(login, host)
89char *login;
90char *host;
91{
92    char *timestamp;
93    int atime;
94    char *ctime();
95   
96    atime = time(0);
97    timestamp = ctime(&atime);
98    printf(IDENT, host, login);
99    printf(TIME, timestamp);
100    printf(LOSSAGE);
101}
102
103/*
104** I don't know what is wrong here.  The idea is to open accfile for append
105** and write the accounting information into it.  The accfile is specified
106** on the command line; lpd gets it from the "af" printcap entry.  For some
107** reason the file never gets created.  I tried to create another file
108** explicitly.  This file gets created, but nothing gets written into it.
109** I've tried both stdio and not stdio.  I give up.
110*/
111
112do_accounting(accfile, login, host, pages)
113char *accfile;
114char *login;
115char *host;
116int pages;
117{
118    char acct[64];
119    register FILE *f;
120    register int ft;
121    register int n;
122    register int status;
123
124    ft = open("/tmp/ln03acct", O_WRONLY|O_CREAT|O_APPEND, 0770);
125    if (ft < 0) {
126        perror(open);
127        exit(2);
128    }
129    f = fopen(accfile, "a");
130    if (f == NULL) {
131        perror("fopen");
132        exit(2);
133    }
134    sprintf(acct, "%-12s  %-8s  %d\n", host, login, pages);
135    n = strlen(acct);
136    status = fwrite(acct, 1, n, f);
137    if (status == 0) perror("fwrite");
138    status = write(ft, acct, n);
139    if (status < 0) perror("write");
140    close(ft);
141    fclose(f);
142    fflush(stderr);
143}
Note: See TracBrowser for help on using the repository browser.