[PATCH 1/2] tools/nolibc: Add fread() to stdio.h

Daniel Palmer posted 2 patches 1 month ago
There is a newer version of this series
[PATCH 1/2] tools/nolibc: Add fread() to stdio.h
Posted by Daniel Palmer 1 month ago
Add a very basic version of fread() like we already have for fwrite().

Signed-off-by: Daniel Palmer <daniel@thingy.jp>
---
 tools/include/nolibc/stdio.h | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 1f16dab2ac88..21569ebae824 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -170,7 +170,7 @@ int putchar(int c)
 }
 
 
-/* fwrite(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */
+/* fwrite(), fread(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */
 
 /* internal fwrite()-like function which only takes a size and returns 0 on
  * success or EOF on error. It automatically retries on short writes.
@@ -204,6 +204,39 @@ size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream)
 	return written;
 }
 
+/* internal fread()-like function which only takes a size and returns 0 on
+ * success or EOF on error. It automatically retries on short reads.
+ */
+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;
+		size -= ret;
+		buf += ret;
+	}
+	return 0;
+}
+
+
+static __attribute__((unused))
+size_t fread(void *s, size_t size, size_t nmemb, FILE *stream)
+{
+	size_t readed;
+
+	for (readed = 0; readed < nmemb; readed++) {
+		if (_fread(s, size, stream) != 0)
+			break;
+		s += size;
+	}
+	return readed;
+}
+
 static __attribute__((unused))
 int fputs(const char *s, FILE *stream)
 {
-- 
2.51.0
Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h
Posted by David Laight 1 month ago
On Sun,  4 Jan 2026 17:38:36 +0900
Daniel Palmer <daniel@thingy.jp> wrote:

> Add a very basic version of fread() like we already have for fwrite().
> 
> Signed-off-by: Daniel Palmer <daniel@thingy.jp>
> ---
>  tools/include/nolibc/stdio.h | 35 ++++++++++++++++++++++++++++++++++-
>  1 file changed, 34 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
> index 1f16dab2ac88..21569ebae824 100644
> --- a/tools/include/nolibc/stdio.h
> +++ b/tools/include/nolibc/stdio.h
> @@ -170,7 +170,7 @@ int putchar(int c)
>  }
>  
>  
> -/* fwrite(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */
> +/* fwrite(), fread(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */
>  
>  /* internal fwrite()-like function which only takes a size and returns 0 on
>   * success or EOF on error. It automatically retries on short writes.
> @@ -204,6 +204,39 @@ size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream)
>  	return written;
>  }
>  
> +/* internal fread()-like function which only takes a size and returns 0 on
> + * success or EOF on error. It automatically retries on short reads.
> + */
> +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.

> +		size -= ret;
> +		buf += ret;
> +	}
> +	return 0;
> +}
> +
> +
> +static __attribute__((unused))
> +size_t fread(void *s, size_t size, size_t nmemb, FILE *stream)
> +{
> +	size_t readed;

Isn't it enough to just multiply 'size' and 'nmemb' together
and then do a single long read() (maybe with retries for short reads).
The multiply can't overflow (for a valid request) because the buffer
size is also limited to 'size_t'.

OTOH Linux limits the maximum return from read() to INT_MAX - PAGE_SIZE
(even on 64bit).

	David 


> +
> +	for (readed = 0; readed < nmemb; readed++) {
> +		if (_fread(s, size, stream) != 0)
> +			break;
> +		s += size;
> +	}
> +	return readed;
> +}
> +
>  static __attribute__((unused))
>  int fputs(const char *s, FILE *stream)
>  {
Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h
Posted by Daniel Palmer 1 month ago
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.
_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
Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h
Posted by David Laight 1 month ago
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
Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h
Posted by Daniel Palmer 1 month ago
Hi David,

On Mon, 5 Jan 2026 at 18:27, David Laight <david.laight.linux@gmail.com> wrote:
> 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...)

I see now. If a partial read happens, the next call to fread() will
read from after the end of the partial read that happened and it'll be
broken.
Since in nolibc the FILE pointer that gets used isn't really a pointer
but the file descriptor I'm not sure where we'd stash the partial part
so we need to avoid doing the partial read entirely.

Thanks,

Daniel
Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h
Posted by David Laight 1 month ago
On Mon, 5 Jan 2026 18:43:03 +0900
Daniel Palmer <daniel@thingy.jp> wrote:

> Hi David,
> 
> On Mon, 5 Jan 2026 at 18:27, David Laight <david.laight.linux@gmail.com> wrote:
> > 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...)  
> 
> I see now. If a partial read happens, the next call to fread() will
> read from after the end of the partial read that happened and it'll be
> broken.
> Since in nolibc the FILE pointer that gets used isn't really a pointer
> but the file descriptor I'm not sure where we'd stash the partial part
> so we need to avoid doing the partial read entirely.

Except you can't really avoid the partial read.
Doing multiple read() system calls doesn't help.
The situation where it can happen probably doesn't happen for nolibc.
Is there support for ferror() and/or feof() ?
(a global 'u8 fstate[64]' indexed by fd number would probably suffice.)
If so you could set the 'error' bit and then error any further fread()s.

	David

> 
> Thanks,
> 
> Daniel
Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h
Posted by Thomas Weißschuh 1 month ago
On 2026-01-05 11:01:42+0000, David Laight wrote:
> On Mon, 5 Jan 2026 18:43:03 +0900
> Daniel Palmer <daniel@thingy.jp> wrote:
> > On Mon, 5 Jan 2026 at 18:27, David Laight <david.laight.linux@gmail.com> wrote:
> > > 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...)  
> > 
> > I see now. If a partial read happens, the next call to fread() will
> > read from after the end of the partial read that happened and it'll be
> > broken.
> > Since in nolibc the FILE pointer that gets used isn't really a pointer
> > but the file descriptor I'm not sure where we'd stash the partial part
> > so we need to avoid doing the partial read entirely.
> 
> Except you can't really avoid the partial read.
> Doing multiple read() system calls doesn't help.
> The situation where it can happen probably doesn't happen for nolibc.

> Is there support for ferror() and/or feof() ?
> (a global 'u8 fstate[64]' indexed by fd number would probably suffice.)
> If so you could set the 'error' bit and then error any further fread()s.

No, neither of them is currently supported.
Given that the support for 'FILE *' is obviously somewhat limited in
general and the existing fwrite() shares the same issues, I am leaning
towards picking up these patches. We can then find a proper solution.

Users trying to use these APIs really correctly will quite fast find out
that ferror() and feof() are missing.

The only real solution for partial writes that I see with the current
architecture is to limit the 'size' argument to '1'.
Maybe even with a static assertion. But there still won't be any way to
properly signal the issue to the caller. Not that ferror() could
distinguish the types of error in any case...


Thomas
Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h
Posted by Willy Tarreau 1 month ago
On Tue, Jan 06, 2026 at 12:02:18PM +0100, Thomas Weißschuh wrote:
> On 2026-01-05 11:01:42+0000, David Laight wrote:
> > On Mon, 5 Jan 2026 18:43:03 +0900
> > Daniel Palmer <daniel@thingy.jp> wrote:
> > > On Mon, 5 Jan 2026 at 18:27, David Laight <david.laight.linux@gmail.com> wrote:
> > > > 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...)  
> > > 
> > > I see now. If a partial read happens, the next call to fread() will
> > > read from after the end of the partial read that happened and it'll be
> > > broken.
> > > Since in nolibc the FILE pointer that gets used isn't really a pointer
> > > but the file descriptor I'm not sure where we'd stash the partial part
> > > so we need to avoid doing the partial read entirely.
> > 
> > Except you can't really avoid the partial read.
> > Doing multiple read() system calls doesn't help.
> > The situation where it can happen probably doesn't happen for nolibc.
> 
> > Is there support for ferror() and/or feof() ?
> > (a global 'u8 fstate[64]' indexed by fd number would probably suffice.)
> > If so you could set the 'error' bit and then error any further fread()s.
> 
> No, neither of them is currently supported.
> Given that the support for 'FILE *' is obviously somewhat limited in
> general and the existing fwrite() shares the same issues, I am leaning
> towards picking up these patches. We can then find a proper solution.
> 
> Users trying to use these APIs really correctly will quite fast find out
> that ferror() and feof() are missing.

I agree with you. The lib's called "nolibc" for a reason: it's not a
full-featured libc. Users have to accept some limitations.

> The only real solution for partial writes that I see with the current
> architecture is to limit the 'size' argument to '1'.

I thought about this as well but I don't know if it will be more
annoying than helpful...

> Maybe even with a static assertion. But there still won't be any way to
> properly signal the issue to the caller. Not that ferror() could
> distinguish the types of error in any case...

Let's see with the current patch how it goes. The first reported issues
(if any) will help us lean in the most desirable direction.

Willy
Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h
Posted by Thomas Weißschuh 1 month ago
On 2026-01-04 17:38:36+0900, Daniel Palmer wrote:
> Add a very basic version of fread() like we already have for fwrite().
> 
> Signed-off-by: Daniel Palmer <daniel@thingy.jp>
> ---
>  tools/include/nolibc/stdio.h | 35 ++++++++++++++++++++++++++++++++++-
>  1 file changed, 34 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
> index 1f16dab2ac88..21569ebae824 100644
> --- a/tools/include/nolibc/stdio.h
> +++ b/tools/include/nolibc/stdio.h
> @@ -170,7 +170,7 @@ int putchar(int c)
>  }
>  
>  
> -/* fwrite(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */
> +/* fwrite(), fread(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */
>  
>  /* internal fwrite()-like function which only takes a size and returns 0 on
>   * success or EOF on error. It automatically retries on short writes.
> @@ -204,6 +204,39 @@ size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream)
>  	return written;
>  }
>  
> +/* internal fread()-like function which only takes a size and returns 0 on
> + * success or EOF on error. It automatically retries on short reads.
> + */
> +static __attribute__((unused))
> +int _fread(void *buf, size_t size, FILE *stream)
> +{
> +	ssize_t ret;
> +	int fd = fileno(stream);

Switch these two lines around.

> +
> +	while (size) {
> +		ret = read(fd, buf, size);
> +		if (ret <= 0)
> +			return EOF;
> +		size -= ret;
> +		buf += ret;
> +	}
> +	return 0;
> +}
> +
> +
> +static __attribute__((unused))
> +size_t fread(void *s, size_t size, size_t nmemb, FILE *stream)
> +{
> +	size_t readed;

I really don't like this name. Maybe 'nread'?

> +
> +	for (readed = 0; readed < nmemb; readed++) {
> +		if (_fread(s, size, stream) != 0)
> +			break;
> +		s += size;
> +	}
> +	return readed;
> +}
> +
>  static __attribute__((unused))
>  int fputs(const char *s, FILE *stream)
>  {
> -- 
> 2.51.0
>