From: David Laight <david.laight.linux@gmail.com>
To: Daniel Palmer <daniel@thingy.jp>
Cc: w@1wt.eu, linux@weissschuh.net, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h
Date: Mon, 5 Jan 2026 09:27:29 +0000 [thread overview]
Message-ID: <20260105092729.386bac33@pumpkin> (raw)
In-Reply-To: <CAFr9PXkkWw=M2VNODi5i4u3Qwd2Xy6HGweeD9X-K6aX_ejkRpg@mail.gmail.com>
On Mon, 5 Jan 2026 09:54:10 +0900
Daniel Palmer <daniel@thingy.jp> wrote:
> Hi David,
>
> On Mon, 5 Jan 2026 at 03:34, David Laight <david.laight.linux@gmail.com> wrote:
> > > +static __attribute__((unused))
> > > +int _fread(void *buf, size_t size, FILE *stream)
> > > +{
> > > + ssize_t ret;
> > > + int fd = fileno(stream);
> > > +
> > > + while (size) {
> > > + ret = read(fd, buf, size);
> > > + if (ret <= 0)
> > > + return EOF;
> >
> > You need to return a partial length if some data was read before EOF.
>
> According to the man page:
>
> On success, fread() and fwrite() return the number of items read
> or written. This number equals the number of bytes transferred
> only when size is 1. If an error occurs, or the end of the file
> is reached, the return value is a short item count (or zero).
>
> So I think the current logic is correct? If you have a file that has a
> length that is not a multiple of the item size and try to read more
> items than possible (i.e. you have 3 bytes in the file, you try to
> read 2 2 byte items) we read and count the items that are possible,
> the partial data is read but the read loop returns EOF, the partial
> item isn't counted and fread() returns the number of fully read items.
But you've deleted the partial bytes from the input.
I'm sure that isn't right.
Normally a FILE is buffered and the bytes are saved for the next read.
Remember you can be reading from a pipe that is being written using
'block buffering' - so it is valid for only a partial 'item' be read.
(I'm sure non-blocking IO is also valid...)
> _fread() could return the amount that was partially read but fread()
> is only checking for non-zero so there wouldn't be any difference.
>
> Maybe I'm missing something?
What you are doing is equivalent to a long read and then dividing
the number of bytes read by the item size.
So you might as well do a single read system call.
David
>
> Cheers,
>
> Daniel
next prev parent reply other threads:[~2026-01-05 9:27 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-04 8:38 [PATCH 0/2] nolibc: Add fread() and fseek() Daniel Palmer
2026-01-04 8:38 ` [PATCH 1/2] tools/nolibc: Add fread() to stdio.h Daniel Palmer
2026-01-04 9:13 ` Thomas Weißschuh
2026-01-04 18:34 ` David Laight
2026-01-05 0:54 ` Daniel Palmer
2026-01-05 9:27 ` David Laight [this message]
2026-01-05 9:43 ` Daniel Palmer
2026-01-05 11:01 ` David Laight
2026-01-06 11:02 ` Thomas Weißschuh
2026-01-06 11:07 ` Willy Tarreau
2026-01-04 8:38 ` [PATCH 2/2] tools/nolibc: Add fseek() " Daniel Palmer
2026-01-04 9:11 ` [PATCH 0/2] nolibc: Add fread() and fseek() Thomas Weißschuh
2026-01-04 11:12 ` Daniel Palmer
2026-01-04 12:42 ` Willy Tarreau
2026-01-04 14:36 ` Thomas Weißschuh
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=20260105092729.386bac33@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=daniel@thingy.jp \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@weissschuh.net \
--cc=w@1wt.eu \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.