ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/getinfo.c
Revision: 2.12
Committed: Mon Sep 8 18:21:39 2014 UTC (9 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad4R2P1
Changes since 2.11: +2 -1 lines
Log Message:
Turned off input buffering for -c option to avoid issues with lost data

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.12 static const char RCSid[] = "$Id: getinfo.c,v 2.11 2014/07/28 20:12:20 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 schorsch 2.8 #include "resolu.h"
15 greg 2.2
16 greg 2.9 #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 greg 2.10 #ifdef _WIN32
24 greg 2.11 #include <process.h>
25 greg 2.10 #define execvp _execvp
26     #endif
27 greg 2.9
28 schorsch 2.8 static gethfunc tabstr;
29 greg 2.10 static void getdim(FILE *fp);
30 schorsch 2.8 static void copycat(void);
31 greg 1.1
32 greg 1.3
33 schorsch 2.8 static int
34     tabstr( /* put out line followed by tab */
35 greg 2.10 char *s,
36 schorsch 2.8 void *p
37     )
38 greg 1.1 {
39     while (*s) {
40     putchar(*s);
41     s++;
42     }
43     if (*--s == '\n')
44     putchar('\t');
45 gwlarson 2.4 return(0);
46 greg 1.1 }
47    
48    
49 schorsch 2.8 int
50     main(
51     int argc,
52     char **argv
53     )
54 greg 1.1 {
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 schorsch 2.6 SET_FILE_BINARY(stdin);
63 greg 2.10 } else if (argc > 2 && !strcmp(argv[1], "-c")) {
64     SET_FILE_BINARY(stdin);
65     SET_FILE_BINARY(stdout);
66 greg 2.12 setvbuf(stdin, NULL, _IONBF, 2);
67 greg 2.10 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 greg 1.2 } else if (argc == 2 && !strcmp(argv[1], "-")) {
75 schorsch 2.6 SET_FILE_BINARY(stdin);
76     SET_FILE_BINARY(stdout);
77 greg 2.3 getheader(stdin, NULL, NULL);
78 greg 1.2 copycat();
79 schorsch 2.8 return 0;
80 greg 1.1 }
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 schorsch 2.8 tabstr(":\n", NULL);
91 greg 1.3 getheader(fp, tabstr, NULL);
92 greg 2.10 fputc('\n', stdout);
93 greg 1.1 }
94     fclose(fp);
95     }
96     }
97 schorsch 2.7 if (argc == 1) {
98 greg 1.1 if (dim) {
99     getdim(stdin);
100     } else {
101 greg 2.10 getheader(stdin, (gethfunc *)fputs, stdout);
102     fputc('\n', stdout);
103 greg 1.1 }
104 schorsch 2.7 }
105 schorsch 2.8 return 0;
106 greg 1.1 }
107    
108    
109 schorsch 2.8 static void
110     getdim( /* get dimensions from file */
111 greg 2.10 FILE *fp
112 schorsch 2.8 )
113 greg 1.1 {
114     int j;
115 greg 2.10 int c;
116 greg 1.1
117 greg 2.3 getheader(fp, NULL, NULL); /* skip header */
118 greg 1.1
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 greg 2.10 if (c == 0) {
131 greg 1.1 if (++j >= 4)
132     break;
133 greg 2.10 putchar(' ');
134     } else {
135 greg 1.1 putchar(c);
136 greg 2.10 }
137 greg 1.1 putchar('\n');
138     break;
139     default: /* ??? */
140     fputs("unknown file type\n", stdout);
141     break;
142     }
143 greg 1.2 }
144    
145    
146 schorsch 2.8 static void
147     copycat(void) /* copy input to output */
148 greg 1.2 {
149 greg 2.10 char buf[8192];
150 greg 2.11 int n;
151 greg 2.3
152 greg 2.10 fflush(stdout);
153     while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0)
154     if (write(fileno(stdout), buf, n) != n)
155     break;
156 greg 1.1 }