--- ray/src/common/portio.c 1992/09/21 12:02:23 2.2 +++ ray/src/common/portio.c 2012/10/19 01:40:33 2.15 @@ -1,19 +1,31 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: portio.c,v 2.15 2012/10/19 01:40:33 greg Exp $"; #endif - /* * Portable i/o for binary files + * + * External symbols declared in rtio.h */ -#include +#include "copyright.h" +#include "rtio.h" -putstr(s, fp) /* write null-terminated string to fp */ -register char *s; -register FILE *fp; +#include + +#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ +#undef getc +#undef putc +#define getc getc_unlocked +#define putc putc_unlocked +#endif + + +void +putstr( /* write null-terminated string to fp */ + char *s, + FILE *fp +) { do putc(*s, fp); @@ -21,35 +33,48 @@ register FILE *fp; } -putint(i, siz, fp) /* write a siz-byte integer to fp */ -long i; -register int siz; -register FILE *fp; +void +putint( /* write a siz-byte integer to fp */ + long i, + int siz, + FILE *fp +) { while (siz--) putc((int)(i>>(siz<<3) & 0xff), fp); } -putflt(f, fp) /* put out floating point number */ -double f; -FILE *fp; +void +putflt( /* put out floating point number */ + double f, + FILE *fp +) { - extern double frexp(); + long m; int e; - putint((long)(frexp(f,&e)*0x7fffffff), 4, fp); + m = frexp(f, &e) * 0x7fffffff; + if (e > 127) { /* overflow */ + m = m > 0 ? (long)0x7fffffff : -(long)0x7fffffff; + e = 127; + } else if (e < -128) { /* underflow */ + m = 0; + e = 0; + } + putint(m, 4, fp); putint((long)e, 1, fp); } char * -getstr(s, fp) /* get null-terminated string */ -char *s; -register FILE *fp; +getstr( /* get null-terminated string */ + char *s, + FILE *fp +) { - register char *cp; - register int c; + char *cp; + int c; cp = s; while ((c = getc(fp)) != EOF) @@ -61,12 +86,13 @@ register FILE *fp; long -getint(siz, fp) /* get a siz-byte integer */ -int siz; -register FILE *fp; +getint( /* get a siz-byte integer */ + int siz, + FILE *fp +) { - register int c; - register long r; + int c; + long r; if ((c = getc(fp)) == EOF) return(EOF); @@ -82,12 +108,20 @@ register FILE *fp; double -getflt(fp) /* get a floating point number */ -FILE *fp; +getflt( /* get a floating point number */ + FILE *fp +) { - extern double ldexp(); + long l; double d; - d = (double)getint(4, fp)/0x7fffffff; + l = getint(4, fp); + if (l == EOF && feof(fp)) /* EOF? */ + return((double)EOF); + if (l == 0) { + getc(fp); /* exactly zero -- ignore exponent */ + return(0.0); + } + d = (l + (l > 0 ? .5 : -.5)) * (1./0x7fffffff); return(ldexp(d, (int)getint(1, fp))); }