Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 15 Dec 1996 13:17:48 +0200 (EET)
From:      jau@iki.fi (Jukka Ukkonen)
To:        hackers@freebsd.org, questions@freebsd.org
Subject:   funny unstable behaviour in the stdio library...
Message-ID:  <199612151117.NAA24559@jau.thunderbolt.fi>

next in thread | raw e-mail | index | archive | help

	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 <stdio.h>

#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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199612151117.NAA24559>