tool: improve --stderr handling

- freopen stderr with the user-specified file (--stderr file) instead of
  using a separate 'errors' stream.

- In tool_setup.h override stdio.h's stderr macro as global variable
  tool_stderr.

Both freopen and overriding the stderr macro are necessary because if
the user-specified filename is "-" then stdout is assigned to
tool_stderr and no freopen takes place. See the PR for more information.

Ref: https://github.com/curl/curl/issues/10491

Closes https://github.com/curl/curl/pull/10673
This commit is contained in:
Jay Satiro 2023-03-04 04:07:24 -05:00
parent 395b9175b7
commit 2f17a9b654
14 changed files with 151 additions and 60 deletions

View file

@ -53,6 +53,7 @@
#include "tool_vms.h"
#include "tool_main.h"
#include "tool_libinfo.h"
#include "tool_stderr.h"
/*
* This is low-level hard-hacking memory leak tracking and similar. Using
@ -156,7 +157,6 @@ static CURLcode main_init(struct GlobalConfig *config)
/* Initialise the global config */
config->showerror = FALSE; /* show errors when silent */
config->errors = stderr; /* Default errors to stderr */
config->styled_output = TRUE; /* enable detection */
config->parallel_max = PARALLEL_DEFAULT;
@ -196,10 +196,6 @@ static void free_globalconfig(struct GlobalConfig *config)
{
Curl_safefree(config->trace_dump);
if(config->errors_fopened && config->errors)
fclose(config->errors);
config->errors = NULL;
if(config->trace_fopened && config->trace_stream)
fclose(config->trace_stream);
config->trace_stream = NULL;
@ -250,6 +246,8 @@ int main(int argc, char *argv[])
struct GlobalConfig global;
memset(&global, 0, sizeof(global));
tool_init_stderr();
#ifdef WIN32
/* Undocumented diagnostic option to list the full paths of all loaded
modules. This is purposely pre-init. */