[ECOS] Change output stream for printf

Nicolas Brouard nicolas.brouard@silicomp.ca
Mon Nov 15 21:49:00 GMT 2004

Thank you very much.
The redirection (or pipe) mechanism of *NIX works !
This is my code:
// Create a socket
int socket_descriptor = accept(...);

// Duplicate socket_descriptor to stdout descriptor
int stdout_no = fileno(stdout);
dup2(socket_descriptor, stdout_no); // dup(socket_descriptor) works as well

printf("This is printed over the socket\n");
diag_printf("This is printed over serial port\n");

Nicolas Brouard

-----Original Message-----
From: Nick Garnett [mailto:nickg@ecoscentric.com]
Sent: Monday, November 15, 2004 1:51 PM
To: Andrew Lunn
Cc: Nicolas Brouard; eCos Discussion
Subject: Re: [ECOS] Change output stream for printf

Andrew Lunn <andrew@lunn.ch> writes:

> > Another approach would be to use fileno(stdout) to get the file
> > descriptor underneath stdout (it should be 1). close() it and then
> > dup2() the socket into that descriptor. This is how it is done in *NIX
> > and it should work in eCos. You may need to do a bit of business to
> > flush any data still in the stream buffers.
> I seemed to remember something about this not working, which i why i
> did not suggest it. Now you have mentioned it, i went googling. Its
> probably worth reading the thread:
> http://sources.redhat.com/ml/ecos-discuss/2003-04/msg00135.html

Yes, I recall that not working as expected. Which is why my approach
manipulates the file descriptors underneath the C library, rather than
mess with the C library itself. If this is done early on in the
program, before any output is generated, it is logically equivalent to
redirecting or pipelining a *NIX application in the shell.

Nick Garnett                    eCos Kernel Architect
http://www.ecoscentric.com/     The eCos and RedBoot experts

Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

More information about the Ecos-discuss mailing list