C++ Logo


Advanced search

Re: [std-proposals] A standard way to redirect all standard output

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 14 Mar 2023 13:10:14 +0000
On Mon, Mar 13, 2023 at 6:51 PM Thiago Macieira via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> Your investigation is wrong. On POSIX systems, writing to stdout will cause a
> write() to STDOUT_FILENO, which is 1. That's required.

I thought if I provided a definition for "write", then it would have
an effect for printf, puts, cout, cerr, and so on. But it doesn't.

It doesn't because even if you link dynamically with the Standard C
library on a Linux GNU compiler (i.e. glibc), you still get some
inline assembler in your executable file. That is to say, 'printf'
doesn't invoke 'write'.

Where you would expect to see something like the following assembler
for 'printf':
       mov rdi, 1
       mov rsi, "hello!!"
       mov rdx, 7
      call write_at_plt

You actually get an x86 instructions for a 'syscall':
       mov al, 1
       add di, 1
       mov rsi, "hello!!"
       push rsi
       mov rsi, rsp
       mov dl, 7

I think the "1" for stdout isn't hardcoded though -- it must be gotten
at runtime from something like "stdout->_fileno".

In my original post I listed three ways in which I was redirecting
output. All three are necessary -- I can't remove any of them.

Received on 2023-03-14 13:10:26