ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/getinfo.c
Revision: 2.17
Committed: Sun Jun 9 18:22:44 2019 UTC (3 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.16: +14 -8 lines
Log Message:
Added error-checking

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.17 static const char RCSid[] = "$Id: getinfo.c,v 2.16 2018/03/20 18:18:39 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     if (argc > 1 && !strcmp(argv[1], "-d")) {
58     argc--; argv++;
59     dim = 1;
60 schorsch 2.6 SET_FILE_BINARY(stdin);
61 greg 2.10 } else if (argc > 2 && !strcmp(argv[1], "-c")) {
62     SET_FILE_BINARY(stdin);
63     SET_FILE_BINARY(stdout);
64 greg 2.12 setvbuf(stdin, NULL, _IONBF, 2);
65 greg 2.17 if (getheader(stdin, (gethfunc *)fputs, stdout) < 0)
66     return 1;
67 greg 2.10 printargs(argc-2, argv+2, stdout);
68     fputc('\n', stdout);
69     fflush(stdout);
70     execvp(argv[2], argv+2);
71     perror(argv[2]);
72     return 1;
73 greg 2.14 } else if (argc > 2 && !strcmp(argv[1], "-a")) {
74     SET_FILE_BINARY(stdin);
75     SET_FILE_BINARY(stdout);
76 greg 2.17 if (getheader(stdin, (gethfunc *)fputs, stdout) < 0)
77     return 1;
78 greg 2.14 for (i = 2; i < argc; i++) {
79     int len = strlen(argv[i]);
80     if (!len) continue;
81     fputs(argv[i], stdout);
82     if (argv[i][len-1] != '\n')
83     fputc('\n', stdout);
84     }
85     fputc('\n', stdout);
86     copycat();
87     return 0;
88 greg 1.2 } else if (argc == 2 && !strcmp(argv[1], "-")) {
89 schorsch 2.6 SET_FILE_BINARY(stdin);
90     SET_FILE_BINARY(stdout);
91 greg 2.17 if (getheader(stdin, NULL, NULL) < 0)
92     return 1;
93 greg 1.2 copycat();
94 schorsch 2.8 return 0;
95 greg 1.1 }
96     for (i = 1; i < argc; i++) {
97     fputs(argv[i], stdout);
98     if ((fp = fopen(argv[i], "r")) == NULL)
99     fputs(": cannot open\n", stdout);
100     else {
101     if (dim) {
102     fputs(": ", stdout);
103     getdim(fp);
104     } else {
105 schorsch 2.8 tabstr(":\n", NULL);
106 greg 1.3 getheader(fp, tabstr, NULL);
107 greg 2.10 fputc('\n', stdout);
108 greg 1.1 }
109     fclose(fp);
110     }
111     }
112 schorsch 2.7 if (argc == 1) {
113 greg 1.1 if (dim) {
114     getdim(stdin);
115     } else {
116 greg 2.17 if (getheader(stdin, (gethfunc *)fputs, stdout) < 0)
117     return 1;
118 greg 2.10 fputc('\n', stdout);
119 greg 1.1 }
120 schorsch 2.7 }
121 schorsch 2.8 return 0;
122 greg 1.1 }
123    
124    
125 schorsch 2.8 static void
126     getdim( /* get dimensions from file */
127 greg 2.10 FILE *fp
128 schorsch 2.8 )
129 greg 1.1 {
130     int j;
131 greg 2.10 int c;
132 greg 2.17 /* skip header */
133     if (getheader(fp, NULL, NULL) < 0) {
134     fputs("bad header\n", stdout);
135     return;
136     }
137 greg 1.1 switch (c = getc(fp)) {
138     case '+': /* picture */
139     case '-':
140     do
141     putchar(c);
142     while (c != '\n' && (c = getc(fp)) != EOF);
143     break;
144     case 1: /* octree */
145     getc(fp);
146     j = 0;
147     while ((c = getc(fp)) != EOF)
148 greg 2.10 if (c == 0) {
149 greg 1.1 if (++j >= 4)
150     break;
151 greg 2.10 putchar(' ');
152     } else {
153 greg 1.1 putchar(c);
154 greg 2.10 }
155 greg 1.1 putchar('\n');
156     break;
157     default: /* ??? */
158     fputs("unknown file type\n", stdout);
159     break;
160     }
161 greg 1.2 }
162    
163    
164 schorsch 2.8 static void
165     copycat(void) /* copy input to output */
166 greg 1.2 {
167 greg 2.10 char buf[8192];
168 greg 2.11 int n;
169 greg 2.3
170 greg 2.10 fflush(stdout);
171     while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0)
172 greg 2.15 if (writebuf(fileno(stdout), buf, n) != n)
173 greg 2.10 break;
174 greg 1.1 }