From owner-freebsd-questions Sun Dec 15 03:18:08 1996 Return-Path: Received: (from root@localhost) by freefall.freebsd.org (8.8.4/8.8.4) id DAA19719 for questions-outgoing; Sun, 15 Dec 1996 03:18:08 -0800 (PST) Received: from jau.thunderbolt.fi (jukkonen.dial.tele.fi [194.89.253.78]) by freefall.freebsd.org (8.8.4/8.8.4) with ESMTP id DAA19685; Sun, 15 Dec 1996 03:17:59 -0800 (PST) Received: (from jau@localhost) by jau.thunderbolt.fi (8.7.5/8.6.12+CSC-2.1) id NAA24559; Sun, 15 Dec 1996 13:17:49 +0200 (EET) Message-Id: <199612151117.NAA24559@jau.thunderbolt.fi> Subject: funny unstable behaviour in the stdio library... To: hackers@freebsd.org, questions@freebsd.org Date: Sun, 15 Dec 1996 13:17:48 +0200 (EET) Reply-To: jau@iki.fi From: jau@iki.fi (Jukka Ukkonen) Latin-Date: Duminice XV Decembrie a.d. MCMXCVI Organization: Private person Phone: +358-9-6215280 (home) Reply-To: jau@iki.fi (Jukka Ukkonen) Content-Conversion: prohibited X-Mailer: ELM [version 2.4 PL25+pgp] MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Sender: owner-questions@freebsd.org X-Loop: FreeBSD.org Precedence: bulk Hi everybody! Has anyone noticed the following unexpected behaviour in the stdio library? When one reads the input using fgetc() and echoes the read character immediately using fputc() there is no problem. If one instead reads the input input using fgetc(stdin) and tries to output something using fprintf(stdout, ...) (or printf) there appears some weird extra garbage on the out (usually an 'a' in my case) just as if the printf machinery left some trash in the input for the fgetc (stdin) to find. If I fseek() the stdin even without trying any real movement within the stream as in fseek (stdin, 0, SEEK_CUR); before calling fgetc(stdin) the next time everything seems to be almost alright. But even then it only works correctly for one character at the time input, because fseek (stdin, ...) flushes the input stream after the one character that was just read and processed. You could try the following short piece of code to reproduce the problem. ------------------------------ clip clip ------------------------------ /* * stdio-echo-test */ #include #undef fputc int main (ac, av) int ac; char **av; { register char ch; for (ch = fgetc (stdin); ! feof (stdin); ch = fgetc (stdin)) { fprintf (stdout, "ch = %x %c\n", ch, ch); #ifdef SEEK_STDIN fseek (stdin, 0, SEEK_CUR); #endif } return (0); } ------------------------------ clip clip ------------------------------ When everything works as expected one would expect it produce something like this... input: XYZ output: ch = 58 X output: ch = 59 Y output: ch = 5a Z What I get without the additional call to fseek() is... input: XYZ output: ch = 58 X output: ch = 59 Y output: ch = 5a Z output: ch = a I have no idea whether this oddity is present already in the original BSD version of stdio library or has someone tried to fix or enhance something and broken the original on the way. I assume though that this is probably a problem in the original code because also Linux and DEC's OSF/1 both seem to behave exactly the same way. Anyhow one does not (And should not have to!!) expect the stdin change when calling one of the printf functions on the stdout stream, if the input stream does not change also when using the fputc() family of functions. The same weird behaviour seems to be present at least on FreeBSD versions from 2.1.0-RELEASE to 2.2-960501-SNAP. Does anyone have good ideas about why is this as it is, and what could be done about it if anything? Cheers, // jau ------ / Jukka A. Ukkonen, Telemedia / Finnish Telecom Ltd. /__ M.Sc. (sw-eng & cs) (Phone) +358-2040-4025 / Internet: Jukka.Ukkonen@tele.fi (Fax) +358-2040-2712 / Internet: jau@iki.fi (Mobile) +358-400-606671 v Internet: ukkonen@nic.funet.fi (Home&Fax) +358-9-6215280