* [PATCH v2] fread.3: Add example
@ 2020-06-18 14:22 Arkadiusz Drabczyk
2020-06-19 12:39 ` Michael Kerrisk (man-pages)
0 siblings, 1 reply; 2+ messages in thread
From: Arkadiusz Drabczyk @ 2020-06-18 14:22 UTC (permalink / raw)
To: mtk.manpages; +Cc: linux-man
Signed-off-by: Arkadiusz Drabczyk <arkadiusz@drabczyk.org>
---
man3/fread.3 | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 55 insertions(+)
diff --git a/man3/fread.3 b/man3/fread.3
index 2dd7be9..4af3850 100644
--- a/man3/fread.3
+++ b/man3/fread.3
@@ -113,6 +113,61 @@ 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: 0x02
+.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, sizeof(buffer) / sizeof(*buffer), sizeof(*buffer),
+ fp);
+ if (ret != sizeof(*buffer)) {
+ fprintf(stderr, "fread() failed: %zu\en", ret);
+ exit(EXIT_FAILURE);
+ }
+
+ printf("ELF magic: %#04x%02x%02x%02x\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: %#04x\en", buffer[0]);
+
+ fclose(fp);
+
+ exit(EXIT_SUCCESS);
+}
+.EE
.SH SEE ALSO
.BR read (2),
.BR write (2),
--
2.9.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] fread.3: Add example
2020-06-18 14:22 [PATCH v2] fread.3: Add example Arkadiusz Drabczyk
@ 2020-06-19 12:39 ` Michael Kerrisk (man-pages)
0 siblings, 0 replies; 2+ messages in thread
From: Michael Kerrisk (man-pages) @ 2020-06-19 12:39 UTC (permalink / raw)
To: Arkadiusz Drabczyk; +Cc: mtk.manpages, linux-man
Hello Arkadiusz,
On 6/18/20 4:22 PM, Arkadiusz Drabczyk wrote:
> Signed-off-by: Arkadiusz Drabczyk <arkadiusz@drabczyk.org>
Thanks! Patch applied. One glitch below edited.
> ---
> man3/fread.3 | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 55 insertions(+)
>
> diff --git a/man3/fread.3 b/man3/fread.3
> index 2dd7be9..4af3850 100644
> --- a/man3/fread.3
> +++ b/man3/fread.3
> @@ -113,6 +113,61 @@ 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
I removed the preceding line (which did not render, and I assume was
cruft you meant to delete).
Thanks,
Michael
> +ELF magic: 0x7f454c46
> +Class: 0x02
> +.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, sizeof(buffer) / sizeof(*buffer), sizeof(*buffer),
> + fp);
> + if (ret != sizeof(*buffer)) {
> + fprintf(stderr, "fread() failed: %zu\en", ret);
> + exit(EXIT_FAILURE);
> + }
> +
> + printf("ELF magic: %#04x%02x%02x%02x\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: %#04x\en", buffer[0]);
> +
> + fclose(fp);
> +
> + exit(EXIT_SUCCESS);
> +}
> +.EE
> .SH SEE ALSO
> .BR read (2),
> .BR write (2),
>
--
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-06-19 12:39 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-18 14:22 [PATCH v2] fread.3: Add example Arkadiusz Drabczyk
2020-06-19 12:39 ` Michael Kerrisk (man-pages)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).