gpioset: fix checking mutually exclusive options
authorBartosz Golaszewski <bartekgola@gmail.com>
Mon, 9 Oct 2017 21:05:30 +0000 (23:05 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Mon, 9 Oct 2017 21:05:30 +0000 (23:05 +0200)
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 <bartekgola@gmail.com>
src/tools/gpioset.c
tests/tests-gpioset.c

index 5a62c0b834319c6e6722cb371aba3b14af959b71..6096e7cf5ad73a66eb126e8b6ec4dfa554082ea9 100644 (file)
@@ -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");
 
index 3970c1664fb72022e3d8e26291d1201ed2381c87..d265bf562ec84358a1646f15efeb61fe8c693840 100644 (file)
@@ -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'",