]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
tool: use errorf() for error output
authorDaniel Stenberg <daniel@haxx.se>
Wed, 31 May 2023 07:34:02 +0000 (09:34 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Thu, 1 Jun 2023 06:19:11 +0000 (08:19 +0200)
Convert a number of fprintf() calls.

src/tool_dirhie.c
src/tool_dirhie.h
src/tool_formparse.c
src/tool_getparam.c
src/tool_main.c
src/tool_msgs.c
src/tool_msgs.h
src/tool_operate.c
src/tool_stderr.c
src/tool_stderr.h

index 929d18e2a2d75a4ac1ae27a0866629f07ac6a554..c01c19cfb253863a742999c0ff37565f329c5c18 100644 (file)
@@ -34,6 +34,7 @@
 #include "curlx.h"
 
 #include "tool_dirhie.h"
+#include "tool_msgs.h"
 
 #include "memdebug.h" /* keep this as LAST include */
 
 #  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 */
       }
index de8311a6098e561c175b0683a904e880f060b81f..0ee407fe54f166591434b408df3faf7320c8de72 100644 (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 */
index 0dff4c985006b1cf32b4a12ebad3818747e154ac..ba9c073859ed300a9b1c4c77e07bcde9f8f7c8c1 100644 (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;
         }
index bcff83824c448642bcbbd768e8ad17d2f70e3d4b..c4c4be8bc6a94443c2764ec4424b0abd2d344ce7 100644 (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 */
index e65b087d801d1651eb1ed8e7606c018ad71d7f04..6b796b43bd6a8b88d537084caba1ca35d858205c 100644 (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;
   }
 
index 9a2281baf3f6e7accdc47650f54ff33af43db8de..9754870a39a2398fea22857c00675780854c3ddb 100644 (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);
index 19397f7f64ddbedaf4696c241e732d852a262117..9458991c01ac895c19157ae6726875f84d374ac6 100644 (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, ...);
index a5b53f83d3d173f2a184cbf806b7ce0ed1bb2057..20b679ea731ab01f7ce4986006349eba31c6eaf1 100644 (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;
index cd752508b84dbbf56069a44f17934f88baa454c9..23643df7cfe885883d8a8816123c3f17dca338cf 100644 (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);
index a3080259ef994fba5ce4744dabed2658ea449dae..c887275fc8e02315b2a9e29cf0e2d5429d998580 100644 (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 */