Skip to contentSkip to conversation

fread.3: Add example

PATCH Superseded Arkadiusz Drabczyk 2 participants 5 messages Jun 17, 2020 – Jun 18, 2020 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fread.3: Add example

---
 man3/fread.3 | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
man3/fread.3 +53 -0
index 2dd7be9..c19a59c 100644
@@ -113,6 +113,59 @@ T}	Thread safety	MT-Safe
 .TE
 .SH CONFORMING TO
 POSIX.1-2001, POSIX.1-2008, C89.
+.SH EXAMPLES
+The program below demonstrates the use of
+.BR fread ()
+by parsing /bin/sh ELF executable in binary mode and printing its
+magic and class:
+.PP
+.in +4n
+.EX
+$ \fB./a.out\fP
+./a.out
+ELF magic: 0x7f454c46
+Class: 0x2
+.EE
+.in
+.SS Program source
+\&
+.EX
+#include <stdio.h>
+#include <stdlib.h>
+
+int
+main(void)
+{
+    FILE *fp = fopen("/bin/sh", "rb");
+    if (!fp) {
+        perror("fopen");
+        return EXIT_FAILURE;
+    }
+
+    unsigned char buffer[4];
+
+    size_t ret = fread(buffer, 4, 1, fp);
+    if (ret != 1) {
+        fprintf(stderr, "fread() failed: %zu\en", ret);
+        exit(EXIT_FAILURE);
+    }
+
+    printf("ELF magic: %#x%x%x%x\en", buffer[0], buffer[1], buffer[2],
+           buffer[3]);
+
+    ret = fread(buffer, 1, 1, fp);
+    if (ret != 1) {
+        fprintf(stderr, "fread() failed: %zu\en", ret);
+        exit(EXIT_FAILURE);
+    }
+
+    printf("Class: %#x\en", buffer[0]);
+
+    fclose(fp);
+
+    exit(EXIT_SUCCESS);
+}
+.EE
 .SH SEE ALSO
 .BR read (2),
 .BR write (2),
Signature · 1 line
2.9.0
#1 lore raw

few comments below,
hope that helps

re,
wh
________________________________________
Von: [email protected] [[email protected]] im Auftrag von Arkadiusz Drabczyk [[email protected]]
Gesendet: Mittwoch, 17. Juni 2020 20:45
An: [email protected]
Cc: [email protected]
Betreff: [PATCH] fread.3: Add example

---
 man3/fread.3 | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
man3/fread.3 +32 -0
index 2dd7be9..c19a59c 100644
@@ -113,6 +113,59 @@ T} Thread safety   MT-Safe
 .TE
 .SH CONFORMING TO
 POSIX.1-2001, POSIX.1-2008, C89.
+.SH EXAMPLES
+The program below demonstrates the use of
+.BR fread ()
+by parsing /bin/sh ELF executable in binary mode and printing its
+magic and class:
+.PP
+.in +4n
+.EX
+$ \fB./a.out\fP
+./a.out
+ELF magic: 0x7f454c46
+Class: 0x2
+.EE
+.in
+.SS Program source
+\&
+.EX
+#include <stdio.h>
+#include <stdlib.h>
+
+int
+main(void)
+{
+    FILE *fp = fopen("/bin/sh", "rb");
+    if (!fp) {
+        perror("fopen");
+        return EXIT_FAILURE;
+    }
+
+    unsigned char buffer[4];
+
+    size_t ret = fread(buffer, 4, 1, fp);

this is the core of the example and should be a bit more verbose:
maybe this is a bit to cryptic
ret = fread(buffer,sizeof (buffer)/sizeof (*buffer), sizeof (*buffer), fp);

alt:
size_of_buf= sizeof (buffer)/sizeof (*buffer);

ret = fread(buffer, // start of buffer
sizeof (buffer)/sizeof (*buffer), // size of buffer in bytes
sizeof (*buffer), // size of element
fp); // file pointer

+ if (ret != 1) {
+ fprintf(stderr, "fread() failed: %zu\en", ret);
+ exit(EXIT_FAILURE);
+ }
+
+ printf("ELF magic: %#x%x%x%x\en", buffer[0], buffer[1], buffer[2],
+ buffer[3]);
+
this works for /bin/sh but if the user plays round this may show confusing results so you could simply do:

    printf("ELF magic: %#02x%02x%02x%02x\en", 

so you are consistent in all cased. (until some will test this on a big endian, no idea what will happen then).

+ ret = fread(buffer, 1, 1, fp);
+ if (ret != 1) {
+ fprintf(stderr, "fread() failed: %zu\en", ret);
+ exit(EXIT_FAILURE);
+ }
+
please drop a line what case you want to explain here, looks like the same as above.

+ printf("Class: %#x\en", buffer[0]);
+
+ fclose(fp);
+
+ exit(EXIT_SUCCESS);
+}
+.EE
.SH SEE ALSO
.BR read (2),
.BR write (2),
--
2.9.0

#2 lore raw
Quoted 4 lines · Walter Harms

+ size_t ret = fread(buffer, 4, 1, fp);
this is the core of the example and should be a bit more verbose:
maybe this is a bit to cryptic
ret = fread(buffer,sizeof (buffer)/sizeof (*buffer), sizeof (*buffer), fp);

ok, I'll use this version in v2.

Quoted 7 lines
(quoted diff) +3 -0
+    printf("ELF magic: %#x%x%x%x\en", buffer[0], buffer[1], buffer[2],
+           buffer[3]);
+

this works for /bin/sh but if the user plays round this may show confusing results so you could simply do:

    printf("ELF magic: %#02x%02x%02x%02x\en",

so you are consistent in all cased.

ok, I'll do it in v2.

Quoted 2 lines

(until some will test this on a big endian, no idea what will happen then).

Nothing, in ELF spec it says that "the initial bytes of the file specify how to interpret the file, independent of the processor on which the inquiry is made and independent of the file's remaining contents.". And of course I tested this example on my router which has MIPS CPU which is big-endian and runs big-edian ELFs:

$ file sh
sh: ELF 32-bit MSB executable, MIPS, MIPS32 rel2 version 1, dynamically linked, interpreter /lib/ld-uClibc.so.0, corrupted section header size

and output of the example program is the same as on x86.

Quoted 8 lines
(quoted diff) +6 -0
+    ret = fread(buffer, 1, 1, fp);
+    if (ret != 1) {
+        fprintf(stderr, "fread() failed: %zu\en", ret);
+        exit(EXIT_FAILURE);
+    }
+

please drop a line what case you want to explain here, looks like the same as above.

As said a few lines above, this retrieves ELF class. I wanted to show that file pointer moves automatically after fread() finishes.

Signature · 1 line
Arkadiusz Drabczyk <[email protected]>
#3 lore raw

________________________________________
Von: Arkadiusz Drabczyk [[email protected]]
Gesendet: Donnerstag, 18. Juni 2020 15:36

Quoted 11 lines
Quoted 8 lines
(quoted diff) +6 -0
+    ret = fread(buffer, 1, 1, fp);
+    if (ret != 1) {
+        fprintf(stderr, "fread() failed: %zu\en", ret);
+        exit(EXIT_FAILURE);
+    }
+

please drop a line what case you want to explain here, looks like the same as above.

As said a few lines above, this retrieves ELF class. I wanted to show that file pointer moves automatically after fread() finishes.

ok i get the point(s).
1. you show you can read an "item" a bunch of bytes

the second read() should demonstrate that consecutive reads return
consecutive blocks of data (here the magic and class id of an ELF).

perhaps you can support what want to show with a
printf("pos=%ld\n",ftell(fp));
before and after read.

hope that helps,
re,
wh

--
Arkadiusz Drabczyk <[email protected]>

#4 lore raw
Quoted 14 lines · Walter Harms

________________________________________
Von: Arkadiusz Drabczyk [[email protected]]
Gesendet: Donnerstag, 18. Juni 2020 15:36

Quoted 8 lines
Quoted 8 lines
(quoted diff) +6 -0
+    ret = fread(buffer, 1, 1, fp);
+    if (ret != 1) {
+        fprintf(stderr, "fread() failed: %zu\en", ret);
+        exit(EXIT_FAILURE);
+    }
+

please drop a line what case you want to explain here, looks like the same as above.

Quoted 8 lines
Quoted 2 lines

As said a few lines above, this retrieves ELF class. I wanted to show that file pointer moves automatically after fread() finishes.

ok i get the point(s).
1. you show you can read an "item" a bunch of bytes

the second read() should demonstrate that consecutive reads return
consecutive blocks of data (here the magic and class id of an ELF).

Exactly.

Quoted 3 lines

perhaps you can support what want to show with a
printf("pos=%ld\n",ftell(fp));
before and after read.

Let's what Michael says. If he thinks this suggestion and the entire example in the first place is a good idea I will add it.

Signature · 1 line
Arkadiusz Drabczyk <[email protected]>