]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev/cdrom: split out parse_argv() and help()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 24 Dec 2020 05:59:30 +0000 (14:59 +0900)
committerLuca Boccassi <luca.boccassi@gmail.com>
Fri, 8 Jan 2021 14:08:38 +0000 (14:08 +0000)
src/udev/cdrom_id/cdrom_id.c

index c0071d91fc2824e303c72808656656b305000cab..14b204ee4534479208065334a499d372e27bc6ae 100644 (file)
 #include "random-util.h"
 #include "udev-util.h"
 
+static bool arg_eject = false;
+static bool arg_lock = false;
+static bool arg_unlock = false;
+static const char *arg_node = NULL;
+
 /* device info */
 static unsigned cd_rw_nonremovable;
 static unsigned cd_rw_removable;
@@ -990,44 +995,40 @@ static int cd_media_toc(int fd) {
         return 0;
 }
 
-int main(int argc, char *argv[]) {
+static int help(void) {
+        printf("Usage: %s [options] <device>\n"
+               "  -l --lock-media    lock the media (to enable eject request events)\n"
+               "  -u --unlock-media  unlock the media\n"
+               "  -e --eject-media   eject the media\n"
+               "  -d --debug         print debug messages to stderr\n"
+               "  -h --help          print this help text\n"
+               "\n",
+               program_invocation_short_name);
+
+        return 0;
+}
+
+static int parse_argv(int argc, char *argv[]) {
         static const struct option options[] = {
-                { "lock-media", no_argument, NULL, 'l' },
+                { "lock-media",   no_argument, NULL, 'l' },
                 { "unlock-media", no_argument, NULL, 'u' },
-                { "eject-media", no_argument, NULL, 'e' },
-                { "debug", no_argument, NULL, 'd' },
-                { "help", no_argument, NULL, 'h' },
+                { "eject-media",  no_argument, NULL, 'e' },
+                { "debug",        no_argument, NULL, 'd' },
+                { "help",         no_argument, NULL, 'h' },
                 {}
         };
-        bool eject = false;
-        bool lock = false;
-        bool unlock = false;
-        const char *node = NULL;
-        int fd = -1;
-        int cnt;
-        int rc = 0;
-
-        log_set_target(LOG_TARGET_AUTO);
-        udev_parse_config();
-        log_parse_environment();
-        log_open();
-
-        for (;;) {
-                int option;
-
-                option = getopt_long(argc, argv, "deluh", options, NULL);
-                if (option == -1)
-                        break;
+        int c;
 
-                switch (option) {
+        while ((c = getopt_long(argc, argv, "deluh", options, NULL)) >= 0)
+                switch (c) {
                 case 'l':
-                        lock = true;
+                        arg_lock = true;
                         break;
                 case 'u':
-                        unlock = true;
+                        arg_unlock = true;
                         break;
                 case 'e':
-                        eject = true;
+                        arg_eject = true;
                         break;
                 case 'd':
                         log_set_target(LOG_TARGET_CONSOLE);
@@ -1035,24 +1036,32 @@ int main(int argc, char *argv[]) {
                         log_open();
                         break;
                 case 'h':
-                        printf("Usage: %s [options] <device>\n"
-                               "  -l,--lock-media    lock the media (to enable eject request events)\n"
-                               "  -u,--unlock-media  unlock the media\n"
-                               "  -e,--eject-media   eject the media\n"
-                               "  -d,--debug         debug to stderr\n"
-                               "  -h,--help          print this help text\n\n",
-                               program_invocation_short_name);
-                        goto exit;
+                        return help();
                 default:
-                        rc = 1;
-                        goto exit;
+                        assert_not_reached("Unknown option");
                 }
-        }
 
-        node = argv[optind];
-        if (!node) {
-                log_error("no device");
-                rc = 1;
+        arg_node = argv[optind];
+        if (!arg_node)
+                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No device is specified.");
+
+        return 1;
+}
+
+int main(int argc, char *argv[]) {
+        int fd = -1;
+        int cnt;
+        int rc = 0;
+        int r;
+
+        log_set_target(LOG_TARGET_AUTO);
+        udev_parse_config();
+        log_parse_environment();
+        log_open();
+
+        r = parse_argv(argc, argv);
+        if (r <= 0) {
+                rc = r < 0;
                 goto exit;
         }
 
@@ -1060,7 +1069,7 @@ int main(int argc, char *argv[]) {
         for (cnt = 20; cnt > 0; cnt--) {
                 struct timespec duration;
 
-                fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
+                fd = open(arg_node, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
                 if (fd >= 0 || errno != EBUSY)
                         break;
                 duration.tv_sec = 0;
@@ -1068,11 +1077,11 @@ int main(int argc, char *argv[]) {
                 nanosleep(&duration, NULL);
         }
         if (fd < 0) {
-                log_debug("unable to open '%s'", node);
+                log_debug("unable to open '%s'", arg_node);
                 rc = 1;
                 goto exit;
         }
-        log_debug("probing: '%s'", node);
+        log_debug("probing: '%s'", arg_node);
 
         /* same data as original cdrom_id */
         if (cd_capability_compat(fd) < 0) {
@@ -1102,17 +1111,17 @@ int main(int argc, char *argv[]) {
 
 work:
         /* lock the media, so we enable eject button events */
-        if (lock && cd_media) {
+        if (arg_lock && cd_media) {
                 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (lock)");
                 media_lock(fd, true);
         }
 
-        if (unlock && cd_media) {
+        if (arg_unlock && cd_media) {
                 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)");
                 media_lock(fd, false);
         }
 
-        if (eject) {
+        if (arg_eject) {
                 log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)");
                 media_lock(fd, false);
                 log_debug("START_STOP_UNIT (eject)");