LOST in a PIPE

Jonathan I. Kamens jik at athena.mit.edu
Tue Feb 26 04:41:14 AEST 1991


  Well, you can't use system() or popen(), because neither of them allow you
to do what you want to to, i.e. read the stderr output of the tar process.  In
the former case, system(), it doesn't allow you to read any output at all.  In
the latter case, popen(), you can read stdout but not stderr, since stderr
automatically goes to whatever the stderr of your process is.

  Ah, but wait, there's a trick!  You can do this:

1. Create a pipe.

2. Fclose(stderr).

3. Fdopen() the write end of the pipe and assign the return value to stderr.

At this point, you've got a pipe connected to stderr, and you can find out
what goes into the pipe by reading from the read end of the pipe.   Then, you
can use popen() to start the tar process, and read from the FILE * returned by
the popen, or from the read end of the pipe, as appropriate.

  Since output will be going to both at the same time, you might want to use
select()  or poll() to watch both at once and read from one of them was there
is output available on it.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik at Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710



More information about the Comp.unix.programmer mailing list