public inbox for linux-man@vger.kernel.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <colomar.6.4.3@gmail.com>
To: mtk.manpages@gmail.com
Cc: linux-man@vger.kernel.org, Alejandro Colomar <colomar.6.4.3@gmail.com>
Subject: [PATCH 2/2] fopencookie.3: Fix bugs in example
Date: Mon,  7 Sep 2020 12:21:17 +0200	[thread overview]
Message-ID: <20200907102116.16924-3-colomar.6.4.3@gmail.com> (raw)
In-Reply-To: <20200907102116.16924-1-colomar.6.4.3@gmail.com>

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.

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;
         }
-- 
2.28.0


  parent reply	other threads:[~2020-09-07 10:22 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 ` Alejandro Colomar [this message]
2020-09-07 10:34   ` [PATCH 2/2] fopencookie.3: Fix bugs in example Michael Kerrisk (man-pages)

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=20200907102116.16924-3-colomar.6.4.3@gmail.com \
    --to=colomar.6.4.3@gmail.com \
    --cc=linux-man@vger.kernel.org \
    --cc=mtk.manpages@gmail.com \
    /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