ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/getinfo.c
Revision: 2.15
Committed: Tue Mar 20 17:48:16 2018 UTC (6 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.14: +3 -7 lines
Log Message:
Replaced write() call with writebuf()

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.15 static const char RCSid[] = "$Id: getinfo.c,v 2.14 2016/08/30 06:10:12 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     #undef getchar
19     #undef putchar
20     #define getchar getchar_unlocked
21     #define putchar putchar_unlocked
22     #endif
23    
24 schorsch 2.8 static gethfunc tabstr;
25 greg 2.10 static void getdim(FILE *fp);
26 schorsch 2.8 static void copycat(void);
27 greg 1.1
28 greg 1.3
29 schorsch 2.8 static int
30     tabstr( /* put out line followed by tab */
31 greg 2.10 char *s,
32 schorsch 2.8 void *p
33     )
34 greg 1.1 {
35     while (*s) {
36     putchar(*s);
37     s++;
38     }
39     if (*--s == '\n')
40     putchar('\t');
41 gwlarson 2.4 return(0);
42 greg 1.1 }
43    
44    
45 schorsch 2.8 int
46     main(
47     int argc,
48     char **argv
49     )
50 greg 1.1 {
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 schorsch 2.6 SET_FILE_BINARY(stdin);
59 greg 2.10 } else if (argc > 2 && !strcmp(argv[1], "-c")) {
60     SET_FILE_BINARY(stdin);
61     SET_FILE_BINARY(stdout);
62 greg 2.12 setvbuf(stdin, NULL, _IONBF, 2);
63 greg 2.10 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 greg 2.14 } 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 greg 1.2 } else if (argc == 2 && !strcmp(argv[1], "-")) {
85 schorsch 2.6 SET_FILE_BINARY(stdin);
86     SET_FILE_BINARY(stdout);
87 greg 2.3 getheader(stdin, NULL, NULL);
88 greg 1.2 copycat();
89 schorsch 2.8 return 0;
90 greg 1.1 }
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 schorsch 2.8 tabstr(":\n", NULL);
101 greg 1.3 getheader(fp, tabstr, NULL);
102 greg 2.10 fputc('\n', stdout);
103 greg 1.1 }
104     fclose(fp);
105     }
106     }
107 schorsch 2.7 if (argc == 1) {
108 greg 1.1 if (dim) {
109     getdim(stdin);
110     } else {
111 greg 2.10 getheader(stdin, (gethfunc *)fputs, stdout);
112     fputc('\n', stdout);
113 greg 1.1 }
114 schorsch 2.7 }
115 schorsch 2.8 return 0;
116 greg 1.1 }
117    
118    
119 schorsch 2.8 static void
120     getdim( /* get dimensions from file */
121 greg 2.10 FILE *fp
122 schorsch 2.8 )
123 greg 1.1 {
124     int j;
125 greg 2.10 int c;
126 greg 1.1
127 greg 2.3 getheader(fp, NULL, NULL); /* skip header */
128 greg 1.1
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 greg 2.10 if (c == 0) {
141 greg 1.1 if (++j >= 4)
142     break;
143 greg 2.10 putchar(' ');
144     } else {
145 greg 1.1 putchar(c);
146 greg 2.10 }
147 greg 1.1 putchar('\n');
148     break;
149     default: /* ??? */
150     fputs("unknown file type\n", stdout);
151     break;
152     }
153 greg 1.2 }
154    
155    
156 schorsch 2.8 static void
157     copycat(void) /* copy input to output */
158 greg 1.2 {
159 greg 2.10 char buf[8192];
160 greg 2.11 int n;
161 greg 2.3
162 greg 2.10 fflush(stdout);
163     while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0)
164 greg 2.15 if (writebuf(fileno(stdout), buf, n) != n)
165 greg 2.10 break;
166 greg 1.1 }