ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/getinfo.c
Revision: 2.16
Committed: Tue Mar 20 18:18:39 2018 UTC (4 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2
Changes since 2.15: +3 -1 lines
Log Message:
Added redefine of getc()

File Contents

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