--- ray/src/common/portio.c 2016/08/18 00:52:48 2.19 +++ ray/src/common/portio.c 2021/02/19 16:15:23 2.25 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: portio.c,v 2.19 2016/08/18 00:52:48 greg Exp $"; +static const char RCSid[] = "$Id: portio.c,v 2.25 2021/02/19 16:15:23 greg Exp $"; #endif /* * Portable i/o for binary files @@ -14,7 +14,7 @@ static const char RCSid[] = "$Id: portio.c,v 2.19 2016 #include -void +int putstr( /* write null-terminated string to fp */ char *s, FILE *fp @@ -22,24 +22,32 @@ putstr( /* write null-terminated string to fp */ { do putc(*s, fp); - while (*s++); + while (*++s); + + return(putc(0, fp)); /* terminator */ } -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,25 +65,26 @@ 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 > 512) + if (nbytes > 128) return(fwrite(p, elsiz, nel, fp)); while (nbytes-- > 0) - putc(*s++, fp); + if (putc(*s++, fp) == EOF) + return((elsiz*nel - nbytes)/elsiz); return(nel); } @@ -110,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); @@ -141,18 +152,18 @@ getflt( /* get a floating point number */ } -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 > 512) + if (nbytes > 128) return(fread(p, elsiz, nel, fp)); while (nbytes-- > 0) {