tool: use errorf() for error output

Convert a number of fprintf() calls.
This commit is contained in:
Daniel Stenberg 2023-05-31 09:34:02 +02:00
parent 6d45b9ca9c
commit 741f7ed4bc
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
10 changed files with 38 additions and 37 deletions

View File

@ -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 */
}

View File

@ -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 */

View File

@ -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;
}

View File

@ -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 */

View File

@ -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;
}

View File

@ -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);

View File

@ -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, ...);

View File

@ -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;

View File

@ -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);

View File

@ -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 */