From: Daniel Stenberg Date: Wed, 31 May 2023 07:34:02 +0000 (+0200) Subject: tool: use errorf() for error output X-Git-Tag: curl-8_2_0~171 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=741f7ed4bcecb304ff0b967beaabf5a8ee95e80b;p=thirdparty%2Fcurl.git tool: use errorf() for error output Convert a number of fprintf() calls. --- diff --git a/src/tool_dirhie.c b/src/tool_dirhie.c index 929d18e2a2..c01c19cfb2 100644 --- a/src/tool_dirhie.c +++ b/src/tool_dirhie.c @@ -34,6 +34,7 @@ #include "curlx.h" #include "tool_dirhie.h" +#include "tool_msgs.h" #include "memdebug.h" /* keep this as LAST include */ @@ -44,38 +45,38 @@ # endif #endif -static void show_dir_errno(FILE *errors, const char *name) +static void show_dir_errno(struct GlobalConfig *global, const char *name) { switch(errno) { #ifdef EACCES case EACCES: - fprintf(errors, "You don't have permission to create %s.\n", name); + errorf(global, "You don't have permission to create %s", name); break; #endif #ifdef ENAMETOOLONG case ENAMETOOLONG: - fprintf(errors, "The directory name %s is too long.\n", name); + errorf(global, "The directory name %s is too long", name); break; #endif #ifdef EROFS case EROFS: - fprintf(errors, "%s resides on a read-only file system.\n", name); + errorf(global, "%s resides on a read-only file system", name); break; #endif #ifdef ENOSPC case ENOSPC: - fprintf(errors, "No space left on the file system that will " - "contain the directory %s.\n", name); + errorf(global, "No space left on the file system that will " + "contain the directory %s", name); break; #endif #ifdef EDQUOT case EDQUOT: - fprintf(errors, "Cannot create directory %s because you " - "exceeded your quota.\n", name); + errorf(global, "Cannot create directory %s because you " + "exceeded your quota", name); break; #endif default: - fprintf(errors, "Error creating directory %s.\n", name); + errorf(global, "Error creating directory %s", name); break; } } @@ -95,7 +96,7 @@ static void show_dir_errno(FILE *errors, const char *name) #endif -CURLcode create_dir_hierarchy(const char *outfile, FILE *errors) +CURLcode create_dir_hierarchy(const char *outfile, struct GlobalConfig *global) { char *tempdir; char *tempdir2; @@ -151,7 +152,7 @@ CURLcode create_dir_hierarchy(const char *outfile, FILE *errors) /* Create directory. Ignore access denied error to allow traversal. */ if(!skip && (-1 == mkdir(dirbuildup, (mode_t)0000750)) && (errno != EACCES) && (errno != EEXIST)) { - show_dir_errno(errors, dirbuildup); + show_dir_errno(global, dirbuildup); result = CURLE_WRITE_ERROR; break; /* get out of loop */ } diff --git a/src/tool_dirhie.h b/src/tool_dirhie.h index de8311a609..0ee407fe54 100644 --- a/src/tool_dirhie.h +++ b/src/tool_dirhie.h @@ -24,7 +24,9 @@ * ***************************************************************************/ #include "tool_setup.h" +#include "tool_cfgable.h" -CURLcode create_dir_hierarchy(const char *outfile, FILE *errors); +CURLcode create_dir_hierarchy(const char *outfile, + struct GlobalConfig *global); #endif /* HEADER_CURL_TOOL_DIRHIE_H */ diff --git a/src/tool_formparse.c b/src/tool_formparse.c index 0dff4c9850..ba9c073859 100644 --- a/src/tool_formparse.c +++ b/src/tool_formparse.c @@ -417,7 +417,7 @@ static int read_field_headers(struct OperationConfig *config, if(hdrlen) { hdrbuf[hdrlen] = '\0'; if(slist_append(pheaders, hdrbuf)) { - fprintf(stderr, "Out of memory for field headers!\n"); + errorf(config->global, "Out of memory for field headers"); return -1; } hdrlen = 0; @@ -427,8 +427,8 @@ static int read_field_headers(struct OperationConfig *config, switch(c) { case EOF: if(ferror(fp)) { - fprintf(stderr, "Header file %s read error: %s\n", filename, - strerror(errno)); + errorf(config->global, "Header file %s read error: %s", filename, + strerror(errno)); return -1; } return 0; /* Done. */ @@ -584,7 +584,7 @@ static int get_param_part(struct OperationConfig *config, char endchar, sep = *p; *endpos = '\0'; if(slist_append(&headers, hdr)) { - fprintf(stderr, "Out of memory for field header!\n"); + errorf(config->global, "Out of memory for field header!"); curl_slist_free_all(headers); return -1; } diff --git a/src/tool_getparam.c b/src/tool_getparam.c index bcff83824c..c4c4be8bc6 100644 --- a/src/tool_getparam.c +++ b/src/tool_getparam.c @@ -1038,7 +1038,7 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */ break; case 'v': /* --stderr */ - tool_set_stderr_file(nextarg); + tool_set_stderr_file(global, nextarg); break; case 'w': /* --interface */ /* interface */ diff --git a/src/tool_main.c b/src/tool_main.c index e65b087d80..6b796b43bd 100644 --- a/src/tool_main.c +++ b/src/tool_main.c @@ -261,13 +261,13 @@ int main(int argc, char *argv[]) /* win32_init must be called before other init routines. */ result = win32_init(); if(result) { - fprintf(stderr, "curl: (%d) Windows-specific init failed.\n", result); + errorf(&global, "(%d) Windows-specific init failed", result); return result; } #endif if(main_checkfds()) { - fprintf(stderr, "curl: out of file descriptors\n"); + errorf(&global, "out of file descriptors"); return CURLE_FAILED_INIT; } diff --git a/src/tool_msgs.c b/src/tool_msgs.c index 9a2281baf3..9754870a39 100644 --- a/src/tool_msgs.c +++ b/src/tool_msgs.c @@ -136,7 +136,7 @@ void helpf(FILE *errors, const char *fmt, ...) */ void errorf(struct GlobalConfig *config, const char *fmt, ...) { - if(!config->silent) { + if(!config->silent || config->showerror) { va_list ap; va_start(ap, fmt); voutf(config, ERROR_PREFIX, fmt, ap); diff --git a/src/tool_msgs.h b/src/tool_msgs.h index 19397f7f64..9458991c01 100644 --- a/src/tool_msgs.h +++ b/src/tool_msgs.h @@ -24,6 +24,7 @@ * ***************************************************************************/ #include "tool_setup.h" +#include "tool_cfgable.h" void warnf(struct GlobalConfig *config, const char *fmt, ...); void notef(struct GlobalConfig *config, const char *fmt, ...); diff --git a/src/tool_operate.c b/src/tool_operate.c index a5b53f83d3..20b679ea73 100644 --- a/src/tool_operate.c +++ b/src/tool_operate.c @@ -384,6 +384,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global, struct OutStruct *outs = &per->outs; CURL *curl = per->curl; struct OperationConfig *config = per->config; + int rc; if(!curl || !config) return result; @@ -424,7 +425,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global, } /* Set file extended attributes */ if(!result && config->xattr && outs->fopened && outs->stream) { - int rc = fwrite_xattr(curl, per->this_url, fileno(outs->stream)); + rc = fwrite_xattr(curl, per->this_url, fileno(outs->stream)); if(rc) warnf(config->global, "Error setting extended attributes on '%s': %s", outs->filename, strerror(errno)); @@ -444,12 +445,11 @@ static CURLcode post_per_transfer(struct GlobalConfig *global, if(!outs->s_isreg && outs->stream) { /* Dump standard stream buffered data */ - int rc = fflush(outs->stream); + rc = fflush(outs->stream); if(!result && rc) { /* something went wrong in the writing process */ result = CURLE_WRITE_ERROR; - if(!global->silent || global->showerror) - fprintf(stderr, "curl: (%d) Failed writing body\n", result); + errorf(global, "Failed writing body"); } } @@ -586,7 +586,6 @@ static CURLcode post_per_transfer(struct GlobalConfig *global, per->retry_sleep = RETRY_SLEEP_MAX; } if(outs->bytes && outs->filename && outs->stream) { - int rc; /* We have written data to an output file, we truncate file */ notef(config->global, @@ -598,8 +597,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global, if(ftruncate(fileno(outs->stream), outs->init)) { /* when truncate fails, we can't just append as then we'll create something strange, bail out */ - if(!global->silent || global->showerror) - fprintf(stderr, "curl: (23) Failed to truncate file\n"); + errorf(config->global, "Failed to truncate file"); return CURLE_WRITE_ERROR; } /* now seek to the end of the file, the position where we @@ -613,8 +611,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global, rc = fseek(outs->stream, (long)outs->init, SEEK_SET); #endif if(rc) { - if(!global->silent || global->showerror) - fprintf(stderr, "curl: (23) Failed seeking to end of file\n"); + errorf(config->global, "Failed seeking to end of file"); return CURLE_WRITE_ERROR; } outs->bytes = 0; /* clear for next round */ @@ -634,12 +631,11 @@ noretry: /* Close the outs file */ if(outs->fopened && outs->stream) { - int rc = fclose(outs->stream); + rc = fclose(outs->stream); if(!result && rc) { /* something went wrong in the writing process */ result = CURLE_WRITE_ERROR; - if(!global->silent || global->showerror) - fprintf(stderr, "curl: (%d) Failed writing body\n", result); + errorf(config->global, "curl: (%d) Failed writing body", result); } if(result && config->rm_partial) { notef(global, "Removing output file: %s", outs->filename); @@ -1094,7 +1090,7 @@ static CURLcode single_transfer(struct GlobalConfig *global, file output call */ if(config->create_dirs) { - result = create_dir_hierarchy(per->outfile, stderr); + result = create_dir_hierarchy(per->outfile, global); /* create_dir_hierarchy shows error upon CURLE_WRITE_ERROR */ if(result) break; diff --git a/src/tool_stderr.c b/src/tool_stderr.c index cd752508b8..23643df7cf 100644 --- a/src/tool_stderr.c +++ b/src/tool_stderr.c @@ -26,8 +26,8 @@ #define CURL_DO_NOT_OVERRIDE_STDERR #include "tool_setup.h" - #include "tool_stderr.h" +#include "tool_msgs.h" #include "memdebug.h" /* keep this as LAST include */ @@ -39,7 +39,7 @@ void tool_init_stderr(void) tool_stderr = stderr; } -void tool_set_stderr_file(char *filename) +void tool_set_stderr_file(struct GlobalConfig *global, char *filename) { FILE *fp; @@ -55,7 +55,7 @@ void tool_set_stderr_file(char *filename) subsequent freopen will fail. */ fp = fopen(filename, FOPEN_WRITETEXT); if(!fp) { - fprintf(tool_stderr, "Warning: Failed to open %s!\n", filename); + warnf(global, "Warning: Failed to open %s", filename); return; } fclose(fp); diff --git a/src/tool_stderr.h b/src/tool_stderr.h index a3080259ef..c887275fc8 100644 --- a/src/tool_stderr.h +++ b/src/tool_stderr.h @@ -24,8 +24,9 @@ * ***************************************************************************/ #include "tool_setup.h" +#include "tool_cfgable.h" void tool_init_stderr(void); -void tool_set_stderr_file(char *filename); +void tool_set_stderr_file(struct GlobalConfig *global, char *filename); #endif /* HEADER_CURL_TOOL_STDERR_H */