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

@ -48,7 +48,7 @@ int tool_debug_cb(CURL *handle, curl_infotype type,
{
struct OperationConfig *operation = userdata;
struct GlobalConfig *config = operation->global;
FILE *output = config->errors;
FILE *output = stderr;
const char *text;
struct timeval tv;
char timebuf[20];
@ -80,7 +80,7 @@ int tool_debug_cb(CURL *handle, curl_infotype type,
config->trace_stream = stdout;
else if(!strcmp("%", config->trace_dump))
/* Ok, this is somewhat hackish but we do it undocumented for now */
config->trace_stream = config->errors; /* aka stderr */
config->trace_stream = stderr;
else {
config->trace_stream = fopen(config->trace_dump, FOPEN_WRITETEXT);
config->trace_fopened = TRUE;