[PATCH] iptables: fix error reporting for missing/wrong arguments This patch fixes a confusing error reporting when some arguments are missing or the option passed is unknown. According to man getopt_long: "If getopt() finds an option character in argv that was not included in optstring, or if it detects a missing option argument, it returns '?' and sets the external variable optopt to the actual option character." Without the patch: % iptables -I INPUT -m state --whatever iptables v1.4.2-rc1: Unknown arg `(null) % iptables -I INPUT -m state --state iptables v1.4.2-rc1: Unknown arg `(null) With the patch: % iptables -I INPUT -m state --whatever iptables v1.4.2-rc1: unknown option `--whatever' Try `iptables -h' or 'iptables --help' for more information. % iptables -I INPUT -m state --state iptables v1.4.2-rc1: option `--state' requires an argument Try `iptables -h' or 'iptables --help' for more information. Signed-off-by: Pablo Neira Ayuso diff --git a/ip6tables.c b/ip6tables.c index 12298ca..9ce1074 100644 --- a/ip6tables.c +++ b/ip6tables.c @@ -1888,9 +1888,26 @@ int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle) continue; } - if (!m) + if (!m) { + if (c == '?') { + if (optopt) { + exit_error( + PARAMETER_PROBLEM, + "option `%s' " + "requires an " + "argument", + argv[optind-1]); + } else { + exit_error( + PARAMETER_PROBLEM, + "unknown option " + "`%s'", + argv[optind-1]); + } + } exit_error(PARAMETER_PROBLEM, "Unknown arg `%s'", optarg); + } } } invert = FALSE; diff --git a/iptables.c b/iptables.c index b927a11..d2b9081 100644 --- a/iptables.c +++ b/iptables.c @@ -1909,9 +1909,26 @@ int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle) optind--; continue; } - if (!m) + if (!m) { + if (c == '?') { + if (optopt) { + exit_error( + PARAMETER_PROBLEM, + "option `%s' " + "requires an " + "argument", + argv[optind-1]); + } else { + exit_error( + PARAMETER_PROBLEM, + "unknown option " + "`%s'", + argv[optind-1]); + } + } exit_error(PARAMETER_PROBLEM, "Unknown arg `%s'", optarg); + } } } invert = FALSE;