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

@ -306,7 +306,7 @@ static CURLcode pre_transfer(struct GlobalConfig *global,
if((per->infd == -1) || fstat(per->infd, &fileinfo))
#endif
{
helpf(global->errors, "Can't open '%s'!\n", per->uploadfile);
helpf(stderr, "Can't open '%s'!\n", per->uploadfile);
if(per->infd != -1) {
close(per->infd);
per->infd = STDIN_FILENO;
@ -404,10 +404,10 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
if(!config->synthetic_error && result &&
(!global->silent || global->showerror)) {
const char *msg = per->errorbuffer;
fprintf(global->errors, "curl: (%d) %s\n", result,
fprintf(stderr, "curl: (%d) %s\n", result,
(msg && msg[0]) ? msg : curl_easy_strerror(result));
if(result == CURLE_PEER_FAILED_VERIFICATION)
fputs(CURL_CA_CERT_ERRORMSG, global->errors);
fputs(CURL_CA_CERT_ERRORMSG, stderr);
}
else if(config->failwithbody) {
/* if HTTP response >= 400, return error */
@ -415,7 +415,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
if(code >= 400) {
if(!global->silent || global->showerror)
fprintf(global->errors,
fprintf(stderr,
"curl: (%d) The requested URL returned error: %ld\n",
CURLE_HTTP_RETURNED_ERROR, code);
result = CURLE_HTTP_RETURNED_ERROR;
@ -448,7 +448,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
/* something went wrong in the writing process */
result = CURLE_WRITE_ERROR;
if(!global->silent || global->showerror)
fprintf(global->errors, "curl: (%d) Failed writing body\n", result);
fprintf(stderr, "curl: (%d) Failed writing body\n", result);
}
}
@ -589,8 +589,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
/* We have written data to an output file, we truncate file
*/
if(!global->silent)
fprintf(global->errors, "Throwing away %"
CURL_FORMAT_CURL_OFF_T " bytes\n",
fprintf(stderr, "Throwing away %" CURL_FORMAT_CURL_OFF_T " bytes\n",
outs->bytes);
fflush(outs->stream);
/* truncate file at the position where we started appending */
@ -599,8 +598,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
/* when truncate fails, we can't just append as then we'll
create something strange, bail out */
if(!global->silent || global->showerror)
fprintf(global->errors,
"curl: (23) Failed to truncate file\n");
fprintf(stderr, "curl: (23) Failed to truncate file\n");
return CURLE_WRITE_ERROR;
}
/* now seek to the end of the file, the position where we
@ -615,8 +613,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
#endif
if(rc) {
if(!global->silent || global->showerror)
fprintf(global->errors,
"curl: (23) Failed seeking to end of file\n");
fprintf(stderr, "curl: (23) Failed seeking to end of file\n");
return CURLE_WRITE_ERROR;
}
outs->bytes = 0; /* clear for next round */
@ -641,7 +638,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
/* something went wrong in the writing process */
result = CURLE_WRITE_ERROR;
if(!global->silent || global->showerror)
fprintf(global->errors, "curl: (%d) Failed writing body\n", result);
fprintf(stderr, "curl: (%d) Failed writing body\n", result);
}
if(result && config->rm_partial) {
notef(global, "Removing output file: %s\n", outs->filename);
@ -801,7 +798,7 @@ static CURLcode single_transfer(struct GlobalConfig *global,
/* Unless explicitly shut off */
result = glob_url(&inglob, infiles, &state->infilenum,
(!global->silent || global->showerror)?
global->errors:NULL);
stderr:NULL);
if(result)
break;
config->state.inglob = inglob;
@ -837,7 +834,7 @@ static CURLcode single_transfer(struct GlobalConfig *global,
expressions and return total number of URLs in pattern set */
result = glob_url(&state->urls, urlnode->url, &state->urlnum,
(!global->silent || global->showerror)?
global->errors:NULL);
stderr:NULL);
if(result)
break;
urlnum = state->urlnum;
@ -1096,7 +1093,7 @@ static CURLcode single_transfer(struct GlobalConfig *global,
file output call */
if(config->create_dirs) {
result = create_dir_hierarchy(per->outfile, global->errors);
result = create_dir_hierarchy(per->outfile, stderr);
/* create_dir_hierarchy shows error upon CURLE_WRITE_ERROR */
if(result)
break;
@ -1240,9 +1237,6 @@ static CURLcode single_transfer(struct GlobalConfig *global,
}
}
if(!global->errors)
global->errors = stderr;
if((!per->outfile || !strcmp(per->outfile, "-")) &&
!config->use_ascii) {
/* We get the output to stdout and we have not got the ASCII/text
@ -1851,7 +1845,7 @@ static CURLcode single_transfer(struct GlobalConfig *global,
my_setopt(curl, CURLOPT_TIMEVALUE_LARGE, config->condtime);
my_setopt_str(curl, CURLOPT_CUSTOMREQUEST, config->customrequest);
customrequest_helper(config, config->httpreq, config->customrequest);
my_setopt(curl, CURLOPT_STDERR, global->errors);
my_setopt(curl, CURLOPT_STDERR, stderr);
/* three new ones in libcurl 7.3: */
my_setopt_str(curl, CURLOPT_INTERFACE, config->iface);
@ -2517,8 +2511,7 @@ static CURLcode transfer_per_config(struct GlobalConfig *global,
/* Check we have a url */
if(!config->url_list || !config->url_list->url) {
helpf(global->errors, "(%d) no URL specified!\n",
CURLE_FAILED_INIT);
helpf(stderr, "(%d) no URL specified!\n", CURLE_FAILED_INIT);
return CURLE_FAILED_INIT;
}
@ -2576,7 +2569,7 @@ static CURLcode transfer_per_config(struct GlobalConfig *global,
if(!config->capath) {
curl_free(env);
curl_easy_cleanup(curltls);
helpf(global->errors, "out of memory\n");
helpf(stderr, "out of memory\n");
return CURLE_OUT_OF_MEMORY;
}
capath_from_env = true;
@ -2694,7 +2687,7 @@ CURLcode operate(struct GlobalConfig *global, int argc, argv_item_t argv[])
/* If we had no arguments then make sure a url was specified in .curlrc */
if((argc < 2) && (!global->first->url_list)) {
helpf(global->errors, NULL);
helpf(stderr, NULL);
result = CURLE_FAILED_INIT;
}
}