public inbox for linux-man@vger.kernel.org
 help / color / mirror / Atom feed
From: Arkadiusz Drabczyk <arkadiusz@drabczyk.org>
To: Walter Harms <wharms@bfs.de>
Cc: "mtk.manpages@gmail.com" <mtk.manpages@gmail.com>,
	"linux-man@vger.kernel.org" <linux-man@vger.kernel.org>
Subject: Re: [PATCH] fread.3: Add example
Date: Thu, 18 Jun 2020 15:36:47 +0200	[thread overview]
Message-ID: <20200618133647.rq2w6lanuatoweri@comp.lan> (raw)
In-Reply-To: <be5391cc853c41a683069fa0c42d0a88@bfs.de>

On Thu, Jun 18, 2020 at 08:49:53AM +0000, Walter Harms wrote:
> +    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.

> +    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.

> (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.

> 
> +    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.

-- 
Arkadiusz Drabczyk <arkadiusz@drabczyk.org>

  reply	other threads:[~2020-06-18 14:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-17 18:45 [PATCH] fread.3: Add example Arkadiusz Drabczyk
2020-06-18  8:49 ` AW: " Walter Harms
2020-06-18 13:36   ` Arkadiusz Drabczyk [this message]
2020-06-18 14:47     ` Walter Harms
2020-06-18 19:01       ` Arkadiusz Drabczyk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200618133647.rq2w6lanuatoweri@comp.lan \
    --to=arkadiusz@drabczyk.org \
    --cc=linux-man@vger.kernel.org \
    --cc=mtk.manpages@gmail.com \
    --cc=wharms@bfs.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox