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

@ -417,8 +417,7 @@ static int read_field_headers(struct OperationConfig *config,
if(hdrlen) {
hdrbuf[hdrlen] = '\0';
if(slist_append(pheaders, hdrbuf)) {
fprintf(config->global->errors,
"Out of memory for field headers!\n");
fprintf(stderr, "Out of memory for field headers!\n");
return -1;
}
hdrlen = 0;
@ -428,8 +427,8 @@ static int read_field_headers(struct OperationConfig *config,
switch(c) {
case EOF:
if(ferror(fp)) {
fprintf(config->global->errors,
"Header file %s read error: %s\n", filename, strerror(errno));
fprintf(stderr, "Header file %s read error: %s\n", filename,
strerror(errno));
return -1;
}
return 0; /* Done. */
@ -585,7 +584,7 @@ static int get_param_part(struct OperationConfig *config, char endchar,
sep = *p;
*endpos = '\0';
if(slist_append(&headers, hdr)) {
fprintf(config->global->errors, "Out of memory for field header!\n");
fprintf(stderr, "Out of memory for field header!\n");
curl_slist_free_all(headers);
return -1;
}