--- ray/src/common/portio.c 2019/07/05 03:04:22 2.21 +++ ray/src/common/portio.c 2021/07/12 17:42:51 2.27 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: portio.c,v 2.21 2019/07/05 03:04:22 greg Exp $"; +static const char RCSid[] = "$Id: portio.c,v 2.27 2021/07/12 17:42:51 greg Exp $"; #endif /* * Portable i/o for binary files @@ -14,7 +14,7 @@ static const char RCSid[] = "$Id: portio.c,v 2.21 2019 #include -void +int putstr( /* write null-terminated string to fp */ char *s, FILE *fp @@ -23,23 +23,31 @@ putstr( /* write null-terminated string to fp */ do putc(*s, fp); while (*s++); + + return(ferror(fp) ? EOF : 0); } -void +int putint( /* write a siz-byte integer to fp */ long i, int siz, FILE *fp ) { + while (siz > sizeof(long)) { + putc((i<0)*0xff, fp); + siz--; + } siz <<= 3; - while ((siz -= 8) >= 0) + while ((siz -= 8) > 0) putc((int)(i>>siz & 0xff), fp); + + return(putc((int)(i & 0xff), fp) == EOF ? EOF : 0); } -void +int putflt( /* put out floating point number */ double f, FILE *fp @@ -57,21 +65,21 @@ putflt( /* put out floating point number */ e = 0; } putint(m, 4, fp); - putint((long)e, 1, fp); + return(putint(e, 1, fp)); } -int +size_t putbinary( /* fwrite() replacement for small objects */ const void *p, - int elsiz, - int nel, + size_t elsiz, + size_t nel, FILE *fp) { const char *s = (const char *)p; - int nbytes = elsiz*nel; + size_t nbytes = elsiz*nel; - if (nbytes > 256) + if (nbytes > 128) return(fwrite(p, elsiz, nel, fp)); while (nbytes-- > 0) @@ -111,7 +119,9 @@ getint( /* get a siz-byte integer */ if ((c = getc(fp)) == EOF) return(EOF); - r = 0x80&c ? -1<<8|c : c; /* sign extend */ + r = c; + if (c & 0x80) /* sign extend? */ + r |= -256L; while (--siz > 0) { if ((c = getc(fp)) == EOF) return(EOF); @@ -137,23 +147,23 @@ getflt( /* get a floating point number */ getc(fp); /* exactly zero -- ignore exponent */ return(0.0); } - d = (l + (l > 0 ? .5 : -.5)) * (1./0x7fffffff); + d = (l + .5 - (l<0)) * (1./0x7fffffff); return(ldexp(d, (int)getint(1, fp))); } -int +size_t getbinary( /* fread() replacement for small objects */ void *p, - int elsiz, - int nel, + size_t elsiz, + size_t nel, FILE *fp) { char *s = (char *)p; - int nbytes = elsiz*nel; + size_t nbytes = elsiz*nel; int c; - if (nbytes > 256) + if (nbytes > 128) return(fread(p, elsiz, nel, fp)); while (nbytes-- > 0) {