diff options
author | Lorenzo Colitti <lorenzo@google.com> | 2017-03-16 16:55:01 +0900 |
---|---|---|
committer | Lorenzo Colitti <lorenzo@google.com> | 2017-03-22 17:13:12 +0900 |
commit | 097d6bee9cb25c94e288ad72099c51bab7fe113c (patch) | |
tree | 86f5faee186f6905f5c1e1161b09ca43f4fce045 /iptables/xshared.c | |
parent | 0e96b2160f961dbbdb4b0803a256c11beec1d264 (diff) | |
download | platform_external_iptables-097d6bee9cb25c94e288ad72099c51bab7fe113c.tar.gz platform_external_iptables-097d6bee9cb25c94e288ad72099c51bab7fe113c.tar.bz2 platform_external_iptables-097d6bee9cb25c94e288ad72099c51bab7fe113c.zip |
iptables: remove duplicated argument parsing code
1. Factor out repeated code to a new xs_has_arg function.
2. Add a new parse_wait_time option to parse the value of -w.
3. Make parse_wait_interval take argc and argv so its callers
can be simpler.
Signed-off-by: Lorenzo Colitti <lorenzo@google.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
(cherry picked from commit 6e2e169eb66b63d2991e1c7ada931e3cdb0ced32)
Bug: 36108349
Test: see top of change stack.
Change-Id: Iae185e267d90806dac2cbfdad2a066a2929947fc
Diffstat (limited to 'iptables/xshared.c')
-rw-r--r-- | iptables/xshared.c | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/iptables/xshared.c b/iptables/xshared.c index 9b8e856e..06147d18 100644 --- a/iptables/xshared.c +++ b/iptables/xshared.c @@ -285,12 +285,36 @@ bool xtables_lock(int wait, struct timeval *wait_interval) } } -void parse_wait_interval(const char *str, struct timeval *wait_interval) +int parse_wait_time(int argc, char *argv[]) { + int wait = -1; + + if (optarg) { + if (sscanf(optarg, "%i", &wait) != 1) + xtables_error(PARAMETER_PROBLEM, + "wait seconds not numeric"); + } else if (xs_has_arg(argc, argv)) + if (sscanf(argv[optind++], "%i", &wait) != 1) + xtables_error(PARAMETER_PROBLEM, + "wait seconds not numeric"); + + return wait; +} + +void parse_wait_interval(int argc, char *argv[], struct timeval *wait_interval) +{ + const char *arg; unsigned int usec; int ret; - ret = sscanf(str, "%u", &usec); + if (optarg) + arg = optarg; + else if (xs_has_arg(argc, argv)) + arg = argv[optind++]; + else + return; + + ret = sscanf(arg, "%u", &usec); if (ret == 1) { if (usec > 999999) xtables_error(PARAMETER_PROBLEM, @@ -303,3 +327,10 @@ void parse_wait_interval(const char *str, struct timeval *wait_interval) } xtables_error(PARAMETER_PROBLEM, "wait interval not numeric"); } + +inline bool xs_has_arg(int argc, char *argv[]) +{ + return optind < argc && + argv[optind][0] != '-' && + argv[optind][0] != '!'; +} |