[PATCH 2/4] find: remove redundant function error_severity and avoid magic numbers

Bernhard Voelker <[email protected]>
Newsgroups gmane.comp.gnu.findutils.bugs,gmane.comp.gnu.findutils.patches
Message-ID <[email protected]>
The struct state field exit_status is only set to 0 (EXIT_SUCCESS) or
to 1 (EXIT_FAILURE) throughout the code.
Therefore, remove the more complicated wrapper function error_severity
which was only used in very few cases anyway.
While at it, replace the above magic numbers by EXIT_OKAY/EXIT_SUCCESS.

* find/defs.h (error_severity): Remove.
* find/util.c (error_severity): Remove, and update all callers by replacing
it by the direct assignment to state.exit_status.
(get_statinfo): Use the above better names.
(report_file_err): Likewise.
* find/exec.c (launch): Likewise.
* find/ftsfind.c (consider_visiting): Likewise.
(find): Likewise.
(process_all_startpoints): Likewise.
(main): Likewise.
* find/pred.c (pred_delete): Likewise.
(pred_empty): Likewise.
(match_lname): Likewise.
(pred_xtype): Likewise.
* find/print.c (do_fprintf): Likewise.
---
 find/defs.h    |  1 -
 find/exec.c    |  6 +++---
 find/ftsfind.c | 14 +++++++-------
 find/pred.c    | 14 +++++++-------
 find/print.c   |  6 +++---
 find/util.c    | 17 ++---------------
 6 files changed, 22 insertions(+), 36 deletions(-)

diff --git a/find/defs.h b/find/defs.h
index 27b00a0e..1b02457d 100644
--- a/find/defs.h
+++ b/find/defs.h
@@ -517,7 +517,6 @@ void nonfatal_nontarget_file_error (int errno_value, const char *name);
 
 int process_leading_options (int argc, char *argv[]);
 void set_option_defaults (struct options *p);
-void error_severity (int level);
 
 # if 0
 #  define apply_predicate(pathname, stat_buf_ptr, node)	\
diff --git a/find/exec.c b/find/exec.c
index ab63d60e..7267c2de 100644
--- a/find/exec.c
+++ b/find/exec.c
@@ -347,7 +347,7 @@ launch (struct buildcmd_control *ctl, void *usercontext, int argc, char **argv)
 	{
 	  error (0, errno, _("error waiting for %s"),
 		 safely_quote_err_filename (0, argv[0]));
-	  state.exit_status = 1;
+	  state.exit_status = EXIT_FAILURE;
 	  return 0;		/* FAIL */
 	}
     }
@@ -364,7 +364,7 @@ launch (struct buildcmd_control *ctl, void *usercontext, int argc, char **argv)
 	   * -exec {} + returns true if the invoked command fails, but
 	   *            sets the program exit status.
 	   */
-	  state.exit_status = 1;
+	  state.exit_status = EXIT_FAILURE;
 	}
 
       return 1;			/* OK */
@@ -392,7 +392,7 @@ launch (struct buildcmd_control *ctl, void *usercontext, int argc, char **argv)
 	   * -exec {} + returns true if the invoked command fails, but
 	   *            sets the program exit status.
 	   */
-	  state.exit_status = 1;
+	  state.exit_status = EXIT_FAILURE;
 	}
       /* The child failed, but this is the exec callback.  We
        * don't want to run the child again in this case anwyay.
diff --git a/find/ftsfind.c b/find/ftsfind.c
index 75da0707..bd3b8ee8 100644
--- a/find/ftsfind.c
+++ b/find/ftsfind.c
@@ -319,7 +319,7 @@ consider_visiting (FTS *p, FTSENT *ent)
   else if (ent->fts_info == FTS_DC)
     {
       issue_loop_warning (ent);
-      error_severity (EXIT_FAILURE);
+      state.exit_status = EXIT_FAILURE;
       return;
     }
   else if (ent->fts_info == FTS_SLNONE)
@@ -508,7 +508,7 @@ find (char *arg)
     {
       error (0, errno, _("cannot search %s"),
 	     safely_quote_err_filename (0, arg));
-      error_severity (EXIT_FAILURE);
+      state.exit_status = EXIT_FAILURE;
     }
   else
     {
@@ -541,7 +541,7 @@ find (char *arg)
 	  error (0, errno,
 		 "failed to read file names from file system at or below %s",
 		 safely_quote_err_filename (0, arg));
-	  error_severity (EXIT_FAILURE);
+	  state.exit_status = EXIT_FAILURE;
 	  return false;
 	}
 
@@ -554,7 +554,7 @@ find (char *arg)
 	  error (0, errno,
 		 _("failed to restore working directory after searching %s"),
 		 arg);
-	  error_severity (EXIT_FAILURE);
+	  state.exit_status = EXIT_FAILURE;
 	  return false;
 	}
       p = NULL;
@@ -666,7 +666,7 @@ process_all_startpoints (int argc, char *argv[])
               goto argv_iter_done;
             case AI_ERR_READ:  /* may only happen with -files0-from  */
               error (0, errno, _("%s: read error"), files0_filename_quoted);
-              state.exit_status = 1;
+              state.exit_status = EXIT_FAILURE;
               ok = false;
               goto argv_iter_done;
             case AI_ERR_MEM:
@@ -694,7 +694,7 @@ process_all_startpoints (int argc, char *argv[])
               error (0, 0, "%s:%lu: %s", files0_filename_quoted, file_number,
                      _("invalid zero-length file name"));
             }
-          state.exit_status = 1;
+          state.exit_status = EXIT_FAILURE;
           ok = false;
           continue;
         }
@@ -742,7 +742,7 @@ main (int argc, char **argv)
   record_initial_cwd ();
 
   state.already_issued_stat_error_msg = false;
-  state.exit_status = 0;
+  state.exit_status = EXIT_SUCCESS;
   state.execdirs_outstanding = false;
   state.cwd_dir_fd = AT_FDCWD;
 
diff --git a/find/pred.c b/find/pred.c
index 07572608..bf995723 100644
--- a/find/pred.c
+++ b/find/pred.c
@@ -269,7 +269,7 @@ pred_delete (const char *pathname, struct stat *stat_buf, struct predicate *pred
        * seems somewhat arbitrary and confusing.  So, as of
        * findutils-4.3.11, we also set the exit status in this case.
        */
-      state.exit_status = 1;
+      state.exit_status = EXIT_FAILURE;
       return false;
     }
   else
@@ -300,14 +300,14 @@ pred_empty (const char *pathname, struct stat *stat_buf, struct predicate *pred_
 			| O_CLOEXEC | O_DIRECTORY | O_NOCTTY | O_NONBLOCK)) < 0)
 	{
 	  error (0, errno, "%s", safely_quote_err_filename (0, pathname));
-	  state.exit_status = 1;
+	  state.exit_status = EXIT_FAILURE;
 	  return false;
 	}
       d = fdopendir (fd);
       if (d == NULL)
 	{
 	  error (0, errno, "%s", safely_quote_err_filename (0, pathname));
-	  state.exit_status = 1;
+	  state.exit_status = EXIT_FAILURE;
 	  close (fd);
 	  return false;
 	}
@@ -328,14 +328,14 @@ pred_empty (const char *pathname, struct stat *stat_buf, struct predicate *pred_
 	{
 	  /* Handle errors from readdir(3). */
 	  error (0, errno, "%s", safely_quote_err_filename (0, pathname));
-	  state.exit_status = 1;
+	  state.exit_status = EXIT_FAILURE;
 	  CLOSEDIR (d);
 	  return false;
 	}
       if (CLOSEDIR (d))
 	{
 	  error (0, errno, "%s", safely_quote_err_filename (0, pathname));
-	  state.exit_status = 1;
+	  state.exit_status = EXIT_FAILURE;
 	  return false;
 	}
       return (empty);
@@ -574,7 +574,7 @@ match_lname (const char *pathname, struct stat *stat_buf, struct predicate *pred
       else
 	{
 	  nonfatal_target_file_error (errno, pathname);
-	  state.exit_status = 1;
+	  state.exit_status = EXIT_FAILURE;
 	}
       free (linkname);
     }
@@ -1150,7 +1150,7 @@ pred_xtype (const char *pathname, struct stat *stat_buf, struct predicate *pred_
       else
 	{
 	  error (0, errno, "%s", safely_quote_err_filename (0, pathname));
-	  state.exit_status = 1;
+	  state.exit_status = EXIT_FAILURE;
 	}
       return false;
     }
diff --git a/find/print.c b/find/print.c
index 716eb819..598f4a20 100644
--- a/find/print.c
+++ b/find/print.c
@@ -1029,7 +1029,7 @@ do_fprintf (struct format_val *dest,
                 if (linkname == NULL)
                   {
                     nonfatal_target_file_error (errno, pathname);
-                    state.exit_status = 1;
+                    state.exit_status = EXIT_FAILURE;
                   }
               }
             if (linkname)
@@ -1198,7 +1198,7 @@ do_fprintf (struct format_val *dest,
                         checked_fprintf (dest, segment->text, "?");
                         error (0, errno, "%s",
                                safely_quote_err_filename (0, pathname));
-                        /* exit_status = 1;
+                        /* exit_status = EXIT_FAILURE;
                            return ; */
                         break;
                       }
@@ -1237,7 +1237,7 @@ do_fprintf (struct format_val *dest,
 
                 error (0, errno, _("getfilecon failed: %s"),
                     safely_quote_err_filename (0, pathname));
-                state.exit_status = 1;
+                state.exit_status = EXIT_FAILURE;
               }
             else
               {
diff --git a/find/util.c b/find/util.c
index 63de5cce..4446f89b 100644
--- a/find/util.c
+++ b/find/util.c
@@ -254,7 +254,7 @@ get_statinfo (const char *pathname, const char *name, struct stat *p)
 	      /* Savannah bug #16378. */
 	      error (0, 0, _("WARNING: file %s appears to have mode 0000"),
 		     quotearg_n_style (0, options.err_quoting_style, name));
-	      error_severity (1);
+	      state.exit_status = EXIT_FAILURE;
 	    }
 	}
       else
@@ -1124,19 +1124,6 @@ safely_quote_err_filename (int n, char const *arg)
   return quotearg_n_style (n, options.err_quoting_style, arg);
 }
 
-/* We have encountered an error which should affect the exit status.
- * This is normally used to change the exit status from 0 to 1.
- * However, if the exit status is already 2 for example, we don't want to
- * reduce it to 1.
- */
-void
-error_severity (int level)
-{
-  if (state.exit_status < level)
-    state.exit_status = level;
-}
-
-
 /* report_file_err
  */
 static void
@@ -1150,7 +1137,7 @@ report_file_err(int exitval, int errno_value,
   if (!is_target_file || !state.already_issued_stat_error_msg)
     {
       error (exitval, errno_value, "%s", safely_quote_err_filename (0, name));
-      error_severity (1);
+      state.exit_status = EXIT_FAILURE;
     }
   if (is_target_file)
     {
-- 
2.33.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.