lua-users home
lua-l archive

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


Daniel Stephens wrote:
>
> On OSX my experience doesn't match your explanation, for two reasons...

Did you try the "lua -" + CTRL-D test with and without the line?

> 1) feof doesn't actually do anything that'd cause the EOF flag to get 
> set, it just checks the existing flag

Right, the feof flag is set somewhere deep within stdio.

> 2) While the 'condition' isn't permanent, the FLAG is, until you call 
> clearerr

Exactly.  And that's the point: it's a notification that stdio has
seen EOF on the FILE*.

> In this code, feof is completely unnecessary.

Only if you never call fread/fgetc/etc after you've seen an EOF.
But if you call one of these routines again with the expectation
that they will report EOF again you *may* lose.

> A more thorough implementation might be something along the lines of:
> 
> *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);
> if (*size > 0) return lf->buff;
> if (feof(lf->f)) {
>   /* Do something indicating EOF occurred */
>   return NULL;
> }

This feof-test is at the wrong place.  First, you may already
be at EOF (happens in Lua) but more important, the fread may
return > 0 but may have also set feof.  If you call fread again
and lf->f is a terminal it will wait for the next line or CTRL-D.
To circumvent this, the feof-test has to be before the fread.

Ciao, ET.