From: Bartosz Golaszewski Date: Mon, 9 Oct 2017 21:05:30 +0000 (+0200) Subject: gpioset: fix checking mutually exclusive options X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=6521c1234097277c3d8f2c14fc6031e17dfcfeb0;p=qemu-gpiodev%2Flibgpiod.git gpioset: fix checking mutually exclusive options Certain options are mutually exclusive but they're checked in a way that doesn't always guarantee gpioset to bail out. Fix it by checking the options that don't work together after all command-line parsing is done. Signed-off-by: Bartosz Golaszewski --- diff --git a/src/tools/gpioset.c b/src/tools/gpioset.c index 5a62c0b..6096e7c 100644 --- a/src/tools/gpioset.c +++ b/src/tools/gpioset.c @@ -206,23 +206,17 @@ int main(int argc, char **argv) die("invalid mode: %s", optarg); break; case 's': - if (mode->id != MODE_TIME) - die("can't specify seconds in this mode"); cbdata.tv.tv_sec = strtoul(optarg, &end, 10); if (*end != '\0') die("invalid time value in seconds: %s", optarg); break; case 'u': - if (mode->id != MODE_TIME) - die("can't specify microseconds in this mode"); cbdata.tv.tv_usec = strtoul(optarg, &end, 10); if (*end != '\0') die("invalid time value in microseconds: %s", optarg); break; case 'b': - if (mode->id != MODE_SIGNAL && mode->id != MODE_TIME) - die("can't daemonize in this mode"); cbdata.daemonize = true; break; case '?': @@ -235,6 +229,14 @@ int main(int argc, char **argv) argc -= optind; argv += optind; + if (mode->id != MODE_TIME && (cbdata.tv.tv_sec || cbdata.tv.tv_usec)) + die("can't specify wait time in this mode"); + + if (mode->id != MODE_SIGNAL && + mode->id != MODE_TIME && + cbdata.daemonize) + die("can't daemonize in this mode"); + if (argc < 1) die("gpiochip must be specified"); diff --git a/tests/tests-gpioset.c b/tests/tests-gpioset.c index 3970c16..d265bf5 100644 --- a/tests/tests-gpioset.c +++ b/tests/tests-gpioset.c @@ -307,7 +307,7 @@ static void gpioset_sec_usec_without_time(void) TEST_ASSERT_NULL(test_tool_stdout()); TEST_ASSERT_NOT_NULL(test_tool_stderr()); TEST_ASSERT_STR_CONTAINS(test_tool_stderr(), - "can't specify seconds in this mode"); + "can't specify wait time in this mode"); test_tool_run("gpioset", "--mode=exit", "--usec=100", test_chip_name(0), "0=1", (char *)NULL); @@ -318,7 +318,7 @@ static void gpioset_sec_usec_without_time(void) TEST_ASSERT_NULL(test_tool_stdout()); TEST_ASSERT_NOT_NULL(test_tool_stderr()); TEST_ASSERT_STR_CONTAINS(test_tool_stderr(), - "can't specify microseconds in this mode"); + "can't specify wait time in this mode"); } TEST_DEFINE(gpioset_sec_usec_without_time, "tools: gpioset - using --sec/--usec with mode other than 'time'",