[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAFr9PXkkWw=M2VNODi5i4u3Qwd2Xy6HGweeD9X-K6aX_ejkRpg@mail.gmail.com>
Date: Mon, 5 Jan 2026 09:54:10 +0900
From: Daniel Palmer <daniel@...ngy.jp>
To: David Laight <david.laight.linux@...il.com>
Cc: w@....eu, linux@...ssschuh.net, linux-kernel@...r.kernel.org
Subject: Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h
Hi David,
On Mon, 5 Jan 2026 at 03:34, David Laight <david.laight.linux@...il.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.
_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?
Cheers,
Daniel
Powered by blists - more mailing lists