4.[12]bsd putc's "return value" is inconsistent (with fix and caution)
Arthur David Olson
ado at elsie.UUCP
Wed Feb 26 10:59:25 AEST 1986
Index: include/stdio.h
Description:
The putc macro may "return" different values when asked to output
the same value.
Repeat-By:
Compiling this code:
#include "stdio.h"
main()
{
int a, b;
a = putc(128 + '\n', stdout);
b = putc(128 + '\n', stdout);
printf("a: %d\n", a);
printf("b: %d\n", b);
}
then running the resulting executable, piping its output to "od -c"
and noting the results:
0000000 212 212 a : 1 3 8 \n b : - 1 1 8
0000020 \n \0
0000021
Fix:
Since the "fputc" function returns 138 in both cases, the thing to
do seems to be to change the "putc" macro to do so as well. Here's
the change for the 4.1bsd version of "stdio.h", conditioned on
"OLDVERSION":
#ifdef OLDVERSION
#define putc(x,p) (--(p)->_cnt>=0? ((int)(*(p)->_ptr++=(unsigned)(x))):_flsbuf((unsigned)(x),p))
#else
#define putc(x,p) (--(p)->_cnt>=0? ((int)((unsigned char)(*(p)->_ptr++=(x)))):_flsbuf((unsigned)(x),p))
#endif
Folks intersted in producing portable code will want to be aware of
the above bug and take pains to walk around it (for example, calling
ferror rather than checking putc's "return value" when all that's
being done is error checking).
--
Bugs is a Warner Brothers trademark.
--
UUCP: ..decvax!seismo!elsie!ado ARPA: elsie!ado at seismo.ARPA
DEC, VAX and Elsie are Digital Equipment and Borden trademarks
More information about the Comp.bugs.4bsd.ucb-fixes
mailing list