[PATCH v1 06/13] tools/x86/kcpuid: Simplify usage() handling
From: Ahmed S. Darwish
Date: Thu Nov 28 2024 - 17:31:38 EST
Modify usage() to take a process exit_code parameter and directly call
exit() after printing the usage string. This simplifies the callers'
code paths.
Remove the manual "Invalid option" error message at option parsing,
since getopt_long(3) already prints sensible error messages by default.
Signed-off-by: Ahmed S. Darwish <darwi@xxxxxxxxxxxxx>
---
tools/arch/x86/kcpuid/kcpuid.c | 21 ++++++++-------------
1 file changed, 8 insertions(+), 13 deletions(-)
diff --git a/tools/arch/x86/kcpuid/kcpuid.c b/tools/arch/x86/kcpuid/kcpuid.c
index ac8fdfdc4844..c0f2eae0d694 100644
--- a/tools/arch/x86/kcpuid/kcpuid.c
+++ b/tools/arch/x86/kcpuid/kcpuid.c
@@ -11,6 +11,7 @@
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define min(a, b) (((a) < (b)) ? (a) : (b))
+#define __noreturn __attribute__((__noreturn__))
typedef unsigned int u32;
typedef unsigned long long u64;
@@ -608,9 +609,9 @@ static void setup_platform_cpuid(void)
leafs_ext = setup_cpuid_range(0x80000000);
}
-static void usage(void)
+static void __noreturn usage(int exit_code)
{
- pr_err("kcpuid [-abdfhr] [-l leaf] [-s subleaf]\n"
+ pr_err_exit(exit_code, "kcpuid [-abdfhr] [-l leaf] [-s subleaf]\n"
"\t-a|--all Show both bit flags and complex bit fields info\n"
"\t-b|--bitflags Show boolean flags only\n"
"\t-d|--detail Show details of the flag/fields (default)\n"
@@ -634,7 +635,7 @@ static struct option opts[] = {
{ NULL, 0, NULL, 0 }
};
-static int parse_options(int argc, char *argv[])
+static void parse_options(int argc, char *argv[])
{
int c;
@@ -654,9 +655,7 @@ static int parse_options(int argc, char *argv[])
user_csv = optarg;
break;
case 'h':
- usage();
- exit(1);
- break;
+ usage(EXIT_SUCCESS);
case 'l':
/* main leaf */
user_index = strtoul(optarg, NULL, 0);
@@ -669,11 +668,8 @@ static int parse_options(int argc, char *argv[])
user_sub = strtoul(optarg, NULL, 0);
break;
default:
- pr_err("%s: Invalid option '%c'\n", argv[0], optopt);
- return -1;
- }
-
- return 0;
+ usage(EXIT_FAILURE);
+ }
}
/*
@@ -686,8 +682,7 @@ static int parse_options(int argc, char *argv[])
*/
int main(int argc, char *argv[])
{
- if (parse_options(argc, argv))
- return -1;
+ parse_options(argc, argv);
/* Setup the cpuid leafs of current platform */
setup_platform_cpuid();
--
2.46.2