lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 26 Mar 2008, at 20:51, Edgar Toernig wrote:
Luiz Henrique de Figueiredo wrote:

/* EOF flag checked here because when lf->f is a terminal, the fread
   may return > 0 even when it is set.  If you call fread again and
   lf->f is a terminal it will wait for the next line or CTRL-D. */

Actually I think the exact problem is a little different: fread can
return > 0  *and* set the EOF flag. The next time getF is called, if
you call fread, then the terminal will wait for user input. By calling
feof before fread, you avoid this wait.

How about

	/*
	 * Trying to read past EOF or after an error exposes implemen-
	 * tation and device (file/tty) differences.  Better not ...
	 */
	if (feof(lf->f) || ferror(lf->f))
		return NULL;

Perhaps with an additional note that this is only necessary for implementations of fread that violate the C standard:

ISO 9899:1999 Section 7.19.7.1 (fgetc):

"If the end-of-file indicator for the stream is set, or if the stream is at end-of-file, the end- of-file indicator for the stream is set and the fgetc function returns EOF"

(and fread is documented as working as if it calls fgetc many times).

This violation (in general that successive reads from a terminal only return EOF once) is a traditional Unix behaviour and there may be many, older, Unix programs that rely on it. Which is perhaps why Linux implements it like it does.

drj