This is the mail archive of the
ecos-discuss@sources.redhat.com
mailing list for the eCos project.
RE: Change output stream for printf
- From: "Nicolas Brouard" <nicolas dot brouard at silicomp dot ca>
- To: "Nick Garnett" <nickg at ecoscentric dot com>,"Andrew Lunn" <andrew at lunn dot ch>
- Cc: "eCos Discussion" <ecos-discuss at ecos dot sourceware dot org>
- Date: Mon, 15 Nov 2004 14:22:11 -0500
- Subject: RE: [ECOS] Change output stream for printf
Thank you very much.
The redirection (or pipe) mechanism of *NIX works !
This is my code:
// Create a socket
socket(...);
bind(...);
listen(...);
int socket_descriptor = accept(...);
// Duplicate socket_descriptor to stdout descriptor
int stdout_no = fileno(stdout);
close(stdout_no);
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