| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: getinfo.c,v 2.9 2012/06/09 04:24:16 greg Exp $";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* getinfo.c - program to read info. header from file.
|
| 6 |
*
|
| 7 |
* 1/3/86
|
| 8 |
*/
|
| 9 |
|
| 10 |
#include <stdio.h>
|
| 11 |
#include <string.h>
|
| 12 |
|
| 13 |
#include "platform.h"
|
| 14 |
#include "resolu.h"
|
| 15 |
|
| 16 |
#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 |
#ifdef _WIN32
|
| 24 |
#define execvp _execvp
|
| 25 |
#endif
|
| 26 |
|
| 27 |
static gethfunc tabstr;
|
| 28 |
static void getdim(FILE *fp);
|
| 29 |
static void copycat(void);
|
| 30 |
|
| 31 |
|
| 32 |
static int
|
| 33 |
tabstr( /* put out line followed by tab */
|
| 34 |
char *s,
|
| 35 |
void *p
|
| 36 |
)
|
| 37 |
{
|
| 38 |
while (*s) {
|
| 39 |
putchar(*s);
|
| 40 |
s++;
|
| 41 |
}
|
| 42 |
if (*--s == '\n')
|
| 43 |
putchar('\t');
|
| 44 |
return(0);
|
| 45 |
}
|
| 46 |
|
| 47 |
|
| 48 |
int
|
| 49 |
main(
|
| 50 |
int argc,
|
| 51 |
char **argv
|
| 52 |
)
|
| 53 |
{
|
| 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 |
SET_FILE_BINARY(stdin);
|
| 62 |
} 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 |
} else if (argc == 2 && !strcmp(argv[1], "-")) {
|
| 73 |
SET_FILE_BINARY(stdin);
|
| 74 |
SET_FILE_BINARY(stdout);
|
| 75 |
getheader(stdin, NULL, NULL);
|
| 76 |
copycat();
|
| 77 |
return 0;
|
| 78 |
}
|
| 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 |
tabstr(":\n", NULL);
|
| 89 |
getheader(fp, tabstr, NULL);
|
| 90 |
fputc('\n', stdout);
|
| 91 |
}
|
| 92 |
fclose(fp);
|
| 93 |
}
|
| 94 |
}
|
| 95 |
if (argc == 1) {
|
| 96 |
if (dim) {
|
| 97 |
getdim(stdin);
|
| 98 |
} else {
|
| 99 |
getheader(stdin, (gethfunc *)fputs, stdout);
|
| 100 |
fputc('\n', stdout);
|
| 101 |
}
|
| 102 |
}
|
| 103 |
return 0;
|
| 104 |
}
|
| 105 |
|
| 106 |
|
| 107 |
static void
|
| 108 |
getdim( /* get dimensions from file */
|
| 109 |
FILE *fp
|
| 110 |
)
|
| 111 |
{
|
| 112 |
int j;
|
| 113 |
int c;
|
| 114 |
|
| 115 |
getheader(fp, NULL, NULL); /* skip header */
|
| 116 |
|
| 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 |
if (c == 0) {
|
| 129 |
if (++j >= 4)
|
| 130 |
break;
|
| 131 |
putchar(' ');
|
| 132 |
} else {
|
| 133 |
putchar(c);
|
| 134 |
}
|
| 135 |
putchar('\n');
|
| 136 |
break;
|
| 137 |
default: /* ??? */
|
| 138 |
fputs("unknown file type\n", stdout);
|
| 139 |
break;
|
| 140 |
}
|
| 141 |
}
|
| 142 |
|
| 143 |
|
| 144 |
static void
|
| 145 |
copycat(void) /* copy input to output */
|
| 146 |
{
|
| 147 |
char buf[8192];
|
| 148 |
ssize_t n;
|
| 149 |
|
| 150 |
fflush(stdout);
|
| 151 |
while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0)
|
| 152 |
if (write(fileno(stdout), buf, n) != n)
|
| 153 |
break;
|
| 154 |
}
|