]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
du: improve diagnostics for --time-style
authorPádraig Brady <P@draigBrady.com>
Mon, 28 Jul 2025 22:05:49 +0000 (23:05 +0100)
committerPádraig Brady <P@draigBrady.com>
Tue, 29 Jul 2025 10:43:09 +0000 (11:43 +0100)
* src/system.h (x_timestyle_args): A new function refactored from ...
* src/ls.c (decode_switches): ... here.
* src/du.c: Use refactored x_timestyle_args() to output a custom error.
Addresses https://bugs.gnu.org/79113

src/du.c
src/ls.c
src/system.h

index bac372a042943792f9de59baf97c6c3e1ae61933..c93d7f71b4bbbef10ff64d9296127d9a8ae7cb6c 100644 (file)
--- a/src/du.c
+++ b/src/du.c
@@ -26,8 +26,8 @@
 #include <config.h>
 #include <getopt.h>
 #include <sys/types.h>
-#include "system.h"
 #include "argmatch.h"
+#include "system.h"
 #include "argv-iter.h"
 #include "assure.h"
 #include "di-set.h"
@@ -974,8 +974,10 @@ main (int argc, char **argv)
         time_format = time_style + 1;
       else
         {
-          switch (XARGMATCH ("time style", time_style,
-                             time_style_args, time_style_types))
+          switch (x_timestyle_match (time_style, /*allow_posix=*/ false,
+                                     time_style_args,
+                                     (char const *) time_style_types,
+                                     sizeof (*time_style_types), EXIT_FAILURE))
             {
             case full_iso_time_style:
               time_format = "%Y-%m-%d %H:%M:%S.%N %z";
index e84e0facff9a46344452ccd2cf80a04613afd11b..d9faddee41cff27c2db712a6a30dd52b592eb5af 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
 # define SA_RESTART 0
 #endif
 
-#include "system.h"
 #include <fnmatch.h>
 
 #include "acl.h"
 #include "argmatch.h"
+#include "system.h"
 #include "assure.h"
 #include "c-strcase.h"
 #include "dev-ino.h"
@@ -2464,30 +2464,10 @@ decode_switches (int argc, char **argv)
         }
       else
         {
-          ptrdiff_t res = argmatch (style, time_style_args,
-                                    (char const *) time_style_types,
-                                    sizeof (*time_style_types));
-          if (res < 0)
-            {
-              /* This whole block used to be a simple use of XARGMATCH.
-                 but that didn't print the "posix-"-prefixed variants or
-                 the "+"-prefixed format string option upon failure.  */
-              argmatch_invalid ("time style", style, res);
-
-              /* The following is a manual expansion of argmatch_valid,
-                 but with the added "+ ..." description and the [posix-]
-                 prefixes prepended.  Note that this simplification works
-                 only because all four existing time_style_types values
-                 are distinct.  */
-              fputs (_("Valid arguments are:\n"), stderr);
-              char const *const *p = time_style_args;
-              while (*p)
-                fprintf (stderr, "  - [posix-]%s\n", *p++);
-              fputs (_("  - +FORMAT (e.g., +%H:%M) for a 'date'-style"
-                       " format\n"), stderr);
-              usage (LS_FAILURE);
-            }
-          switch (res)
+          switch (x_timestyle_match (style, /*allow_posix=*/ true,
+                                     time_style_args,
+                                     (char const *) time_style_types,
+                                     sizeof (*time_style_types), LS_FAILURE))
             {
             case full_iso_time_style:
               long_time_format[0] = long_time_format[1] =
index ca223d547ed67d28eaa90575a00a875ce3721dcf..5cb751cc89a4024c88e4091e2c0ce475b2429d7d 100644 (file)
@@ -794,3 +794,41 @@ is_ENOTSUP (int err)
   quotearg_style (shell_escape_always_quoting_style, arg)
 #define quoteaf_n(n, arg) \
   quotearg_n_style (n, shell_escape_always_quoting_style, arg)
+
+/* Used instead of XARGMATCH() to provide a custom error message.  */
+#ifdef XARGMATCH
+static inline ptrdiff_t
+x_timestyle_match (char const * style, bool allow_posix,
+                   char const *const * timestyle_args,
+                   char const * timestyle_types,
+                   size_t timestyle_types_size,
+                   int fail_status)
+{
+  ptrdiff_t res = argmatch (style, timestyle_args,
+                            (char const *) timestyle_types,
+                            timestyle_types_size);
+  if (res < 0)
+    {
+      /* This whole block used to be a simple use of XARGMATCH.
+         but that didn't print the "posix-"-prefixed variants or
+         the "+"-prefixed format string option upon failure.  */
+      argmatch_invalid ("time style", style, res);
+
+      /* The following is a manual expansion of argmatch_valid,
+         but with the added "+ ..." description and the [posix-]
+         prefixes prepended.  Note that this simplification works
+         only because all four existing time_style_types values
+         are distinct.  */
+      fputs (_("Valid arguments are:\n"), stderr);
+      char const *const *p = timestyle_args;
+      char const *posix_prefix = allow_posix ? "[posix-]" : "";
+      while (*p)
+        fprintf (stderr, "  - %s%s\n", posix_prefix, *p++);
+      fputs (_("  - +FORMAT (e.g., +%H:%M) for a 'date'-style"
+               " format\n"), stderr);
+      usage (fail_status);
+    }
+
+  return res;
+}
+#endif