Sitemap
1 min readJan 3, 2024

Thank you for your comment. I will delete the duplicated paragraph.

Regarding the illusion and the underlying logic, my understanding is based on the following C code: the dup2() function is used to duplicate the file descriptor from the first argument to the second. Consequently, the code duplicates the file descriptor of STDOUT to STDERR.

---

#include<stdio.h>

#include<stdlib.h>

#include<fcntl.h>

#include<unistd.h>

int main()

{

fwrite("For stdout\n", 11, 1, stdout);

fwrite("For stderr\n", 11, 1, stderr);

dup2(STDOUT_FILENO, STDERR_FILENO);

fwrite("To Stdout \n", 11, 1, stdout);

fwrite("To Stderr \n", 11, 1, stderr);

return 0;

}

---

As a result, all the output from STDERR is directed to the same output as STDOUT. I have examined one of the implementations of the fwrite function, and it directly operates on the FILE pointer buffer.

Do you have any references or code examples that I can study to learn more about stream merging? I would like to understand where the merging takes place.

Thank you.

fwrite: https://chromium.googlesource.com/chromiumos/third_party/glibc/+/cvs/libc-970720/stdio/fwrite.c

No responses yet