C++ Logo

std-proposals

Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Tue, 14 Mar 2023 11:45:41 -0700
On Tuesday, 14 March 2023 06:10:14 PDT Frederick Virchanza Gotham via Std-
Proposals wrote:
> 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.

We're going off-topic, so I will not reply to this thread again, unless it
comes back to a standards proposal or at a minimum something related to the
standards.

The standards answer is that that printf() or fprintf() do is implementation-
defined and that overriding standard library functions is nowhere in the
standards. Trying to do so is probably UB, which explains what you've seen.

On POSIX systems, stdout is always opened to STDOUT_FILENO, which must be 1.
It's required, see
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html

But how the stdio library accomplishes that writing is up to it. glibc calls
either __write or __write_nocancel, which are not symbols you are allowed to
override. See
https://codebrowser.dev/glibc/glibc/libio/fileops.c.html#_IO_new_file_write

If you don't like this, talk to your vendor.

But if you're on a POSIX system, you can use dup2() to a pipe or memfd of
yours.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-03-14 18:45:43