mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Thomas Weißschuh" <linux@weissschuh.net>
To: Daniel Palmer <daniel@thingy.jp>
Cc: w@1wt.eu, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h
Date: Sun, 4 Jan 2026 10:13:24 +0100	[thread overview]
Message-ID: <d5fb9eb6-ba65-4690-998a-33478fc4b2ca@t-8ch.de> (raw)
In-Reply-To: <20260104083837.1390041-2-daniel@thingy.jp>

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
> 

  reply	other threads:[~2026-01-04  9:13 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 [this message]
2026-01-04 18:34   ` David Laight
2026-01-05  0:54     ` Daniel Palmer
2026-01-05  9:27       ` David Laight
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=d5fb9eb6-ba65-4690-998a-33478fc4b2ca@t-8ch.de \
    --to=linux@weissschuh.net \
    --cc=daniel@thingy.jp \
    --cc=linux-kernel@vger.kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®