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

@ -54,7 +54,7 @@ static void voutf(struct GlobalConfig *config,
ptr = print_buffer;
while(len > 0) {
fputs(prefix, config->errors);
fputs(prefix, stderr);
if(len > width) {
size_t cut = width-1;
@ -67,13 +67,13 @@ static void voutf(struct GlobalConfig *config,
max text width then! */
cut = width-1;
(void)fwrite(ptr, cut + 1, 1, config->errors);
fputs("\n", config->errors);
(void)fwrite(ptr, cut + 1, 1, stderr);
fputs("\n", stderr);
ptr += cut + 1; /* skip the space too */
len -= cut + 1;
}
else {
fputs(ptr, config->errors);
fputs(ptr, stderr);
len = 0;
}
}