1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: getinfo.c,v 2.12 2014/09/08 18:21:39 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 "resolu.h" |
15 |
|
16 |
#ifdef getc_unlocked /* avoid nasty file-locking overhead */ |
17 |
#undef getchar |
18 |
#undef putchar |
19 |
#define getchar getchar_unlocked |
20 |
#define putchar putchar_unlocked |
21 |
#endif |
22 |
|
23 |
#if defined(_WIN32) || defined(_WIN64) |
24 |
#include <process.h> |
25 |
#define execvp _execvp |
26 |
#endif |
27 |
|
28 |
static gethfunc tabstr; |
29 |
static void getdim(FILE *fp); |
30 |
static void copycat(void); |
31 |
|
32 |
|
33 |
static int |
34 |
tabstr( /* put out line followed by tab */ |
35 |
char *s, |
36 |
void *p |
37 |
) |
38 |
{ |
39 |
while (*s) { |
40 |
putchar(*s); |
41 |
s++; |
42 |
} |
43 |
if (*--s == '\n') |
44 |
putchar('\t'); |
45 |
return(0); |
46 |
} |
47 |
|
48 |
|
49 |
int |
50 |
main( |
51 |
int argc, |
52 |
char **argv |
53 |
) |
54 |
{ |
55 |
int dim = 0; |
56 |
FILE *fp; |
57 |
int i; |
58 |
|
59 |
if (argc > 1 && !strcmp(argv[1], "-d")) { |
60 |
argc--; argv++; |
61 |
dim = 1; |
62 |
SET_FILE_BINARY(stdin); |
63 |
} else if (argc > 2 && !strcmp(argv[1], "-c")) { |
64 |
SET_FILE_BINARY(stdin); |
65 |
SET_FILE_BINARY(stdout); |
66 |
setvbuf(stdin, NULL, _IONBF, 2); |
67 |
getheader(stdin, (gethfunc *)fputs, stdout); |
68 |
printargs(argc-2, argv+2, stdout); |
69 |
fputc('\n', stdout); |
70 |
fflush(stdout); |
71 |
execvp(argv[2], argv+2); |
72 |
perror(argv[2]); |
73 |
return 1; |
74 |
} else if (argc == 2 && !strcmp(argv[1], "-")) { |
75 |
SET_FILE_BINARY(stdin); |
76 |
SET_FILE_BINARY(stdout); |
77 |
getheader(stdin, NULL, NULL); |
78 |
copycat(); |
79 |
return 0; |
80 |
} |
81 |
for (i = 1; i < argc; i++) { |
82 |
fputs(argv[i], stdout); |
83 |
if ((fp = fopen(argv[i], "r")) == NULL) |
84 |
fputs(": cannot open\n", stdout); |
85 |
else { |
86 |
if (dim) { |
87 |
fputs(": ", stdout); |
88 |
getdim(fp); |
89 |
} else { |
90 |
tabstr(":\n", NULL); |
91 |
getheader(fp, tabstr, NULL); |
92 |
fputc('\n', stdout); |
93 |
} |
94 |
fclose(fp); |
95 |
} |
96 |
} |
97 |
if (argc == 1) { |
98 |
if (dim) { |
99 |
getdim(stdin); |
100 |
} else { |
101 |
getheader(stdin, (gethfunc *)fputs, stdout); |
102 |
fputc('\n', stdout); |
103 |
} |
104 |
} |
105 |
return 0; |
106 |
} |
107 |
|
108 |
|
109 |
static void |
110 |
getdim( /* get dimensions from file */ |
111 |
FILE *fp |
112 |
) |
113 |
{ |
114 |
int j; |
115 |
int c; |
116 |
|
117 |
getheader(fp, NULL, NULL); /* skip header */ |
118 |
|
119 |
switch (c = getc(fp)) { |
120 |
case '+': /* picture */ |
121 |
case '-': |
122 |
do |
123 |
putchar(c); |
124 |
while (c != '\n' && (c = getc(fp)) != EOF); |
125 |
break; |
126 |
case 1: /* octree */ |
127 |
getc(fp); |
128 |
j = 0; |
129 |
while ((c = getc(fp)) != EOF) |
130 |
if (c == 0) { |
131 |
if (++j >= 4) |
132 |
break; |
133 |
putchar(' '); |
134 |
} else { |
135 |
putchar(c); |
136 |
} |
137 |
putchar('\n'); |
138 |
break; |
139 |
default: /* ??? */ |
140 |
fputs("unknown file type\n", stdout); |
141 |
break; |
142 |
} |
143 |
} |
144 |
|
145 |
|
146 |
static void |
147 |
copycat(void) /* copy input to output */ |
148 |
{ |
149 |
char buf[8192]; |
150 |
int n; |
151 |
|
152 |
fflush(stdout); |
153 |
while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) |
154 |
if (write(fileno(stdout), buf, n) != n) |
155 |
break; |
156 |
} |