🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.bugs
Article #301

v7 C features/bugs vs. 4.2bsd putc

#301
From: Geoff Collyer
Date: Mon, 17 Sep 1984 11:42
31 lines
1890 bytes
I've been examining the definition of putc to see why it doesn't work
as advertised on 4.2bsd.  In particular, putc(0377) returns -1 rather
than the correct 0377.  This seems to be due to a conspiracy of
features/bugs in the v7 cc and all known stdio's:

(1) in (unsigned)*cp v7 cc treats cp as an (unsigned char *) [the
infamous uucp checksum `feature'],

(2) in the v7 cc, the value of (*cp++ = (unsigned)(c)) is erroneously
the right-hand side rather than the left-hand side as Kernighan &
Ritchie promises in section 7.14 of the reference manual (appendix A),

(3) putc() uses both bugs to advantage.

We noticed these bugs because we modified our v7 cat to check putc
returns but on moving it to 4.2 found that lint uses cat to concatenate
C compiler intermediate language and cat generates spurious Error 0
complaints upon seeing 0377 bytes.  The 4.2 cc doesn't have bug 2 nor
probably bug 1 and their absence broke putc.

Everyone should change putc in /usr/include/stdio.h from

#define putc(x,p) (--(p)->_cnt>=0? ((int)(*(p)->_ptr++=(unsigned)(x))):_flsbuf((unsigned)(x),p))

	to

#define putc(x,p) (--(p)->_cnt>=0? ((int)(*(p)->_ptr++=(x)))&0377: _flsbuf((x)&0377,p))

Yes, this probably does cost a microsecond or two, but it makes putc
more portable (the 0377 may have to be adjusted for machines with bytes
other than eight bits long).
Technical Details
Message-ID: <2079@utcsstat.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utcsstat!geoff

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.1 6/24/83; site utcsstat.UUCP
Path: arc1-ca.pugleaf.net!utzoo!utcsstat!geoff
From: geoff@utcsstat.UUCP (Geoff Collyer)
Newsgroups: net.bugs,net.lang.c,net.unix-wizards
Subject: v7 C features/bugs vs. 4.2bsd putc
Message-ID: <2079@utcsstat.UUCP>
Date: Mon, 17-Sep-84 07:42:26 EDT
Article-I.D.: utcsstat.2079
Posted: Mon Sep 17 07:42:26 1984
Date-Received: Mon, 17-Sep-84 09:00:07 EDT
Organization: U. of Toronto, Canada
Lines: 31