Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!cbosgd!gatech!seismo!elsie!ado From: ado@elsie.UUCP (Arthur David Olson) Newsgroups: net.bugs.4bsd Subject: 4.[12]bsd putc's "return value" is inconsistent (with fix and caution) Message-ID: <6033@elsie.UUCP> Date: Tue, 25-Feb-86 19:59:25 EST Article-I.D.: elsie.6033 Posted: Tue Feb 25 19:59:25 1986 Date-Received: Fri, 28-Feb-86 06:33:20 EST Organization: NIH-LEC, Bethesda, MD Lines: 49 Keywords: bsd putc 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@seismo.ARPA DEC, VAX and Elsie are Digital Equipment and Borden trademarks