You want to read the error output from a program; for example, you want to capture the system calls displayed by strace(1).
Redirect standard error to standard output by adding 2>&1 to the command line passed to popen( ). Read standard output by opening the pipe in r mode. This is shown in Example 23-47.
Reading standard error
In both the Unix sh and the Windows cmd.exe shells, standard error is file descriptor 2, and standard output is file descriptor 1. Appending 2>&1 to a command tells the shell to redirect what's normally sent to file descriptor 2 (standard error) over to file descriptor 1 (standard output). fgets( ) then reads both standard error and standard output.
This technique reads in standard error but doesn't provide a way to distinguish it from standard output. To read just standard error, you need to prevent standard output from being returned through the pipe. This is done by redirecting it to /dev/null on Unix and NUL on Windows, as in Example 23-48.
Redirecting standard output
23.19.4. See Also
Documentation on popen( ) at http://www.php.net/popen; see your popen(3) manpage for details about the shell your system uses with popen( ); for information about shell redirection, see the Redirection section of the sh(1) manpage on Unix systems; on Windows, see the entry on redirection in the Command Reference section of your system help.