From a7c63296c1364862cedf96e4932147a2646c7843 Mon Sep 17 00:00:00 2001 From: =?utf8?q?P=C3=A1draig=20Brady?= Date: Mon, 28 Jul 2025 23:05:49 +0100 Subject: [PATCH] du: improve diagnostics for --time-style * 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 | 8 +++++--- src/ls.c | 30 +++++------------------------- src/system.h | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/du.c b/src/du.c index bac372a042..c93d7f71b4 100644 --- a/src/du.c +++ b/src/du.c @@ -26,8 +26,8 @@ #include #include #include -#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"; diff --git a/src/ls.c b/src/ls.c index e84e0facff..d9faddee41 100644 --- a/src/ls.c +++ b/src/ls.c @@ -80,11 +80,11 @@ # define SA_RESTART 0 #endif -#include "system.h" #include #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] = diff --git a/src/system.h b/src/system.h index ca223d547e..5cb751cc89 100644 --- a/src/system.h +++ b/src/system.h @@ -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 -- 2.47.2