--- ray/src/common/fltdepth.c 2019/11/07 23:33:45 3.2 +++ ray/src/common/fltdepth.c 2020/03/05 17:37:09 3.5 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: fltdepth.c,v 3.2 2019/11/07 23:33:45 greg Exp $"; +static const char RCSid[] = "$Id: fltdepth.c,v 3.5 2020/03/05 17:37:09 greg Exp $"; #endif /* * Function to open floating-point depth file, making sure it's @@ -22,8 +22,10 @@ open_float_depth(const char *fname, long expected_leng { int fd = open(fname, O_RDONLY|O_BINARY); - if (fd < 0) + if (fd < 0) { + fprintf(stderr, "%s: cannot open for reading\n", fname); return(-1); + } if (expected_length > 0) { off_t flen = lseek(fd, 0, SEEK_END); if (flen != expected_length*sizeof(float)) { @@ -55,21 +57,22 @@ open_float_depth(const char *fname, long expected_leng ssize_t n, nleft; int fd = open(fname, O_RDONLY); - if (fd < 0) + if (fd < 0) { + perror(fname); return(-1); + } dc.finp = NULL; if (expected_length <= 0) { /* need to sniff file? */ extern const char HDRSTR[]; const int len = strlen(HDRSTR); - n = read(fd, buf, len+1); - if (n < len+1) - goto gotEOF; + if (read(fd, buf, len+1) < len+1) + goto badEOF; if (lseek(fd, 0, SEEK_SET) != 0) goto seek_error; for (n = 0; n < len; n++) if (buf[n] != HDRSTR[n]) break; - if (n < len || !isprint(buf[len])) + if ((n < len) | !isprint(buf[len])) return(fd); /* unknown length raw float... */ } else { off_t flen = lseek(fd, 0, SEEK_END); @@ -92,30 +95,30 @@ open_float_depth(const char *fname, long expected_leng fclose(dc.finp); /* already reported error */ return(-1); } - if (expected_length > 0 && (long)dc.res.xr*dc.res.yr != expected_length) { + if ((expected_length > 0) & + ((long)dc.res.xr*dc.res.yr != expected_length)) { fprintf(stderr, "%s: expected length is %ld, actual length is %ld\n", - fname, (long)expected_length, (long)dc.res.xr*dc.res.yr); + fname, expected_length, (long)dc.res.xr*dc.res.yr); fclose(dc.finp); return(-1); } strcpy(buf, TEMPLATE); /* create temporary file to hold raw */ fd = mkstemp(buf); if (fd < 0) { - fputs(buf, stderr); - fputs(": cannot create temporary file\n", stderr); + perror(buf); fclose(dc.finp); return(-1); } - unlink(buf); /* unlink it now (Windows forbids) */ + unlink(buf); /* preemptive remove (Windows forbids) */ for (nleft = (ssize_t)dc.res.xr*dc.res.yr; nleft > 0; nleft -= FBUFLEN) { for (n = 0; n < FBUFLEN; n++) { double d = decode_depth_next(&dc); - if (d < 0) { + if (d < -FTINY) { if (n < nleft) - goto gotEOF; + goto badEOF; break; } - ((float *)buf)[n] = (float)d; + ((float *)buf)[n] = d; } n *= sizeof(float); if (write(fd, buf, n) != n) { @@ -126,17 +129,16 @@ open_float_depth(const char *fname, long expected_leng } } fclose(dc.finp); /* all done -- clean up */ - if (lseek(fd, 0, SEEK_SET) != 0) - goto seek_error; - return(fd); -gotEOF: + if (lseek(fd, 0, SEEK_SET) == 0) + return(fd); +seek_error: + perror("lseek"); + close(fd); + return(-1); +badEOF: fputs(fname, stderr); fputs(": unexpected end-of-file\n", stderr); if (dc.finp) fclose(dc.finp); - close(fd); - return(-1); -seek_error: - perror("lseek"); close(fd); return(-1); }