delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2004/12/11/17:32:30

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
X-Authentication-Warning: koeberg.lysator.liu.se: astrand owned process doing -bs
Date: Sat, 11 Dec 2004 23:32:07 +0100 (MET)
From: Peter Astrand <astrand AT lysator DOT liu DOT se>
To: cygwin AT cygwin DOT com, newlib AT sources DOT redhat DOT com
Subject: Re: short fread(), but no ferror/feof
In-Reply-To: <Pine.GSO.4.51L2.0412051701370.21199@koeberg.lysator.liu.se>
Message-ID: <Pine.GSO.4.51L2.0412112312480.19131@koeberg.lysator.liu.se>
References: <Pine DOT GSO DOT 4 DOT 51L2 DOT 0412051701370 DOT 21199 AT koeberg DOT lysator DOT liu DOT se>
MIME-Version: 1.0
X-Spam-Checker-Version: SpamAssassin 2.63-lysator_fetto_1.2 (2004-01-11) on fetto.lysator.liu.se
X-Spam-Level:
X-Spam-Status: No, hits=-2.4 required=5.0 tests=AWL autolearn=no version=2.63-lysator_fetto_1.2
X-MIME-Autoconverted: from QUOTED-PRINTABLE to 8bit by delorie.com id iBBMWRYM029566

Dave Korn wrote:

>  The fix and the problem Peter is seeing are orthogonal.
>
>  The fix referred to above fixes this problem:
>
>http://sources.redhat.com/ml/newlib/2004/msg00478.html
>"Hence, one can see that fread() in unbuffered mode always returns the
>specified count instead of the number of elements actually read."
>
>  That is why, as Peter has observed, fread() returns the number of
>elements
>actually read.  Without the patch, it would have been returning the entire
>number requested, which would have been even wronger.

Ah, thanks for clarifying this.


>  However Peter's problem is that when fread() does a partial read, it doesn't
>set either the EOF mark or the error indication on the file stream.  A strict
>reading of the fread() specification suggests that it should have set one of
>those.

IMO, there's no room for intepretation in this situation: As I stated
in my original post, IEEE Std 1003.1 is very clear:

"fread() shall return the number of elements successfully read which is
less than nitems only if a read error or end-of-file is encountered."


>OTOH, there isn't actually any error or EOF here.  It could possibly
>be argued from the specified behaviour of read() and pipes that the
>error mark should be set and that errno should be EAGAIN.  It may well
>be in practice that errno does indeed have the value EAGAIN but
>fread() can't return -1 because otherwise the application wouldn't
>know how much data it _had_ read.

I've discussed this issue with some local gurus, and the consensus is that
fread() should do as many read() calls as it takes. This is what glibc
does, for example (if I understand the source correctly). Basically,
fread() means "read everything". It's just plain wrong to return a partial
result, unless EOF or an error occured.


>  Of course, Peter can always detect when this situation has
>occurred, precisely because fread() returns a value that, while >= 0,
>is < the number of elements requested, and when feof() and ferror()
>both return zero, his code could deduce that it's a short read from a
>pipe and try again.

If you can convince the Python developers to add these checks to the
already-complicated fread() invocation in fileobject.c, I will stop
complaining... But fread() shouldn't behave like that.


(Please CC me on replies.)

/Peter Åstrand <astrand AT lysator DOT liu DOT se>


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


- Raw text -


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