This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

EOF and ferror


Hi!

I ported an application that use file stream functions extensively (fopen,
fgetc, ferror, etc) from Windows/Linux to eCos. I guess that application is
working correctly on both Windows and Linux (I checked it only on Windows).
However, on eCos I have noticed a small problem regarding end of file
condition.

For example, a simple code like this should read bytes from the stream and
after all bytes are read (or error occurred) it should check for eventual
error condition with ferror().
On Windows, if there are no errors reading the file (end of file reached
normally), ferror() will return false. Unfortunately, on eCos ferror() will
always return true after getc() reaches EOF.



    FILE *infile;
    int c;

    if ((infile = fopen("abc.log","r")) == NULL)
    {
        printf("fopen failed.");
	  ...
    }

    while ((c = getc(infile)) != EOF)
    {
	//Do something with 'c'
	  ...
    }

    if ferror(infile)
    {
        printf("Error: %d", errno);
    }

    ...




In file stream.cxx, line 300 (method Cyg_StdioStream::refill_read_buffer)
there is:

    if (read_err == ENOERR) {
        if (len == 0) {
            read_err = EAGAIN;
            flags.at_eof = true;
        }
        else
            flags.at_eof = false;
    } // if

If I get it right, the (len == 0) indicates end of file and sets
corresponding flag (flags.at_eof) for eventual call to feof(), which is
fine.

However, setting 'read_err' to EAGAIN will cause the caller function of
refill_read_buffer() (for example: fgetc() ) to set the error in current
stream (Cyg_StdioStream::set_error() ). Therefore, future calls to ferror()
will indicate an error, although the EOF is no error condition (IMHO).

What was the reason for putting the line 'read_err = EAGAIN'?
Am I missing something obvious :-) (I must emphasize that I am not
acquainted with the standard)?

Thanks,
Jura



-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]