From: "Michael Kerrisk (man-pages)" <mtk.manpages@gmail.com>
To: Alejandro Colomar <colomar.6.4.3@gmail.com>
Cc: mtk.manpages@gmail.com, linux-man@vger.kernel.org
Subject: Re: [PATCH 2/2] fopencookie.3: Fix bugs in example
Date: Mon, 7 Sep 2020 12:34:36 +0200 [thread overview]
Message-ID: <933ccf29-8b43-5556-866d-e967f3b390cf@gmail.com> (raw)
In-Reply-To: <20200907102116.16924-3-colomar.6.4.3@gmail.com>
Hello Alex,
On 9/7/20 12:21 PM, Alejandro Colomar wrote:
> fread(3), unlike read(2) which returns a ssize_t, returns a size_t.
> It doesn't distinguish between error and enf-of-file.
> Instead, either ferror(3) or feof(3) need to be checked if
> fread() returned 0.
Well, gosh, the person who wrote that example really
should have read the documentation more closely :-}.
Thanks. Patch applied.
Cheers,
Michael
> Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com>
> ---
> man3/fopencookie.3 | 10 +++++-----
> 1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/man3/fopencookie.3 b/man3/fopencookie.3
> index 5394ce4a5..055ec64d7 100644
> --- a/man3/fopencookie.3
> +++ b/man3/fopencookie.3
> @@ -392,7 +392,7 @@ main(int argc, char *argv[])
> };
> FILE *stream;
> struct memfile_cookie mycookie;
> - ssize_t nread;
> + size_t nread;
> char buf[1000];
>
> /* Set up the cookie before calling fopencookie() */
> @@ -429,11 +429,11 @@ main(int argc, char *argv[])
> exit(EXIT_FAILURE);
> }
> nread = fread(buf, 1, 2, stream);
> - if (nread == \-1) {
> - perror("fread");
> - exit(EXIT_FAILURE);
> - }
> if (nread == 0) {
> + if (ferror(stream) != 0) {
> + fprintf(stderr, "fread failed\en");
> + exit(EXIT_FAILURE);
> + }
> printf("Reached end of file\en");
> break;
> }
>
--
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
prev parent reply other threads:[~2020-09-07 10:35 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-07 10:21 [PATCH 0/2] fopencookie.3: Fix EXAMPLES section Alejandro Colomar
2020-09-07 10:21 ` [PATCH 1/2] fopencookie.3: printf()'s .* expects an int; cast accordingly Alejandro Colomar
2020-09-07 10:31 ` Michael Kerrisk (man-pages)
2020-09-07 10:21 ` [PATCH 2/2] fopencookie.3: Fix bugs in example Alejandro Colomar
2020-09-07 10:34 ` Michael Kerrisk (man-pages) [this message]
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=933ccf29-8b43-5556-866d-e967f3b390cf@gmail.com \
--to=mtk.manpages@gmail.com \
--cc=colomar.6.4.3@gmail.com \
--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