[PATCH v2] test-skeleton: redirect stderr to stdout
Pedro Alves
palves@redhat.com
Fri Jun 17 11:12:00 GMT 2016
On 06/17/2016 06:29 AM, Mike Frysinger wrote:
> + /* Bind stderr to stdout so that tests don't have to worry about which
> + one to use, and whether funcs they use (e.g. assert) will go to the
> + wrong place. */
> + fclose (stderr);
> + if (dup2 (STDOUT_FILENO, STDERR_FILENO) == -1)
> + {
> + printf ("binding stderr to stdout failed: %m\n");
> + exit (1);
> + }
> + stderr = fdopen (STDERR_FILENO, "w");
> + /* Since stderr always starts out unbuffered, recreate that here. */
> + setbuf (stderr, NULL);
Why do you need to fclose/fdopen stderr at all?
Just the "dup2 (STDOUT_FILENO, STDERR_FILENO)" should be enough.
Note that that's what
http://pubs.opengroup.org/onlinepubs/9699919799/functions/dup.html
suggests in its example section:
~~~~~~~~~~~~~~~~~~~~~
Redirecting Error Messages
The following example redirects messages from stderr to stdout.
#include <unistd.h>
...
dup2(1, 2);
...
~~~~~~~~~~~~~~~~~~~~~
Thanks,
Pedro Alves
More information about the Libc-alpha
mailing list