This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH v2] test-skeleton: redirect stderr to stdout


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]