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