| 22 |
|
{ |
| 23 |
|
int fd = open(fname, O_RDONLY|O_BINARY); |
| 24 |
|
|
| 25 |
< |
if (fd < 0) |
| 25 |
> |
if (fd < 0) { |
| 26 |
> |
fprintf(stderr, "%s: cannot open for reading\n", fname); |
| 27 |
|
return(-1); |
| 28 |
+ |
} |
| 29 |
|
if (expected_length > 0) { |
| 30 |
|
off_t flen = lseek(fd, 0, SEEK_END); |
| 31 |
|
if (flen != expected_length*sizeof(float)) { |
| 57 |
|
ssize_t n, nleft; |
| 58 |
|
int fd = open(fname, O_RDONLY); |
| 59 |
|
|
| 60 |
< |
if (fd < 0) |
| 60 |
> |
if (fd < 0) { |
| 61 |
> |
perror(fname); |
| 62 |
|
return(-1); |
| 63 |
+ |
} |
| 64 |
|
dc.finp = NULL; |
| 65 |
|
if (expected_length <= 0) { /* need to sniff file? */ |
| 66 |
|
extern const char HDRSTR[]; |
| 67 |
|
const int len = strlen(HDRSTR); |
| 68 |
< |
n = read(fd, buf, len+1); |
| 69 |
< |
if (n < len+1) |
| 66 |
< |
goto gotEOF; |
| 68 |
> |
if (read(fd, buf, len+1) < len+1) |
| 69 |
> |
goto badEOF; |
| 70 |
|
if (lseek(fd, 0, SEEK_SET) != 0) |
| 71 |
|
goto seek_error; |
| 72 |
|
for (n = 0; n < len; n++) |
| 73 |
|
if (buf[n] != HDRSTR[n]) |
| 74 |
|
break; |
| 75 |
< |
if (n < len || !isprint(buf[len])) |
| 75 |
> |
if ((n < len) | !isprint(buf[len])) |
| 76 |
|
return(fd); /* unknown length raw float... */ |
| 77 |
|
} else { |
| 78 |
|
off_t flen = lseek(fd, 0, SEEK_END); |
| 95 |
|
fclose(dc.finp); /* already reported error */ |
| 96 |
|
return(-1); |
| 97 |
|
} |
| 98 |
< |
if (expected_length > 0 && (long)dc.res.xr*dc.res.yr != expected_length) { |
| 98 |
> |
if ((expected_length > 0) & |
| 99 |
> |
((long)dc.res.xr*dc.res.yr != expected_length)) { |
| 100 |
|
fprintf(stderr, "%s: expected length is %ld, actual length is %ld\n", |
| 101 |
< |
fname, (long)expected_length, (long)dc.res.xr*dc.res.yr); |
| 101 |
> |
fname, expected_length, (long)dc.res.xr*dc.res.yr); |
| 102 |
|
fclose(dc.finp); |
| 103 |
|
return(-1); |
| 104 |
|
} |
| 105 |
|
strcpy(buf, TEMPLATE); /* create temporary file to hold raw */ |
| 106 |
|
fd = mkstemp(buf); |
| 107 |
|
if (fd < 0) { |
| 108 |
< |
fputs(buf, stderr); |
| 105 |
< |
fputs(": cannot create temporary file\n", stderr); |
| 108 |
> |
perror(buf); |
| 109 |
|
fclose(dc.finp); |
| 110 |
|
return(-1); |
| 111 |
|
} |
| 112 |
< |
unlink(buf); /* unlink it now (Windows forbids) */ |
| 112 |
> |
unlink(buf); /* preemptive remove (Windows forbids) */ |
| 113 |
|
for (nleft = (ssize_t)dc.res.xr*dc.res.yr; nleft > 0; nleft -= FBUFLEN) { |
| 114 |
|
for (n = 0; n < FBUFLEN; n++) { |
| 115 |
|
double d = decode_depth_next(&dc); |
| 116 |
< |
if (d < 0) { |
| 116 |
> |
if (d < -FTINY) { |
| 117 |
|
if (n < nleft) |
| 118 |
< |
goto gotEOF; |
| 118 |
> |
goto badEOF; |
| 119 |
|
break; |
| 120 |
|
} |
| 121 |
< |
((float *)buf)[n] = (float)d; |
| 121 |
> |
((float *)buf)[n] = d; |
| 122 |
|
} |
| 123 |
|
n *= sizeof(float); |
| 124 |
|
if (write(fd, buf, n) != n) { |
| 129 |
|
} |
| 130 |
|
} |
| 131 |
|
fclose(dc.finp); /* all done -- clean up */ |
| 132 |
< |
if (lseek(fd, 0, SEEK_SET) != 0) |
| 133 |
< |
goto seek_error; |
| 134 |
< |
return(fd); |
| 135 |
< |
gotEOF: |
| 132 |
> |
if (lseek(fd, 0, SEEK_SET) == 0) |
| 133 |
> |
return(fd); |
| 134 |
> |
seek_error: |
| 135 |
> |
perror("lseek"); |
| 136 |
> |
close(fd); |
| 137 |
> |
return(-1); |
| 138 |
> |
badEOF: |
| 139 |
|
fputs(fname, stderr); |
| 140 |
|
fputs(": unexpected end-of-file\n", stderr); |
| 141 |
|
if (dc.finp) fclose(dc.finp); |
| 136 |
– |
close(fd); |
| 137 |
– |
return(-1); |
| 138 |
– |
seek_error: |
| 139 |
– |
perror("lseek"); |
| 142 |
|
close(fd); |
| 143 |
|
return(-1); |
| 144 |
|
} |