From 004f06f32dd28f225a00d3ba7648a29eab28132e Mon Sep 17 00:00:00 2001 From: Holger Eitzenberger Date: Mon, 24 Jan 2011 22:36:35 +0100 Subject: ipset: avoid the unnecessary argv[] loop After stripping off the global options there simply has to follow a command name, there is no other syntax possible. Therefore the argv[] loop is unnecessary. Signed-off-by: Holger Eitzenberger --- src/ipset.c | 96 +++++++++++++++++++++++++++++-------------------------------- 1 file changed, 46 insertions(+), 50 deletions(-) (limited to 'src/ipset.c') diff --git a/src/ipset.c b/src/ipset.c index 2cfcfac..7dbb303 100644 --- a/src/ipset.c +++ b/src/ipset.c @@ -470,61 +470,57 @@ parse_commandline(int argc, char *argv[]) /* Second: parse command */ for (command = ipset_commands; - command->cmd && cmd == IPSET_CMD_NONE; + argc > 1 && command->cmd && cmd == IPSET_CMD_NONE; command++) { - for (i = 1; i < argc; ) { - if (!ipset_match_cmd(argv[1], command->name)) { - i++; - continue; - } - if (restore_line != 0 - && (command->cmd == IPSET_CMD_RESTORE - || command->cmd == IPSET_CMD_VERSION - || command->cmd == IPSET_CMD_HELP)) + if (!ipset_match_cmd(argv[1], command->name)) + continue; + + if (restore_line != 0 + && (command->cmd == IPSET_CMD_RESTORE + || command->cmd == IPSET_CMD_VERSION + || command->cmd == IPSET_CMD_HELP)) + return exit_error(PARAMETER_PROBLEM, + "Command `%s' is invalid " + "in restore mode.", + command->name[0]); + if (interactive && command->cmd == IPSET_CMD_RESTORE) { + printf("Restore command ignored " + "in interactive mode\n"); + return 0; + } + + /* Shift off matched command arg */ + ipset_shift_argv(&argc, argv, 1); + cmd = command->cmd; + switch (command->has_arg) { + case IPSET_MANDATORY_ARG: + case IPSET_MANDATORY_ARG2: + if (argc < 2) return exit_error(PARAMETER_PROBLEM, - "Command `%s' is invalid " - "in restore mode.", + "Missing mandatory argument " + "to command %s", command->name[0]); - if (interactive - && command->cmd == IPSET_CMD_RESTORE) { - printf("Restore command ignored " - "in interactive mode\n"); - return 0; - } - - /* Shift off matched command arg */ - ipset_shift_argv(&argc, argv, i); - cmd = command->cmd; - switch (command->has_arg) { - case IPSET_MANDATORY_ARG: - case IPSET_MANDATORY_ARG2: - if (i + 1 > argc) - return exit_error(PARAMETER_PROBLEM, - "Missing mandatory argument " - "to command %s", - command->name[0]); - /* Fall through */ - case IPSET_OPTIONAL_ARG: - arg0 = argv[i]; - if (i + 1 <= argc) - /* Shift off first arg */ - ipset_shift_argv(&argc, argv, i); - break; - default: - break; - } - if (command->has_arg == IPSET_MANDATORY_ARG2) { - if (i + 1 > argc) - return exit_error(PARAMETER_PROBLEM, - "Missing second mandatory " - "argument to command %s", - command->name[0]); - arg1 = argv[i]; - /* Shift off second arg */ - ipset_shift_argv(&argc, argv, i); - } + /* Fall through */ + case IPSET_OPTIONAL_ARG: + arg0 = argv[1]; + if (argc >= 2) + /* Shift off first arg */ + ipset_shift_argv(&argc, argv, 1); + break; + default: break; } + if (command->has_arg == IPSET_MANDATORY_ARG2) { + if (argc < 2) + return exit_error(PARAMETER_PROBLEM, + "Missing second mandatory " + "argument to command %s", + command->name[0]); + arg1 = argv[1]; + /* Shift off second arg */ + ipset_shift_argv(&argc, argv, 1); + } + break; } /* Third: catch interactive mode, handle help, version */ -- cgit v1.2.3