1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: getinfo.c,v 2.14 2016/08/30 06:10:12 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* getinfo.c - program to read info. header from file. |
6 |
* |
7 |
* 1/3/86 |
8 |
*/ |
9 |
|
10 |
#include <stdio.h> |
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 getchar |
19 |
#undef putchar |
20 |
#define getchar getchar_unlocked |
21 |
#define putchar putchar_unlocked |
22 |
#endif |
23 |
|
24 |
static gethfunc tabstr; |
25 |
static void getdim(FILE *fp); |
26 |
static void copycat(void); |
27 |
|
28 |
|
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); |
37 |
s++; |
38 |
} |
39 |
if (*--s == '\n') |
40 |
putchar('\t'); |
41 |
return(0); |
42 |
} |
43 |
|
44 |
|
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")) { |
56 |
argc--; argv++; |
57 |
dim = 1; |
58 |
SET_FILE_BINARY(stdin); |
59 |
} else if (argc > 2 && !strcmp(argv[1], "-c")) { |
60 |
SET_FILE_BINARY(stdin); |
61 |
SET_FILE_BINARY(stdout); |
62 |
setvbuf(stdin, NULL, _IONBF, 2); |
63 |
getheader(stdin, (gethfunc *)fputs, stdout); |
64 |
printargs(argc-2, argv+2, stdout); |
65 |
fputc('\n', stdout); |
66 |
fflush(stdout); |
67 |
execvp(argv[2], argv+2); |
68 |
perror(argv[2]); |
69 |
return 1; |
70 |
} else if (argc > 2 && !strcmp(argv[1], "-a")) { |
71 |
SET_FILE_BINARY(stdin); |
72 |
SET_FILE_BINARY(stdout); |
73 |
getheader(stdin, (gethfunc *)fputs, stdout); |
74 |
for (i = 2; i < argc; i++) { |
75 |
int len = strlen(argv[i]); |
76 |
if (!len) continue; |
77 |
fputs(argv[i], stdout); |
78 |
if (argv[i][len-1] != '\n') |
79 |
fputc('\n', stdout); |
80 |
} |
81 |
fputc('\n', stdout); |
82 |
copycat(); |
83 |
return 0; |
84 |
} else if (argc == 2 && !strcmp(argv[1], "-")) { |
85 |
SET_FILE_BINARY(stdin); |
86 |
SET_FILE_BINARY(stdout); |
87 |
getheader(stdin, NULL, NULL); |
88 |
copycat(); |
89 |
return 0; |
90 |
} |
91 |
for (i = 1; i < argc; i++) { |
92 |
fputs(argv[i], stdout); |
93 |
if ((fp = fopen(argv[i], "r")) == NULL) |
94 |
fputs(": cannot open\n", stdout); |
95 |
else { |
96 |
if (dim) { |
97 |
fputs(": ", stdout); |
98 |
getdim(fp); |
99 |
} else { |
100 |
tabstr(":\n", NULL); |
101 |
getheader(fp, tabstr, NULL); |
102 |
fputc('\n', stdout); |
103 |
} |
104 |
fclose(fp); |
105 |
} |
106 |
} |
107 |
if (argc == 1) { |
108 |
if (dim) { |
109 |
getdim(stdin); |
110 |
} else { |
111 |
getheader(stdin, (gethfunc *)fputs, stdout); |
112 |
fputc('\n', stdout); |
113 |
} |
114 |
} |
115 |
return 0; |
116 |
} |
117 |
|
118 |
|
119 |
static void |
120 |
getdim( /* get dimensions from file */ |
121 |
FILE *fp |
122 |
) |
123 |
{ |
124 |
int j; |
125 |
int c; |
126 |
|
127 |
getheader(fp, NULL, NULL); /* skip header */ |
128 |
|
129 |
switch (c = getc(fp)) { |
130 |
case '+': /* picture */ |
131 |
case '-': |
132 |
do |
133 |
putchar(c); |
134 |
while (c != '\n' && (c = getc(fp)) != EOF); |
135 |
break; |
136 |
case 1: /* octree */ |
137 |
getc(fp); |
138 |
j = 0; |
139 |
while ((c = getc(fp)) != EOF) |
140 |
if (c == 0) { |
141 |
if (++j >= 4) |
142 |
break; |
143 |
putchar(' '); |
144 |
} else { |
145 |
putchar(c); |
146 |
} |
147 |
putchar('\n'); |
148 |
break; |
149 |
default: /* ??? */ |
150 |
fputs("unknown file type\n", stdout); |
151 |
break; |
152 |
} |
153 |
} |
154 |
|
155 |
|
156 |
static void |
157 |
copycat(void) /* copy input to output */ |
158 |
{ |
159 |
char buf[8192]; |
160 |
int n; |
161 |
|
162 |
fflush(stdout); |
163 |
while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) |
164 |
if (writebuf(fileno(stdout), buf, n) != n) |
165 |
break; |
166 |
} |