This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH v2] test-skeleton: redirect stderr to stdout
- From: Pedro Alves <palves at redhat dot com>
- To: Mike Frysinger <vapier at gentoo dot org>, libc-alpha at sourceware dot org
- Date: Fri, 17 Jun 2016 12:12:09 +0100
- Subject: Re: [PATCH v2] test-skeleton: redirect stderr to stdout
- Authentication-results: sourceware.org; auth=none
- References: <1466052483-27705-1-git-send-email-vapier at gentoo dot org> <1466141361-4765-1-git-send-email-vapier at gentoo dot org>
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