From: Fouad Hilly <fouad.hilly@cloud.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: Fouad Hilly <fouad.hilly@cloud.com>,
Anthony PERARD <anthony@xenproject.org>
Subject: [PATCH v3 4/5] x86: Use getopt to handle command line args
Date: Tue, 30 Apr 2024 13:47:08 +0100 [thread overview]
Message-ID: <20240430124709.865183-5-fouad.hilly@cloud.com> (raw)
In-Reply-To: <20240430124709.865183-1-fouad.hilly@cloud.com>
Use getopt_long() to handle command line arguments.
Introduce ext_err for common exit with errors.
Signed-off-by: Fouad Hilly <fouad.hilly@cloud.com>
---
[v3]
1- Exit with error prints erros only to stderr.
2- Remove argc checks.
3- Utilize optind.
[v2]
1- Removed unnecessary NULL initialization.
2- Used static at the beginning of type struct declaration.
3- Corrected switch\case indentations.
4- Removed redundant checks.
5- Corrected label indentation.
tools/misc/xen-ucode.c | 45 +++++++++++++++++++++++++++++++-----------
1 file changed, 33 insertions(+), 12 deletions(-)
diff --git a/tools/misc/xen-ucode.c b/tools/misc/xen-ucode.c
index 005bf85b6551..d95f967f021b 100644
--- a/tools/misc/xen-ucode.c
+++ b/tools/misc/xen-ucode.c
@@ -11,6 +11,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <xenctrl.h>
+#include <getopt.h>
static xc_interface *xch;
@@ -20,7 +21,10 @@ static const char amd_id[] = "AuthenticAMD";
static void usage(const char *name)
{
printf("%s: Xen microcode updating tool\n"
- "Usage: %s [<microcode file> | show-cpu-info]\n",
+ "Usage: %s [<microcode file> | Options]\n"
+ "options:\n"
+ " -h, --help display this help and exit\n"
+ " -s, --show-cpu-info show CPU information and exit\n",
name, name);
}
@@ -84,6 +88,13 @@ int main(int argc, char *argv[])
char *filename, *buf;
size_t len;
struct stat st;
+ int opt;
+
+ static const struct option options[] = {
+ {"help", no_argument, NULL, 'h'},
+ {"show-cpu-info", no_argument, NULL, 's'},
+ {NULL, no_argument, NULL, 0}
+ };
xch = xc_interface_open(NULL, NULL, 0);
if ( xch == NULL )
@@ -93,21 +104,25 @@ int main(int argc, char *argv[])
exit(1);
}
- if ( argc < 2 )
+ while ( (opt = getopt_long(argc, argv, "hs", options, NULL)) != -1 )
{
- fprintf(stderr,
- "%s: unable to process command line arguments\n", argv[0]);
- usage(argv[0]);
- exit(2);
+ switch (opt)
+ {
+ case 'h':
+ usage(argv[0]);
+ exit(EXIT_SUCCESS);
+ case 's':
+ show_curr_cpu(stdout);
+ exit(EXIT_SUCCESS);
+ default:
+ goto ext_err;
+ }
}
- if ( !strcmp(argv[1], "show-cpu-info") )
- {
- show_curr_cpu(stdout);
- return 0;
- }
+ if ( optind == argc )
+ goto ext_err;
- filename = argv[1];
+ filename = argv[optind];
fd = open(filename, O_RDONLY);
if ( fd < 0 )
{
@@ -149,4 +164,10 @@ int main(int argc, char *argv[])
close(fd);
return 0;
+
+ ext_err:
+ fprintf(stderr,
+ "%s: unable to process command line arguments\n", argv[0]);
+ usage(argv[0]);
+ exit(EXIT_FAILURE);
}
--
2.42.0
next prev parent reply other threads:[~2024-04-30 12:47 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-30 12:47 [PATCH v3 0/5] x86/xen-ucode: Introduce --force option Fouad Hilly
2024-04-30 12:47 ` [PATCH v3 1/5] x86: Update x86 low level version check of microcode Fouad Hilly
2024-05-06 8:45 ` Jan Beulich
2024-05-09 14:33 ` Fouad Hilly
2024-04-30 12:47 ` [PATCH v3 2/5] x86: Refactor microcode_update() hypercall with flags Fouad Hilly
2024-05-06 9:14 ` Jan Beulich
2024-05-09 14:15 ` Fouad Hilly
2024-04-30 12:47 ` [PATCH v3 3/5] x86: Add usage() to print out usage message Fouad Hilly
2024-05-06 8:20 ` Jan Beulich
2024-05-09 13:59 ` Fouad Hilly
2024-04-30 12:47 ` Fouad Hilly [this message]
2024-05-06 8:21 ` [PATCH v3 4/5] x86: Use getopt to handle command line args Jan Beulich
2024-05-09 13:59 ` Fouad Hilly
2024-04-30 12:47 ` [PATCH v3 5/5] Add --force option to xen-ucode to override microcode version check Fouad Hilly
2024-05-06 9:39 ` Jan Beulich
2024-05-09 14:31 ` Fouad Hilly
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240430124709.865183-5-fouad.hilly@cloud.com \
--to=fouad.hilly@cloud.com \
--cc=anthony@xenproject.org \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).