]> git.ipfire.org Git - thirdparty/man-pages.git/commitdiff
fopencookie.3: Fix bugs in example
authorAlejandro Colomar <colomar.6.4.3@gmail.com>
Mon, 7 Sep 2020 10:21:17 +0000 (12:21 +0200)
committerMichael Kerrisk <mtk.manpages@gmail.com>
Mon, 7 Sep 2020 10:32:03 +0000 (12:32 +0200)
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>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
man3/fopencookie.3

index 5394ce4a58a1dd600788c33b7ae2d8079e1e27e5..055ec64d7209a9e3971028b5e871adec2051c249 100644 (file)
@@ -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;
         }