Skip to contentSkip to conversation

fopencookie.3: Fix EXAMPLES section

PATCH In review Alejandro Colomar 2 participants 5 messages Sep 7, 2020 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

cover fopencookie.3: Fix EXAMPLES section

*** BLURB HERE ***
Hi Michael,

I fixed a few bugs in the example program.

Testing the patch cover-letter now :)

Cheers,

Alex

Alejandro Colomar (2):
fopencookie.3: printf()'s .* expects an int; cast accordingly fopencookie.3: Fix bugs in example

 man3/fopencookie.3 | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/2 fopencookie.3: printf()'s .* expects an int; cast accordingly

---
 man3/fopencookie.3 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
man3/fopencookie.3 +1 -1
index 472a7f3d2..5394ce4a5 100644
@@ -438,7 +438,7 @@ main(int argc, char *argv[])
             break;
         }
 
-        printf("/%.*s/\en", nread, buf);
+        printf("/%.*s/\en", (int) nread, buf);
     }
 
     exit(EXIT_SUCCESS);
Signature · 1 line
2.28.0
#2 lore raw

Re: [PATCH 1/2] fopencookie.3: printf()'s .* expects an int; cast accordingly

Hello ALex,

Quoted 4 lines · Alejandro Colomar
---
 man3/fopencookie.3 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Thanks. Patch applied.

Cheers,

Michael

Quoted 14 lines
man3/fopencookie.3 +1 -1
index 472a7f3d2..5394ce4a5 100644
@@ -438,7 +438,7 @@ main(int argc, char *argv[])
             break;
         }
 
-        printf("/%.*s/\en", nread, buf);
+        printf("/%.*s/\en", (int) nread, buf);
     }
 
     exit(EXIT_SUCCESS);
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#3 lore raw

PATCH 2/2 fopencookie.3: Fix bugs in example

In reply to Alejandro Colomar

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.

---
 man3/fopencookie.3 | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
man3/fopencookie.3 +5 -5
index 5394ce4a5..055ec64d7 100644
@@ -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;
         }
Signature · 1 line
2.28.0
#4 lore raw

Re: [PATCH 2/2] fopencookie.3: Fix bugs in example

Hello Alex,

Quoted 4 lines · Alejandro Colomar

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

Quoted 35 lines
---
 man3/fopencookie.3 | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
man3/fopencookie.3 +5 -5
index 5394ce4a5..055ec64d7 100644
@@ -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;
         }
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/