1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
2.19 |
static const char RCSid[] = "$Id: getinfo.c,v 2.18 2019/07/06 14:08:07 greg Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* getinfo.c - program to read info. header from file. |
6 |
|
|
* |
7 |
|
|
* 1/3/86 |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#include <stdio.h> |
11 |
schorsch |
2.8 |
#include <string.h> |
12 |
greg |
1.1 |
|
13 |
schorsch |
2.6 |
#include "platform.h" |
14 |
greg |
2.15 |
#include "rtprocess.h" |
15 |
schorsch |
2.8 |
#include "resolu.h" |
16 |
greg |
2.2 |
|
17 |
greg |
2.9 |
#ifdef getc_unlocked /* avoid nasty file-locking overhead */ |
18 |
greg |
2.16 |
#undef getc |
19 |
greg |
2.9 |
#undef getchar |
20 |
|
|
#undef putchar |
21 |
greg |
2.16 |
#define getc getc_unlocked |
22 |
greg |
2.9 |
#define getchar getchar_unlocked |
23 |
|
|
#define putchar putchar_unlocked |
24 |
|
|
#endif |
25 |
|
|
|
26 |
schorsch |
2.8 |
static gethfunc tabstr; |
27 |
greg |
2.10 |
static void getdim(FILE *fp); |
28 |
schorsch |
2.8 |
static void copycat(void); |
29 |
greg |
1.1 |
|
30 |
greg |
1.3 |
|
31 |
schorsch |
2.8 |
static int |
32 |
|
|
tabstr( /* put out line followed by tab */ |
33 |
greg |
2.10 |
char *s, |
34 |
schorsch |
2.8 |
void *p |
35 |
|
|
) |
36 |
greg |
1.1 |
{ |
37 |
|
|
while (*s) { |
38 |
|
|
putchar(*s); |
39 |
|
|
s++; |
40 |
|
|
} |
41 |
|
|
if (*--s == '\n') |
42 |
|
|
putchar('\t'); |
43 |
gwlarson |
2.4 |
return(0); |
44 |
greg |
1.1 |
} |
45 |
|
|
|
46 |
|
|
|
47 |
schorsch |
2.8 |
int |
48 |
|
|
main( |
49 |
|
|
int argc, |
50 |
|
|
char **argv |
51 |
|
|
) |
52 |
greg |
1.1 |
{ |
53 |
|
|
int dim = 0; |
54 |
|
|
FILE *fp; |
55 |
|
|
int i; |
56 |
|
|
|
57 |
greg |
2.19 |
if (argc > 1 && (argv[1][0] == '-') | (argv[1][0] == '+') && |
58 |
|
|
argv[1][1] == 'd') { |
59 |
|
|
dim = 1 - 2*(argv[1][0] == '-'); |
60 |
greg |
1.1 |
argc--; argv++; |
61 |
greg |
2.18 |
} |
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 |
greg |
2.10 |
SET_FILE_BINARY(stdout); |
68 |
greg |
2.12 |
setvbuf(stdin, NULL, _IONBF, 2); |
69 |
greg |
2.17 |
if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) |
70 |
|
|
return 1; |
71 |
greg |
2.10 |
printargs(argc-2, argv+2, stdout); |
72 |
|
|
fputc('\n', stdout); |
73 |
greg |
2.18 |
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 |
greg |
2.19 |
if (dim > 0) |
80 |
|
|
fputsresolu(&rs, stdout); |
81 |
greg |
2.18 |
} |
82 |
greg |
2.10 |
fflush(stdout); |
83 |
|
|
execvp(argv[2], argv+2); |
84 |
|
|
perror(argv[2]); |
85 |
|
|
return 1; |
86 |
greg |
2.14 |
} else if (argc > 2 && !strcmp(argv[1], "-a")) { |
87 |
|
|
SET_FILE_BINARY(stdout); |
88 |
greg |
2.17 |
if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) |
89 |
|
|
return 1; |
90 |
greg |
2.14 |
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 |
greg |
1.2 |
} else if (argc == 2 && !strcmp(argv[1], "-")) { |
101 |
schorsch |
2.6 |
SET_FILE_BINARY(stdout); |
102 |
greg |
2.17 |
if (getheader(stdin, NULL, NULL) < 0) |
103 |
|
|
return 1; |
104 |
greg |
2.19 |
if (dim < 0) { /* skip resolution string? */ |
105 |
greg |
2.18 |
RESOLU rs; |
106 |
|
|
if (!fgetsresolu(&rs, stdin)) { |
107 |
|
|
fputs("No resolution string\n", stderr); |
108 |
|
|
return 1; |
109 |
|
|
} |
110 |
|
|
} |
111 |
greg |
1.2 |
copycat(); |
112 |
schorsch |
2.8 |
return 0; |
113 |
greg |
1.1 |
} |
114 |
|
|
for (i = 1; i < argc; i++) { |
115 |
|
|
fputs(argv[i], stdout); |
116 |
|
|
if ((fp = fopen(argv[i], "r")) == NULL) |
117 |
|
|
fputs(": cannot open\n", stdout); |
118 |
|
|
else { |
119 |
greg |
2.19 |
if (dim < 0) { /* dimensions only */ |
120 |
|
|
if (getheader(fp, NULL, NULL) < 0) { |
121 |
|
|
fputs("bad header\n", stdout); |
122 |
|
|
continue; |
123 |
|
|
} |
124 |
greg |
1.1 |
fputs(": ", stdout); |
125 |
|
|
getdim(fp); |
126 |
|
|
} else { |
127 |
schorsch |
2.8 |
tabstr(":\n", NULL); |
128 |
greg |
2.19 |
if (getheader(fp, tabstr, NULL) < 0) |
129 |
|
|
return 1; |
130 |
greg |
2.10 |
fputc('\n', stdout); |
131 |
greg |
2.19 |
if (dim > 0) { |
132 |
|
|
fputc('\t', stdout); |
133 |
|
|
getdim(fp); |
134 |
|
|
} |
135 |
greg |
1.1 |
} |
136 |
|
|
fclose(fp); |
137 |
|
|
} |
138 |
|
|
} |
139 |
schorsch |
2.7 |
if (argc == 1) { |
140 |
greg |
2.19 |
if (dim < 0) { |
141 |
|
|
if (getheader(stdin, NULL, NULL) < 0) |
142 |
|
|
return 1; |
143 |
greg |
1.1 |
getdim(stdin); |
144 |
|
|
} else { |
145 |
greg |
2.17 |
if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) |
146 |
|
|
return 1; |
147 |
greg |
2.10 |
fputc('\n', stdout); |
148 |
greg |
2.19 |
if (dim > 0) |
149 |
|
|
getdim(stdin); |
150 |
greg |
1.1 |
} |
151 |
schorsch |
2.7 |
} |
152 |
schorsch |
2.8 |
return 0; |
153 |
greg |
1.1 |
} |
154 |
|
|
|
155 |
|
|
|
156 |
schorsch |
2.8 |
static void |
157 |
|
|
getdim( /* get dimensions from file */ |
158 |
greg |
2.10 |
FILE *fp |
159 |
schorsch |
2.8 |
) |
160 |
greg |
1.1 |
{ |
161 |
|
|
int j; |
162 |
greg |
2.10 |
int c; |
163 |
greg |
2.19 |
|
164 |
greg |
1.1 |
switch (c = getc(fp)) { |
165 |
|
|
case '+': /* picture */ |
166 |
|
|
case '-': |
167 |
|
|
do |
168 |
|
|
putchar(c); |
169 |
|
|
while (c != '\n' && (c = getc(fp)) != EOF); |
170 |
|
|
break; |
171 |
|
|
case 1: /* octree */ |
172 |
|
|
getc(fp); |
173 |
|
|
j = 0; |
174 |
|
|
while ((c = getc(fp)) != EOF) |
175 |
greg |
2.10 |
if (c == 0) { |
176 |
greg |
1.1 |
if (++j >= 4) |
177 |
|
|
break; |
178 |
greg |
2.10 |
putchar(' '); |
179 |
|
|
} else { |
180 |
greg |
1.1 |
putchar(c); |
181 |
greg |
2.10 |
} |
182 |
greg |
1.1 |
putchar('\n'); |
183 |
|
|
break; |
184 |
|
|
default: /* ??? */ |
185 |
|
|
fputs("unknown file type\n", stdout); |
186 |
|
|
break; |
187 |
|
|
} |
188 |
greg |
1.2 |
} |
189 |
|
|
|
190 |
|
|
|
191 |
schorsch |
2.8 |
static void |
192 |
|
|
copycat(void) /* copy input to output */ |
193 |
greg |
1.2 |
{ |
194 |
greg |
2.10 |
char buf[8192]; |
195 |
greg |
2.11 |
int n; |
196 |
greg |
2.3 |
|
197 |
greg |
2.10 |
fflush(stdout); |
198 |
|
|
while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) |
199 |
greg |
2.15 |
if (writebuf(fileno(stdout), buf, n) != n) |
200 |
greg |
2.10 |
break; |
201 |
greg |
1.1 |
} |