From 276c4a8e0b84627435675dadbff21c84a1da293a Mon Sep 17 00:00:00 2001 From: "Gregory J. Ward" Date: Fri, 19 Feb 2021 16:15:23 +0000 Subject: [PATCH] feat: added output stream error-checking to putstr(), putint(), and putflt() --- src/common/portio.c | 19 +++++++++++-------- src/common/rtio.h | 8 ++++---- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/common/portio.c b/src/common/portio.c index 0f73ccc5a..53d268cfc 100644 --- a/src/common/portio.c +++ b/src/common/portio.c @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: portio.c,v 2.24 2021/02/19 02:59:32 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.24 2021/02/19 02:59:32 greg Exp $ #include -void +int putstr( /* write null-terminated string to fp */ char *s, FILE *fp @@ -22,11 +22,13 @@ 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, @@ -40,11 +42,12 @@ putint( /* write a siz-byte integer to fp */ siz <<= 3; while ((siz -= 8) > 0) putc((int)(i>>siz & 0xff), fp); - putc((int)(i & 0xff), fp); + + return(putc((int)(i & 0xff), fp) == EOF ? EOF : 0); } -void +int putflt( /* put out floating point number */ double f, FILE *fp @@ -62,7 +65,7 @@ putflt( /* put out floating point number */ e = 0; } putint(m, 4, fp); - putint(e, 1, fp); + return(putint(e, 1, fp)); } @@ -118,7 +121,7 @@ getint( /* get a siz-byte integer */ return(EOF); r = c; if (c & 0x80) /* sign extend? */ - r |= ~255L; + r |= -256L; while (--siz > 0) { if ((c = getc(fp)) == EOF) return(EOF); diff --git a/src/common/rtio.h b/src/common/rtio.h index 56c4da6fa..34d256aa1 100644 --- a/src/common/rtio.h +++ b/src/common/rtio.h @@ -1,4 +1,4 @@ -/* RCSid $Id: rtio.h,v 3.24 2021/01/15 18:31:38 greg Exp $ */ +/* RCSid $Id: rtio.h,v 3.25 2021/02/19 16:15:23 greg Exp $ */ /* * Radiance i/o and string routines */ @@ -93,9 +93,9 @@ extern void swap16(char *wp, size_t n); extern void swap32(char *wp, size_t n); extern void swap64(char *wp, size_t n); /* defined in portio.c */ -extern void putstr(char *s, FILE *fp); -extern void putint(long i, int siz, FILE *fp); -extern void putflt(double f, FILE *fp); +extern int putstr(char *s, FILE *fp); +extern int putint(long i, int siz, FILE *fp); +extern int putflt(double f, FILE *fp); extern size_t putbinary(const void *s, size_t elsiz, size_t nel, FILE *fp); extern char *getstr(char *s, FILE *fp); extern long getint(int siz, FILE *fp);