@@ -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);
}
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(-)