linux-man.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Walter Harms <wharms@bfs.de>
To: Arkadiusz Drabczyk <arkadiusz@drabczyk.org>
Cc: "linux-man@vger.kernel.org" <linux-man@vger.kernel.org>
Subject: AW: [PATCH] fread.3: return value
Date: Thu, 18 Jun 2020 14:32:02 +0000	[thread overview]
Message-ID: <726938c5daec4d5fbe7e85a121eff984@bfs.de> (raw)
In-Reply-To: <20200618114344.rsgecwlvpmesfa7a@comp.lan>


________________________________________
Von: linux-man-owner@vger.kernel.org [linux-man-owner@vger.kernel.org] im Auftrag von Arkadiusz Drabczyk [arkadiusz@drabczyk.org]
Gesendet: Donnerstag, 18. Juni 2020 13:43
An: Walter Harms
Cc: linux-man@vger.kernel.org
Betreff: Re: [PATCH] fread.3: Explain that file position is moved after calling fread()/fwrite()

On Thu, Jun 18, 2020 at 08:37:02AM +0000, Walter Harms wrote:
>> Isn't it already covered by this fragment:
>>
>> "If an error occurs, or the end of the file is reached, the return
>> value is a short item count (or zero)."
>>
>> IMHO no,
>> what does a "short item count" mean ?

>"items of data" refers to nmemb:

>"The function fread() reads nmemb items of data"

>so "item count" means number of size-long sequences of bytes and
>"short" means fewer than requested. But I think you know that well and
>you just think that "short item count" is not clear enough, right?

>(And of course I hope that I get it right.)

When reading that i try to "ignore" what i know and get the position of an first time reader,
so i noticed that this was fishy (i guess short item count is derived from short read) 

>> E.g. my size is 4 bytes and there are 3 bytes left to read. what is
>> the return value?

>If the requested size is 4 but there are only 3 bytes left then
>fread() will return 0, no matter what's the value of nmemb because
>it's not able to read even a single item and feof() will return
>non-zero. I think that this is explained clearly enough in the
>manpage.

IMHO i would be more clear to drop that "short item count". 
"If an error occurs, or the end of the file is reached, the  return value is zero"

>> and will the last few bytes ever read ?

>That is interesting. POSIX
>https://pubs.opengroup.org/onlinepubs/9699919799.2008edition/ says
>that "If a partial element is read, its value is unspecified.". But as
>fread() returns number of items it has successfully read the returned
>value should always be checked and it's different than requested nmemb
>it means an error and I guess there is no way to tell how many bytes
>have been partially read - for example if size == 4 and nmemb == 1 and
>returned value is 0 you don't know if fread() read 1, 2 or 3 bytes.

the GNU version fills the buffer with the remaining bytes but returns 0.


>Anyway, the patch that I sent explains that file pointer is moved
>after fread()/fwrite(). The discussion about return value should be
>started in a separate thread.

i have changed the subject.

--
Arkadiusz Drabczyk <arkadiusz@drabczyk.org>

  reply	other threads:[~2020-06-18 14:32 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-16 18:26 [PATCH] fread.3: Explain that file position is moved after calling fread()/fwrite() Arkadiusz Drabczyk
2020-06-16 19:00 ` Jakub Wilk
2020-06-16 22:50   ` Arkadiusz Drabczyk
2020-06-17  7:46     ` AW: " Walter Harms
2020-06-17 17:30       ` Arkadiusz Drabczyk
2020-06-18  8:37         ` AW: " Walter Harms
2020-06-18 11:43           ` Arkadiusz Drabczyk
2020-06-18 14:32             ` Walter Harms [this message]
2020-06-18 19:16               ` [PATCH] fread.3: return value Arkadiusz Drabczyk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=726938c5daec4d5fbe7e85a121eff984@bfs.de \
    --to=wharms@bfs.de \
    --cc=arkadiusz@drabczyk.org \
    --cc=linux-man@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).