From: Kit Westneat Date: Sat, 13 Dec 2014 17:36:29 +0000 (-0500) Subject: LU-6025 utils: reset optind before calls to getopt_long() X-Git-Tag: 2.6.92~6 X-Git-Url: https://git.whamcloud.com/?a=commitdiff_plain;h=81b3399d79d6e8bbe64d3fb68c27209274b4cf9c;p=fs%2Flustre-release.git LU-6025 utils: reset optind before calls to getopt_long() The nodemap commands in lctl were not properly resetting optind between calls to getopt_long(). This patch sets optind to 0 before calling any subcommands. Signed-off-by: Kit Westneat Change-Id: I14a1f34000c678799298dae3c248c839a1f32d52 Reviewed-on: http://review.whamcloud.com/13063 Reviewed-by: Andreas Dilger Tested-by: Jenkins Reviewed-by: Nathaniel Clark Tested-by: Maloo --- diff --git a/libcfs/libcfs/util/parser.c b/libcfs/libcfs/util/parser.c index 556e257..6ca87e0 100644 --- a/libcfs/libcfs/util/parser.c +++ b/libcfs/libcfs/util/parser.c @@ -243,46 +243,45 @@ static char **command_completion(const char *text, int start, int end) /* take a string and execute the function or print help */ int execute_line(char * line) { - command_t *cmd, *ambig; - char *prev; - char *next, *tmp; - char *argv[MAXARGS]; - int i; - int rc = 0; - - switch (process(line, &next, top_level, &cmd, &prev)) { - case CMD_AMBIG: - fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line); - while( (ambig = find_cmd(prev, cmd, &tmp)) ) { - fprintf(stderr, "%s ", ambig->pc_name); - cmd = ambig + 1; - } - fprintf(stderr, "\n"); - break; - case CMD_NONE: - fprintf(stderr, "No such command, type help\n"); - break; - case CMD_INCOMPLETE: - fprintf(stderr, - "'%s' incomplete command. Use '%s x' where x is one of:\n", - line, line); - fprintf(stderr, "\t"); - for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) { - fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name); - } - fprintf(stderr, "\n"); - break; - case CMD_COMPLETE: - i = line2args(line, argv, MAXARGS); - rc = (cmd->pc_func)(i, argv); - - if (rc == CMD_HELP) - fprintf(stderr, "%s\n", cmd->pc_help); - - break; - } + command_t *cmd, *ambig; + char *prev; + char *next, *tmp; + char *argv[MAXARGS]; + int i; + int rc = 0; + + switch (process(line, &next, top_level, &cmd, &prev)) { + case CMD_AMBIG: + fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line); + while ((ambig = find_cmd(prev, cmd, &tmp))) { + fprintf(stderr, "%s ", ambig->pc_name); + cmd = ambig + 1; + } + fprintf(stderr, "\n"); + break; + case CMD_NONE: + fprintf(stderr, "No such command, type help\n"); + break; + case CMD_INCOMPLETE: + fprintf(stderr, "'%s' incomplete command. Use '%s x' where " + "x is one of:\n", line, line); + fprintf(stderr, "\t"); + for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) + fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name); + fprintf(stderr, "\n"); + break; + case CMD_COMPLETE: + optind = 0; + i = line2args(line, argv, MAXARGS); + rc = (cmd->pc_func)(i, argv); + + if (rc == CMD_HELP) + fprintf(stderr, "%s\n", cmd->pc_help); + + break; + } - return rc; + return rc; } #ifdef HAVE_LIBREADLINE diff --git a/lnet/utils/lnetctl.c b/lnet/utils/lnetctl.c index 6f3b2323..978da69 100644 --- a/lnet/utils/lnetctl.c +++ b/lnet/utils/lnetctl.c @@ -299,7 +299,6 @@ static int jt_config_lnet(int argc, char **argv) struct cYAML *err_rc = NULL; bool load_mod_params = false; int rc, opt; - optind = 0; const char *const short_options = "ah"; const struct option long_options[] = { @@ -337,7 +336,6 @@ static int jt_unconfig_lnet(int argc, char **argv) { struct cYAML *err_rc = NULL; int rc; - optind = 0; if (handle_help(lnet_cmds, "lnet", "unconfigure", argc, argv) == 0) return 0; @@ -357,7 +355,6 @@ static int jt_add_route(int argc, char **argv) long int hop = -1, prio = -1; struct cYAML *err_rc = NULL; int rc, opt; - optind = 0; const char *const short_options = "n:g:c:p:h"; const struct option long_options[] = { @@ -418,7 +415,6 @@ static int jt_add_net(int argc, char **argv) long int pto = -1, pc = -1, pbc = -1, cre = -1; struct cYAML *err_rc = NULL; int rc, opt; - optind = 0; const char *const short_options = "n:i:p:t:c:b:r:s:h"; const struct option long_options[] = { @@ -505,7 +501,6 @@ static int jt_del_route(int argc, char **argv) char *network = NULL, *gateway = NULL; struct cYAML *err_rc = NULL; int rc, opt; - optind = 0; const char *const short_options = "n:g:h"; const struct option long_options[] = { @@ -547,7 +542,6 @@ static int jt_del_net(int argc, char **argv) char *network = NULL; struct cYAML *err_rc = NULL; int rc, opt; - optind = 0; const char *const short_options = "n:h"; const struct option long_options[] = { @@ -586,7 +580,6 @@ static int jt_show_route(int argc, char **argv) long int hop = -1, prio = -1; int detail = 0, rc, opt; struct cYAML *err_rc = NULL, *show_rc = NULL; - optind = 0; const char *const short_options = "n:g:h:p:vh"; const struct option long_options[] = { @@ -654,7 +647,6 @@ static int jt_show_net(int argc, char **argv) char *network = NULL; int detail = 0, rc, opt; struct cYAML *err_rc = NULL, *show_rc = NULL; - optind = 0; const char *const short_options = "n:vh"; const struct option long_options[] = { @@ -740,7 +732,6 @@ static int jt_show_peer_credits(int argc, char **argv) { int rc; struct cYAML *show_rc = NULL, *err_rc = NULL; - optind = 0; if (handle_help(credits_cmds, "peer_credits", "show", argc, argv) == 0) return 0; @@ -848,7 +839,6 @@ static int jt_import(int argc, char **argv) struct cYAML *err_rc = NULL; struct cYAML *show_rc = NULL; int rc = 0, opt, opt_found = 0; - optind = 0; char cmd = 'a'; const char *const short_options = "adsh"; @@ -918,7 +908,6 @@ static int jt_export(int argc, char **argv) struct cYAML *err_rc = NULL; int rc, opt; FILE *f = NULL; - optind = 0; const char *const short_options = "h"; const struct option long_options[] = { diff --git a/lustre/utils/lfs.c b/lustre/utils/lfs.c index 03caeed..048b399 100644 --- a/lustre/utils/lfs.c +++ b/lustre/utils/lfs.c @@ -774,7 +774,6 @@ static int lfs_setstripe(int argc, char **argv) if (strcmp(argv[0], "migrate") == 0) migrate_mode = true; - optind = 0; while ((c = getopt_long(argc, argv, "c:di:o:p:s:S:", long_opts, NULL)) >= 0) { switch (c) { @@ -1096,7 +1095,6 @@ static int lfs_find(int argc, char **argv) time(&t); - optind = 0; /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */ while ((c = getopt_long_only(argc, argv, "-A:c:C:D:g:G:i:L:m:M:n:O:Ppqrs:S:t:u:U:v", @@ -1478,7 +1476,6 @@ static int lfs_getstripe_internal(int argc, char **argv, int c, rc; param->fp_max_depth = 1; - optind = 0; while ((c = getopt_long(argc, argv, "cdDghiLMoO:pqrRsSv", long_opts, NULL)) != -1) { switch (c) { @@ -1678,8 +1675,6 @@ static int lfs_setdirstripe(int argc, char **argv) {0, 0, 0, 0} }; - optind = 0; - while ((c = getopt_long(argc, argv, "c:Di:m:t:", long_opts, NULL)) >= 0) { switch (c) { @@ -2097,7 +2092,6 @@ static int lfs_df(int argc, char **argv) {0, 0, 0, 0} }; - optind = 0; while ((c = getopt_long(argc, argv, "hilp:", long_opts, NULL)) != -1) { switch (c) { case 'i': @@ -2144,7 +2138,6 @@ static int lfs_getname(int argc, char **argv) int rc = 0, index = 0, c; char buf[sizeof(struct obd_uuid)]; - optind = 0; while ((c = getopt(argc, argv, "h")) != -1) return CMD_HELP; @@ -2241,7 +2234,6 @@ static int lfs_quotacheck(int argc, char **argv) memset(&qchk, 0, sizeof(qchk)); - optind = 0; while ((c = getopt(argc, argv, "gu")) != -1) { switch (c) { case 'u': @@ -2316,7 +2308,6 @@ static int lfs_quotaon(int argc, char **argv) memset(&qctl, 0, sizeof(qctl)); qctl.qc_cmd = LUSTRE_Q_QUOTAON; - optind = 0; while ((c = getopt(argc, argv, "fgu")) != -1) { switch (c) { case 'u': @@ -2384,7 +2375,6 @@ static int lfs_quotaoff(int argc, char **argv) memset(&qctl, 0, sizeof(qctl)); qctl.qc_cmd = LUSTRE_Q_QUOTAOFF; - optind = 0; while ((c = getopt(argc, argv, "gu")) != -1) { switch (c) { case 'u': @@ -2546,7 +2536,6 @@ int lfs_setquota_times(int argc, char **argv) qctl.qc_cmd = LUSTRE_Q_SETINFO; qctl.qc_type = UGQUOTA; - optind = 0; while ((c = getopt_long(argc, argv, "b:gi:tu", long_opts, NULL)) != -1) { switch (c) { case 'u': @@ -2636,7 +2625,6 @@ int lfs_setquota(int argc, char **argv) * so it can be used as a marker that qc_type * isn't reinitialized from command line */ - optind = 0; while ((c = getopt_long(argc, argv, "b:B:g:i:I:u:", long_opts, NULL)) != -1) { switch (c) { case 'u': @@ -3023,7 +3011,6 @@ static int lfs_quota(int argc, char **argv) __u64 total_ialloc = 0, total_balloc = 0; bool human_readable = false; - optind = 0; while ((c = getopt(argc, argv, "gi:I:o:qtuvh")) != -1) { switch (c) { case 'u': @@ -3202,7 +3189,6 @@ static int lfs_flushctx(int argc, char **argv) int index = 0; int rc = 0; - optind = 0; while ((c = getopt(argc, argv, "k")) != -1) { switch (c) { case 'k': @@ -3291,7 +3277,6 @@ static int lfs_changelog(int argc, char **argv) char short_opts[] = "f"; int rc, follow = 0; - optind = 0; while ((rc = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (rc) { @@ -3418,8 +3403,6 @@ static int lfs_fid2path(int argc, char **argv) int printcur = 0; int rc = 0; - optind = 0; - while ((rc = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (rc) { @@ -3507,7 +3490,6 @@ static int lfs_path2fid(int argc, char **argv) int rc = 0; bool show_parents = false; - optind = 0; while ((rc = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (rc) { @@ -3582,7 +3564,6 @@ static int lfs_data_version(int argc, char **argv) if (argc < 2) return CMD_HELP; - optind = 0; while ((c = getopt(argc, argv, "nrw")) != -1) { switch (c) { case 'n': @@ -3692,7 +3673,6 @@ static int lfs_hsm_change_flags(int argc, char **argv, int mode) if (argc < 3) return CMD_HELP; - optind = 0; while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (c) { @@ -3880,7 +3860,6 @@ static int lfs_hsm_request(int argc, char **argv, int action) if (argc < 2) return CMD_HELP; - optind = 0; while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (c) { diff --git a/lustre/utils/obd.c b/lustre/utils/obd.c index 5a296ee..b33a85a 100644 --- a/lustre/utils/obd.c +++ b/lustre/utils/obd.c @@ -1154,7 +1154,6 @@ int jt_obd_md_common(int argc, char **argv, int cmd) {0, 0, 0, 0} }; - optind = 0; while ((c = getopt_long(argc, argv, "b:c:d:D:m:n:t:v", long_opts, NULL)) >= 0) { switch (c) {