[17] | 1 | #include <sys/file.h> |
---|
[48] | 2 | #include <stdio.h> |
---|
[17] | 3 | |
---|
[48] | 4 | main(argc, argv) |
---|
| 5 | char *argv[]; |
---|
| 6 | { |
---|
| 7 | char *login; |
---|
| 8 | char *host; |
---|
| 9 | char *accfile; |
---|
| 10 | register int eop = 0; |
---|
| 11 | int n; |
---|
| 12 | unsigned char buf[2048]; |
---|
[17] | 13 | |
---|
[48] | 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 | |
---|
| 36 | parse_args(argc, argv, login, host, accfile) |
---|
| 37 | int argc; |
---|
| 38 | register char *argv[]; |
---|
| 39 | char **login; |
---|
| 40 | char **host; |
---|
| 41 | char **accfile; |
---|
[17] | 42 | { |
---|
[48] | 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 | |
---|
| 53 | is_graphics_file(buf, n) |
---|
| 54 | unsigned char *buf; |
---|
| 55 | register int n; |
---|
| 56 | { |
---|
| 57 | register unsigned char *bp; |
---|
| 58 | register int i; |
---|
[17] | 59 | |
---|
[48] | 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); |
---|
[17] | 66 | } |
---|
[48] | 67 | |
---|
| 68 | find_eop(buf, n) |
---|
| 69 | unsigned char *buf; |
---|
| 70 | int *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 | |
---|
| 88 | output_lossage_sheet(login, host) |
---|
| 89 | char *login; |
---|
| 90 | char *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 | |
---|
| 112 | do_accounting(accfile, login, host, pages) |
---|
| 113 | char *accfile; |
---|
| 114 | char *login; |
---|
| 115 | char *host; |
---|
| 116 | int 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 | } |
---|