www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1999/04/06/17:51:20

From: Alain Magloire <alainm AT rcsm DOT ece DOT mcgill DOT ca>
Message-Id: <199904062151.RAA16071@spock2.ECE.McGill.CA>
Subject: Re: v2.03 release: what else has to be done?
To: djgpp-workers AT delorie DOT com
Date: Tue, 6 Apr 1999 17:51:14 -0400 (EDT)
X-Mailer: ELM [version 2.4 PL25]
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com

: K. B. Williams: 
: On 04-06-99 at 12:43:12 EST you wrote:
: >
: > > But I'm unsure whether others have known bugs they'd like to fix in this
: > > release.  If so, and if you think the bugs are well-understood and their
: > > solution(s) are local enough, or tested well enough, to be safe, please
: > > speak up.
: >
: > I'd like to decide what to do about fflush(stdin) for this release.
: > The silent failures generate FAQs; I'd prefer less FAQs.
: >
: 
: I think that it should clear the contents of the input buffer.  That has
: long been the expected behavior, in my experience.  I've written dozens
: of programs based on that expectation.
: 

Since when this was a "long expected behavior" ?
Maybe on DOS but generally on Un*x fflush(stdin) is nonesense.
It's the same as doing i = i++; // undefined behavior.

1-
fflush(stdin), you're also asking fflush to be psychic, stdin is not
*always* a keyboard.

2-
fflush() is suppose to *save* the data, not destroy them. By asking
fflush() on an input stream, your implicitly asking the data to
be discarded not save. And How much should fflush() discard ?
BUFSIZ ? BUFSIZ -1 ? until '\n' ? this will probably depend on the type
of the stream isatty(), a file, a socket etc ..

3-
The C STD is very clear about this, fflush() is done on output stream.
fflush
int fflush(FILE *stream);  
	The function writes any buffered output to the file associated
	with the stream stream and returns zero if successful; otherwise,
	it returns EOF. If stream is a null pointer, fflush writes any
	buffered output to all files opened for output. 

Clearly output streams.

4-
My Posix 1003.1, 1996  p 214(8.2.3.4) lines 363-367 says:
fflush:
	The ffush() functions shall mark for update the st_ctime and st_mtime
	fields of the underlying file if the stream was writable and if
	buffered data had not been written to the file yet.
	The underlying functions are write() and lseek().

Although DJGPP doesn't have fork(), but when you fork() Posix says p213
lines 318-326.

	(e) If it is a stream that is open for writing or append (but not
	also open for reading), either an fflush() shal occur or the stream
	shall be close.
	(g) If the stream is open with a mode that allows reading and
	the underlying open file description refers to a device that
	is capable of seeking, either an fflush() shall occur or the
	stream shall be closed.
	(h) Otherwise, the reult is undefined.

(g) hints that the file pointer is readjust but there is no
destruction of data.

5-
My old iso C 89.
int fflush(FILE *stream);
	If stream points to  an  output  stream  or  an  update
	stream in which the most recent operation was not input, the
	fflush function causes any unwritten data for that stream to
	be  delivered  to  the host environment to be written to the
	file; otherwise, the behavior is undefined.
	If stream  is  a  null  pointer,  the  fflush  function
	performs  this  flushing action on all streams for which the
	behavior is defined above.
Returns
	The fflush  function  returns  EOF  if  a  write  error
	occurs, otherwise zero.

No mention of input streams.

6-
I can see the usefullness of fflush() on input stream in a thread
context, where you want to make sure that both thread has the same
view. Or maybe when the implementation struct/handle holds the file
pointer. So if you do a dup(), you want to fflush() to readjust
the file pointer of the input stream.


I don't even think this deserve a notice in a FAQ.
As someone pointed in comp.std.c/comp.lang.c once :
fflush(stdin);
i = i++;
void main(..);

Those are undefined behaviours.

IMO, it is ill-advise to allow fflush() on an input stream even
if at first it may look like a usefull extension.


-- 
au revoir, alain
----
Aussi haut que l'on soit assis, on est toujours assis que sur son cul !!!

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019