11 |
|
#include <string.h> |
12 |
|
|
13 |
|
#include "platform.h" |
14 |
+ |
#include "rtprocess.h" |
15 |
|
#include "resolu.h" |
16 |
|
|
17 |
+ |
#ifdef getc_unlocked /* avoid nasty file-locking overhead */ |
18 |
+ |
#undef getc |
19 |
+ |
#undef getchar |
20 |
+ |
#undef putchar |
21 |
+ |
#define getc getc_unlocked |
22 |
+ |
#define getchar getchar_unlocked |
23 |
+ |
#define putchar putchar_unlocked |
24 |
+ |
#endif |
25 |
+ |
|
26 |
|
static gethfunc tabstr; |
27 |
< |
static void getdim(register FILE *fp); |
27 |
> |
static void getdim(FILE *fp); |
28 |
|
static void copycat(void); |
29 |
|
|
30 |
|
|
31 |
|
static int |
32 |
|
tabstr( /* put out line followed by tab */ |
33 |
< |
register char *s, |
33 |
> |
char *s, |
34 |
|
void *p |
35 |
|
) |
36 |
|
{ |
54 |
|
FILE *fp; |
55 |
|
int i; |
56 |
|
|
57 |
< |
if (argc > 1 && !strcmp(argv[1], "-d")) { |
57 |
> |
if (argc > 1 && (argv[1][0] == '-') | (argv[1][0] == '+') && |
58 |
> |
argv[1][1] == 'd') { |
59 |
> |
dim = 1 - 2*(argv[1][0] == '-'); |
60 |
|
argc--; argv++; |
61 |
< |
dim = 1; |
62 |
< |
SET_DEFAULT_BINARY(); /* for output file */ |
63 |
< |
SET_FILE_BINARY(stdin); |
61 |
> |
} |
62 |
> |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
63 |
> |
flockfile(stdin); |
64 |
> |
#endif |
65 |
> |
SET_FILE_BINARY(stdin); |
66 |
> |
if (argc > 2 && !strcmp(argv[1], "-c")) { |
67 |
> |
SET_FILE_BINARY(stdout); |
68 |
> |
setvbuf(stdin, NULL, _IONBF, 2); |
69 |
> |
if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) |
70 |
> |
return 1; |
71 |
> |
printargs(argc-2, argv+2, stdout); |
72 |
> |
fputc('\n', stdout); |
73 |
> |
if (dim) { /* copy resolution string? */ |
74 |
> |
RESOLU rs; |
75 |
> |
if (!fgetsresolu(&rs, stdin)) { |
76 |
> |
fputs("No resolution string\n", stderr); |
77 |
> |
return 1; |
78 |
> |
} |
79 |
> |
if (dim > 0) |
80 |
> |
fputsresolu(&rs, stdout); |
81 |
> |
} |
82 |
> |
fflush(stdout); |
83 |
> |
execvp(argv[2], argv+2); |
84 |
> |
perror(argv[2]); |
85 |
> |
return 1; |
86 |
> |
} else if (argc > 2 && !strcmp(argv[1], "-a")) { |
87 |
> |
SET_FILE_BINARY(stdout); |
88 |
> |
if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) |
89 |
> |
return 1; |
90 |
> |
for (i = 2; i < argc; i++) { |
91 |
> |
int len = strlen(argv[i]); |
92 |
> |
if (!len) continue; |
93 |
> |
fputs(argv[i], stdout); |
94 |
> |
if (argv[i][len-1] != '\n') |
95 |
> |
fputc('\n', stdout); |
96 |
> |
} |
97 |
> |
fputc('\n', stdout); |
98 |
> |
copycat(); |
99 |
> |
return 0; |
100 |
|
} else if (argc == 2 && !strcmp(argv[1], "-")) { |
53 |
– |
SET_FILE_BINARY(stdin); |
101 |
|
SET_FILE_BINARY(stdout); |
102 |
< |
getheader(stdin, NULL, NULL); |
102 |
> |
if (getheader(stdin, NULL, NULL) < 0) |
103 |
> |
return 1; |
104 |
> |
if (dim < 0) { /* skip resolution string? */ |
105 |
> |
RESOLU rs; |
106 |
> |
if (!fgetsresolu(&rs, stdin)) { |
107 |
> |
fputs("No resolution string\n", stderr); |
108 |
> |
return 1; |
109 |
> |
} |
110 |
> |
} |
111 |
|
copycat(); |
112 |
|
return 0; |
113 |
|
} |
116 |
|
if ((fp = fopen(argv[i], "r")) == NULL) |
117 |
|
fputs(": cannot open\n", stdout); |
118 |
|
else { |
119 |
< |
if (dim) { |
119 |
> |
if (dim < 0) { /* dimensions only */ |
120 |
> |
if (getheader(fp, NULL, NULL) < 0) { |
121 |
> |
fputs("bad header\n", stdout); |
122 |
> |
continue; |
123 |
> |
} |
124 |
|
fputs(": ", stdout); |
125 |
|
getdim(fp); |
126 |
|
} else { |
127 |
|
tabstr(":\n", NULL); |
128 |
< |
getheader(fp, tabstr, NULL); |
129 |
< |
putchar('\n'); |
128 |
> |
if (getheader(fp, tabstr, NULL) < 0) |
129 |
> |
return 1; |
130 |
> |
fputc('\n', stdout); |
131 |
> |
if (dim > 0) { |
132 |
> |
fputc('\t', stdout); |
133 |
> |
getdim(fp); |
134 |
> |
} |
135 |
|
} |
136 |
|
fclose(fp); |
137 |
|
} |
138 |
|
} |
139 |
|
if (argc == 1) { |
140 |
< |
if (dim) { |
140 |
> |
if (dim < 0) { |
141 |
> |
if (getheader(stdin, NULL, NULL) < 0) |
142 |
> |
return 1; |
143 |
|
getdim(stdin); |
144 |
|
} else { |
145 |
< |
getheader(stdin, (gethfunc*)fputs, stdout); |
146 |
< |
putchar('\n'); |
145 |
> |
if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) |
146 |
> |
return 1; |
147 |
> |
fputc('\n', stdout); |
148 |
> |
if (dim > 0) |
149 |
> |
getdim(stdin); |
150 |
|
} |
151 |
|
} |
152 |
|
return 0; |
155 |
|
|
156 |
|
static void |
157 |
|
getdim( /* get dimensions from file */ |
158 |
< |
register FILE *fp |
158 |
> |
FILE *fp |
159 |
|
) |
160 |
|
{ |
161 |
|
int j; |
162 |
< |
register int c; |
162 |
> |
int c; |
163 |
|
|
95 |
– |
getheader(fp, NULL, NULL); /* skip header */ |
96 |
– |
|
164 |
|
switch (c = getc(fp)) { |
165 |
|
case '+': /* picture */ |
166 |
|
case '-': |
172 |
|
getc(fp); |
173 |
|
j = 0; |
174 |
|
while ((c = getc(fp)) != EOF) |
175 |
< |
if (c == 0) |
175 |
> |
if (c == 0) { |
176 |
|
if (++j >= 4) |
177 |
|
break; |
178 |
< |
else |
179 |
< |
putchar(' '); |
113 |
< |
else |
178 |
> |
putchar(' '); |
179 |
> |
} else { |
180 |
|
putchar(c); |
181 |
+ |
} |
182 |
|
putchar('\n'); |
183 |
|
break; |
184 |
|
default: /* ??? */ |
191 |
|
static void |
192 |
|
copycat(void) /* copy input to output */ |
193 |
|
{ |
194 |
< |
register int c; |
194 |
> |
char buf[8192]; |
195 |
> |
int n; |
196 |
|
|
197 |
< |
while ((c = getchar()) != EOF) |
198 |
< |
putchar(c); |
197 |
> |
fflush(stdout); |
198 |
> |
while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) |
199 |
> |
if (writebuf(fileno(stdout), buf, n) != n) |
200 |
> |
break; |
201 |
|
} |