From: Nikhilkumar Shelke Date: Tue, 25 Jan 2022 18:07:24 +0000 (+0530) Subject: mount: use getopt_long to parse command options X-Git-Tag: v18.0.0~1419^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=b73d279fe617f67e07b7d0d5ae3f2acd79e8d18b;p=ceph-ci.git mount: use getopt_long to parse command options Fixes: https://tracker.ceph.com/issues/53903 Signed-off-by: Nikhilkumar Shelke --- diff --git a/src/mount/mount.ceph.c b/src/mount/mount.ceph.c index abbbe80cb93..87557112bef 100644 --- a/src/mount/mount.ceph.c +++ b/src/mount/mount.ceph.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "common/module.h" #include "common/secret.h" @@ -620,7 +621,15 @@ out: static int parse_arguments(int argc, char *const *const argv, const char **src, const char **node, const char **opts) { - int i; + int opt = 0; + static struct option long_options[] = { + { "help", no_argument, 0, 'h' }, + { "no-mtab", no_argument, 0, 'n' }, + { "verbose", no_argument, 0, 'v' }, + { "fake", no_argument, 0, 'f' }, + { "options", required_argument, 0, 'o' }, + { 0, 0, 0, 0 } + }; if (argc < 2) { // There were no arguments. Just show the usage. @@ -639,25 +648,25 @@ static int parse_arguments(int argc, char *const *const argv, // Parse the remaining options *opts = EMPTY_STRING; - for (i = 3; i < argc; ++i) { - if (!strcmp("-h", argv[i])) - return 1; - else if (!strcmp("-n", argv[i])) - skip_mtab_flag = true; - else if (!strcmp("-v", argv[i])) - verboseflag = true; - else if (!strcmp("-f", argv[i])) - fakeflag = true; - else if (!strcmp("-o", argv[i])) { - ++i; - if (i >= argc) { - fprintf(stderr, "Option -o requires an argument.\n\n"); + while ((opt = getopt_long(argc, argv, "hnvfo:", + long_options, NULL)) != -1) { + switch (opt) { + case 'h' : // -h or --help + return 1; + case 'n' : // -n or --no-mtab + skip_mtab_flag = true; + break; + case 'v' : // -v or --verbose + verboseflag = true; + break; + case 'f' : // -f or --fake + fakeflag = true; + break; + case 'o' : // -o or --options + *opts = optarg; + break; + default: return -EINVAL; - } - *opts = argv[i]; - } else { - fprintf(stderr, "Can't understand option: '%s'\n\n", argv[i]); - return -EINVAL; } } return 0; @@ -679,11 +688,11 @@ static void usage(const char *prog_name) printf("usage: %s [src] [mount-point] [-n] [-v] [-o ceph-options]\n", prog_name); printf("options:\n"); - printf("\t-h: Print this help\n"); - printf("\t-n: Do not update /etc/mtab\n"); - printf("\t-v: Verbose\n"); - printf("\t-f: Fake mount, do not actually mount\n"); - printf("\tceph-options: refer to mount.ceph(8)\n"); + printf("\t-h, --help\tPrint this help\n"); + printf("\t-n, --no-mtab\tDo not update /etc/mtab\n"); + printf("\t-v, --verbose\tVerbose\n"); + printf("\t-f, --fake\tFake mount, do not actually mount\n"); + printf("ceph-options: refer to mount.ceph(8)\n"); printf("\n"); }