From: Alejandro Colomar Date: Mon, 7 Sep 2020 10:21:17 +0000 (+0200) Subject: fopencookie.3: Fix bugs in example X-Git-Tag: man-pages-5.09~422 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a3eeca342dff002448aa7d9b0dc4f0d308c7fb4a;p=thirdparty%2Fman-pages.git fopencookie.3: Fix bugs in example 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 Signed-off-by: Michael Kerrisk --- diff --git a/man3/fopencookie.3 b/man3/fopencookie.3 index 5394ce4a58..055ec64d72 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; }