1 |
– |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* getinfo.c - program to read info. header from file. |
6 |
|
* |
7 |
|
* 1/3/86 |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include <stdio.h> |
10 |
> |
#include "rtio.h" |
11 |
> |
#include "platform.h" |
12 |
> |
#include "rtprocess.h" |
13 |
> |
#include "resolu.h" |
14 |
|
|
15 |
< |
#ifdef MSDOS |
16 |
< |
#include <fcntl.h> |
15 |
> |
#ifdef getc_unlocked /* avoid nasty file-locking overhead */ |
16 |
> |
#undef getc |
17 |
> |
#undef getchar |
18 |
> |
#undef putchar |
19 |
> |
#define getc getc_unlocked |
20 |
> |
#define getchar getchar_unlocked |
21 |
> |
#define putchar putchar_unlocked |
22 |
|
#endif |
23 |
|
|
24 |
< |
extern int fputs(); |
24 |
> |
static gethfunc tabstr; |
25 |
> |
static void getdim(FILE *fp); |
26 |
> |
static void copycat(void); |
27 |
|
|
28 |
|
|
29 |
< |
tabstr(s) /* put out line followed by tab */ |
30 |
< |
register char *s; |
29 |
> |
static int |
30 |
> |
tabstr( /* put out line followed by tab */ |
31 |
> |
char *s, |
32 |
> |
void *p |
33 |
> |
) |
34 |
|
{ |
35 |
|
while (*s) { |
36 |
|
putchar(*s); |
38 |
|
} |
39 |
|
if (*--s == '\n') |
40 |
|
putchar('\t'); |
41 |
+ |
return(0); |
42 |
|
} |
43 |
|
|
44 |
|
|
45 |
< |
main(argc, argv) |
46 |
< |
int argc; |
47 |
< |
char *argv[]; |
45 |
> |
int |
46 |
> |
main( |
47 |
> |
int argc, |
48 |
> |
char **argv |
49 |
> |
) |
50 |
|
{ |
51 |
|
int dim = 0; |
52 |
|
FILE *fp; |
53 |
|
int i; |
54 |
|
|
55 |
< |
if (argc > 1 && !strcmp(argv[1], "-d")) { |
55 |
> |
if (argc > 1 && (argv[1][0] == '-') | (argv[1][0] == '+') && |
56 |
> |
argv[1][1] == 'd') { |
57 |
> |
dim = 1 - 2*(argv[1][0] == '-'); |
58 |
|
argc--; argv++; |
59 |
< |
dim = 1; |
60 |
< |
} else if (argc == 2 && !strcmp(argv[1], "-")) { |
61 |
< |
#ifdef MSDOS |
47 |
< |
setmode(fileno(stdin), O_BINARY); |
48 |
< |
setmode(fileno(stdout), O_BINARY); |
59 |
> |
} |
60 |
> |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
61 |
> |
flockfile(stdin); |
62 |
|
#endif |
63 |
< |
getheader(stdin, NULL); |
63 |
> |
SET_FILE_BINARY(stdin); |
64 |
> |
if (argc > 2 && !strcmp(argv[1], "-c")) { |
65 |
> |
SET_FILE_BINARY(stdout); |
66 |
> |
setvbuf(stdin, NULL, _IONBF, 2); |
67 |
> |
if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) { |
68 |
> |
fputs("Bad header!\n", stderr); |
69 |
> |
return 1; |
70 |
> |
} |
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 |
> |
fputs("Bad header!\n", stderr); |
90 |
> |
return 1; |
91 |
> |
} |
92 |
> |
for (i = 2; i < argc; i++) { |
93 |
> |
int len = strlen(argv[i]); |
94 |
> |
if (!len) continue; |
95 |
> |
fputs(argv[i], stdout); |
96 |
> |
if (argv[i][len-1] != '\n') |
97 |
> |
fputc('\n', stdout); |
98 |
> |
} |
99 |
> |
fputc('\n', stdout); |
100 |
|
copycat(); |
101 |
< |
exit(0); |
101 |
> |
return 0; |
102 |
> |
} else if (argc == 2 && !strcmp(argv[1], "-")) { |
103 |
> |
SET_FILE_BINARY(stdout); |
104 |
> |
if (getheader(stdin, NULL, NULL) < 0) { |
105 |
> |
fputs("Bad header!\n", stderr); |
106 |
> |
return 1; |
107 |
> |
} |
108 |
> |
if (dim < 0) { /* skip resolution string? */ |
109 |
> |
RESOLU rs; |
110 |
> |
if (!fgetsresolu(&rs, stdin)) { |
111 |
> |
fputs("No resolution string!\n", stderr); |
112 |
> |
return 1; |
113 |
> |
} |
114 |
> |
} |
115 |
> |
copycat(); |
116 |
> |
return 0; |
117 |
|
} |
118 |
|
for (i = 1; i < argc; i++) { |
119 |
|
fputs(argv[i], stdout); |
120 |
|
if ((fp = fopen(argv[i], "r")) == NULL) |
121 |
|
fputs(": cannot open\n", stdout); |
122 |
|
else { |
123 |
< |
if (dim) { |
123 |
> |
if (dim < 0) { /* dimensions only */ |
124 |
> |
if (getheader(fp, NULL, NULL) < 0) { |
125 |
> |
fputs("bad header!\n", stdout); |
126 |
> |
continue; |
127 |
> |
} |
128 |
|
fputs(": ", stdout); |
129 |
|
getdim(fp); |
130 |
|
} else { |
131 |
< |
tabstr(":\n"); |
132 |
< |
getheader(fp, tabstr, NULL); |
133 |
< |
putchar('\n'); |
131 |
> |
tabstr(":\n", NULL); |
132 |
> |
if (getheader(fp, tabstr, NULL) < 0) { |
133 |
> |
fputs(argv[i], stderr); |
134 |
> |
fputs(": bad header!\n", stderr); |
135 |
> |
return 1; |
136 |
> |
} |
137 |
> |
fputc('\n', stdout); |
138 |
> |
if (dim > 0) { |
139 |
> |
fputc('\t', stdout); |
140 |
> |
getdim(fp); |
141 |
> |
} |
142 |
|
} |
143 |
|
fclose(fp); |
144 |
|
} |
145 |
|
} |
146 |
< |
if (argc == 1) |
147 |
< |
if (dim) { |
146 |
> |
if (argc == 1) { |
147 |
> |
if (dim < 0) { |
148 |
> |
if (getheader(stdin, NULL, NULL) < 0) { |
149 |
> |
fputs("Bad header!\n", stderr); |
150 |
> |
return 1; |
151 |
> |
} |
152 |
|
getdim(stdin); |
153 |
|
} else { |
154 |
< |
getheader(stdin, fputs, stdout); |
155 |
< |
putchar('\n'); |
154 |
> |
if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) { |
155 |
> |
fputs("Bad header!\n", stderr); |
156 |
> |
return 1; |
157 |
> |
} |
158 |
> |
fputc('\n', stdout); |
159 |
> |
if (dim > 0) |
160 |
> |
getdim(stdin); |
161 |
|
} |
162 |
< |
exit(0); |
162 |
> |
} |
163 |
> |
return 0; |
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
< |
getdim(fp) /* get dimensions from file */ |
168 |
< |
register FILE *fp; |
167 |
> |
static void |
168 |
> |
getdim( /* get dimensions from file */ |
169 |
> |
FILE *fp |
170 |
> |
) |
171 |
|
{ |
172 |
|
int j; |
173 |
< |
register int c; |
173 |
> |
int c; |
174 |
|
|
87 |
– |
getheader(fp, NULL); /* skip header */ |
88 |
– |
|
175 |
|
switch (c = getc(fp)) { |
176 |
|
case '+': /* picture */ |
177 |
|
case '-': |
183 |
|
getc(fp); |
184 |
|
j = 0; |
185 |
|
while ((c = getc(fp)) != EOF) |
186 |
< |
if (c == 0) |
186 |
> |
if (c == 0) { |
187 |
|
if (++j >= 4) |
188 |
|
break; |
189 |
< |
else |
190 |
< |
putchar(' '); |
105 |
< |
else |
189 |
> |
putchar(' '); |
190 |
> |
} else { |
191 |
|
putchar(c); |
192 |
+ |
} |
193 |
|
putchar('\n'); |
194 |
|
break; |
195 |
|
default: /* ??? */ |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
< |
copycat() /* copy input to output */ |
202 |
> |
static void |
203 |
> |
copycat(void) /* copy input to output */ |
204 |
|
{ |
205 |
< |
register int c; |
206 |
< |
|
207 |
< |
while ((c = getchar()) != EOF) |
208 |
< |
putchar(c); |
205 |
> |
char buf[8192]; |
206 |
> |
int n; |
207 |
> |
|
208 |
> |
fflush(stdout); |
209 |
> |
while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) |
210 |
> |
if (writebuf(fileno(stdout), buf, n) != n) |
211 |
> |
break; |
212 |
|
} |