hari
hari

Reputation: 9733

distinguish stdout from stderr on pipe

popen() alternative

My question is related to one posted above. In the first/accepted response, we are doing:

// Child. Let's redirect its standard output to our pipe and replace process with tail
close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
dup2(pipefd[1], STDERR_FILENO);

But what I want is to distinguish ERROR from regular OUTPUT. How can I do that? When I get anything in STDERR, I need to react to it.

It does not make much sense but, can I do following?:

int pipefd[3]     /* instead of 2 */

dup2(pipefd[1], STDOUT_FILENO);
dup2(pipefd[2], STDERR_FILENO);

I am using select to look at the fd and see if output is available. But till now, I just need to look at 1 fd, now I have to look at 2.

NOTE: A pipe can only have 2 ends, right? one to write to and other to read from. How can I accommodate this 3rd end :D ??

Upvotes: 2

Views: 299

Answers (1)

hmakholm left over Monica
hmakholm left over Monica

Reputation: 23332

You need to create two independent pipes and read from each of them separately. Shouldn't be hard since you already have a select() in place.

Upvotes: 3

Related Questions