0

I need to get the stderr stream and the stdout stream for a command, but I only know how to redirect the command's stdout stream only.

Suppose I have a function like this:

int getcmd(cmd, buf, max, len)
    const char *buf;
    char *buf;
    size_t max;
    size_t *len;
{
if (!cmd || !buf)
    return 1;
FILE *stream;
if ((stream = popen(cmd, "r")) == NULL)
    return 1;

size_t length;
while (fgets(buf, max, stream) == buf) {
    length = strlen(buf);
    buf = &buf[length];
    if (len)
        *len += length;
    max -= length;
}
if (*(buf - sizeof(char)) == '\n')
    *(buf - sizeof(char)) = 0;
return pclose(stream);
}

This function can read the command's stdout without any tricks and store it in a variable.
How can I do the same with stderr ?

Amr Ayman
  • 1,129
  • 1
  • 8
  • 24
  • possible duplicate of [how to control popen stdin, stdout, stderr redirection?](http://stackoverflow.com/questions/280571/how-to-control-popen-stdin-stdout-stderr-redirection) – Captain Giraffe Nov 13 '14 at 16:52
  • @CaptainGiraffe: Can you please give an illustration ? – Amr Ayman Nov 13 '14 at 17:34
  • There is a fairly comprehensive example linked in the top answer. http://jineshkj.wordpress.com/2006/12/22/how-to-capture-stdin-stdout-and-stderr-of-child-program/ – Captain Giraffe Nov 13 '14 at 17:45

0 Answers0