ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/getinfo.c
Revision: 2.10
Committed: Mon Jul 28 17:25:03 2014 UTC (8 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.9: +31 -16 lines
Log Message:
Added "-c" option to getinfo for operating on data segment

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.10 static const char RCSid[] = "$Id: getinfo.c,v 2.9 2012/06/09 04:24: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 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     #define execvp _execvp
25     #endif
26 greg 2.9
27 schorsch 2.8 static gethfunc tabstr;
28 greg 2.10 static void getdim(FILE *fp);
29 schorsch 2.8 static void copycat(void);
30 greg 1.1
31 greg 1.3
32 schorsch 2.8 static int
33     tabstr( /* put out line followed by tab */
34 greg 2.10 char *s,
35 schorsch 2.8 void *p
36     )
37 greg 1.1 {
38     while (*s) {
39     putchar(*s);
40     s++;
41     }
42     if (*--s == '\n')
43     putchar('\t');
44 gwlarson 2.4 return(0);
45 greg 1.1 }
46    
47    
48 schorsch 2.8 int
49     main(
50     int argc,
51     char **argv
52     )
53 greg 1.1 {
54     int dim = 0;
55     FILE *fp;
56     int i;
57    
58     if (argc > 1 && !strcmp(argv[1], "-d")) {
59     argc--; argv++;
60     dim = 1;
61 schorsch 2.6 SET_FILE_BINARY(stdin);
62 greg 2.10 } else if (argc > 2 && !strcmp(argv[1], "-c")) {
63     SET_FILE_BINARY(stdin);
64     SET_FILE_BINARY(stdout);
65     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 1.2 } else if (argc == 2 && !strcmp(argv[1], "-")) {
73 schorsch 2.6 SET_FILE_BINARY(stdin);
74     SET_FILE_BINARY(stdout);
75 greg 2.3 getheader(stdin, NULL, NULL);
76 greg 1.2 copycat();
77 schorsch 2.8 return 0;
78 greg 1.1 }
79     for (i = 1; i < argc; i++) {
80     fputs(argv[i], stdout);
81     if ((fp = fopen(argv[i], "r")) == NULL)
82     fputs(": cannot open\n", stdout);
83     else {
84     if (dim) {
85     fputs(": ", stdout);
86     getdim(fp);
87     } else {
88 schorsch 2.8 tabstr(":\n", NULL);
89 greg 1.3 getheader(fp, tabstr, NULL);
90 greg 2.10 fputc('\n', stdout);
91 greg 1.1 }
92     fclose(fp);
93     }
94     }
95 schorsch 2.7 if (argc == 1) {
96 greg 1.1 if (dim) {
97     getdim(stdin);
98     } else {
99 greg 2.10 getheader(stdin, (gethfunc *)fputs, stdout);
100     fputc('\n', stdout);
101 greg 1.1 }
102 schorsch 2.7 }
103 schorsch 2.8 return 0;
104 greg 1.1 }
105    
106    
107 schorsch 2.8 static void
108     getdim( /* get dimensions from file */
109 greg 2.10 FILE *fp
110 schorsch 2.8 )
111 greg 1.1 {
112     int j;
113 greg 2.10 int c;
114 greg 1.1
115 greg 2.3 getheader(fp, NULL, NULL); /* skip header */
116 greg 1.1
117     switch (c = getc(fp)) {
118     case '+': /* picture */
119     case '-':
120     do
121     putchar(c);
122     while (c != '\n' && (c = getc(fp)) != EOF);
123     break;
124     case 1: /* octree */
125     getc(fp);
126     j = 0;
127     while ((c = getc(fp)) != EOF)
128 greg 2.10 if (c == 0) {
129 greg 1.1 if (++j >= 4)
130     break;
131 greg 2.10 putchar(' ');
132     } else {
133 greg 1.1 putchar(c);
134 greg 2.10 }
135 greg 1.1 putchar('\n');
136     break;
137     default: /* ??? */
138     fputs("unknown file type\n", stdout);
139     break;
140     }
141 greg 1.2 }
142    
143    
144 schorsch 2.8 static void
145     copycat(void) /* copy input to output */
146 greg 1.2 {
147 greg 2.10 char buf[8192];
148     ssize_t n;
149 greg 2.3
150 greg 2.10 fflush(stdout);
151     while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0)
152     if (write(fileno(stdout), buf, n) != n)
153     break;
154 greg 1.1 }