]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udevadm-test-builtin: modernize code
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 21 Aug 2018 07:08:54 +0000 (16:08 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 10 Sep 2018 09:27:36 +0000 (18:27 +0900)
src/udev/udevadm-test-builtin.c

index 24eb3e75eeb2695de334ade5f64e208556273191..ac1a8c2d7da8505c9b67a5edd34ee93faacc179f 100644 (file)
 #include "string-util.h"
 #include "udev.h"
 #include "udevadm.h"
-#include "udevadm-util.h"
 
-static void help(void) {
+static const char *arg_command = NULL;
+static char arg_syspath[UTIL_PATH_SIZE] = {};
+
+static int help(void) {
         printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n"
                "Test a built-in command.\n\n"
                "  -h --help     Print this message\n"
@@ -21,79 +23,84 @@ static void help(void) {
                , program_invocation_short_name);
 
         udev_builtin_list();
+
+        return 0;
 }
 
-int builtin_main(int argc, char *argv[], void *userdata) {
+static int parse_argv(int argc, char *argv[]) {
         static const struct option options[] = {
                 { "version", no_argument, NULL, 'V' },
                 { "help",    no_argument, NULL, 'h' },
                 {}
         };
-        char *command = NULL;
-        char *syspath = NULL;
-        char filename[UTIL_PATH_SIZE];
-        struct udev_device *dev = NULL;
-        enum udev_builtin_cmd cmd;
-        int rc = EXIT_SUCCESS, c;
 
-        log_set_max_level(LOG_DEBUG);
+        const char *s;
+        int c;
 
         while ((c = getopt_long(argc, argv, "Vh", options, NULL)) >= 0)
                 switch (c) {
                 case 'V':
-                        print_version();
-                        goto out;
+                        return version();
                 case 'h':
-                        help();
-                        goto out;
+                        return help();
+                case '?':
+                        return -EINVAL;
+                default:
+                        assert_not_reached("Unknown option");
                 }
 
-        command = argv[optind++];
-        if (command == NULL) {
-                fprintf(stderr, "command missing\n");
-                help();
-                rc = 2;
-                goto out;
+        arg_command = argv[optind++];
+        if (!arg_command) {
+                log_error("Command missing.");
+                return -EINVAL;
         }
 
-        syspath = argv[optind++];
-        if (syspath == NULL) {
-                fprintf(stderr, "syspath missing\n");
-                rc = 3;
-                goto out;
+        s = argv[optind++];
+        if (!s) {
+                log_error("syspath missing.");
+                return -EINVAL;
         }
 
+        /* add /sys if needed */
+        if (!path_startswith(s, "/sys"))
+                strscpyl(arg_syspath, sizeof(arg_syspath), "/sys", s, NULL);
+        else
+                strscpy(arg_syspath, sizeof(arg_syspath), s);
+
+        return 1;
+}
+
+int builtin_main(int argc, char *argv[], void *userdata) {
+        _cleanup_(udev_device_unrefp) struct udev_device *dev = NULL;
+        enum udev_builtin_cmd cmd;
+        int r;
+
+        log_set_max_level(LOG_DEBUG);
+
+        r = parse_argv(argc, argv);
+        if (r <= 0)
+                return r;
+
         udev_builtin_init();
 
-        cmd = udev_builtin_lookup(command);
+        cmd = udev_builtin_lookup(arg_command);
         if (cmd >= UDEV_BUILTIN_MAX) {
-                fprintf(stderr, "unknown command '%s'\n", command);
-                help();
-                rc = 5;
-                goto out;
+                log_error("Unknown command '%s'", arg_command);
+                r = -EINVAL;
+                goto finish;
         }
 
-        /* add /sys if needed */
-        if (!path_startswith(syspath, "/sys"))
-                strscpyl(filename, sizeof(filename), "/sys", syspath, NULL);
-        else
-                strscpy(filename, sizeof(filename), syspath);
-        delete_trailing_chars(filename, "/");
-
-        dev = udev_device_new_from_syspath(NULL, filename);
-        if (dev == NULL) {
-                fprintf(stderr, "unable to open device '%s'\n\n", filename);
-                rc = 4;
-                goto out;
+        dev = udev_device_new_from_syspath(NULL, arg_syspath);
+        if (!dev) {
+                r = log_error_errno(errno, "Failed to open device '%s'", arg_syspath);
+                goto finish;
         }
 
-        rc = udev_builtin_run(dev, cmd, command, true);
-        if (rc < 0) {
-                fprintf(stderr, "error executing '%s', exit code %i\n\n", command, rc);
-                rc = 6;
-        }
-out:
-        udev_device_unref(dev);
+        r = udev_builtin_run(dev, cmd, arg_command, true);
+        if (r < 0)
+                log_debug("error executing '%s', exit code %i", arg_command, r);
+
+finish:
         udev_builtin_exit();
-        return rc;
+        return r;
 }