command 7>&1 >&2 2>&7
Hope this helps,
Brainbench MVP for Unix Programming
In a particular process which forks 1 child, I would like to have the
parent log to stdout and the child to stderr both using functions which
typically write to stdout (like puts(), and printf()).
Then when I launch the program, I can use redirection to specify the files
to which I log, rather than adding parameters to the programs. Also, then,
if I run it interactively, both processes' output goes to my terminal.
Here's what I've done:
#define STDOUT 1
#define STDERR 2
if ((saved_fp = dup(STDERR)) == -1) exit(1);
if (dup(STDOUT) != STDERR) exit(1);
if (dup(saved) != STDOUT) exit(1);
and... it works. Afterward, puts() goes to stderr, and fprintf(stderr,) goes
to stdout (from the shell's perspective anyway).
However, I'm wondering if it is legal to do this, ie. rely
on dup() to return the lowest fd after closing stdout, for instance. (I assumed
stdin fd was unavailable.) Is it OK to close stdout and stderr in this way?
Please email. Thanks,