]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev-builtin-blkid: convert to OPTION macros
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Tue, 28 Apr 2026 11:24:36 +0000 (13:24 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Wed, 29 Apr 2026 12:00:41 +0000 (14:00 +0200)
Co-developed-by: Claude Opus 4.7 <noreply@anthropic.com>
src/udev/udev-builtin-blkid.c

index ca40b62d782b9d3119bc04bb2de1a48d66bcbdd8..16eaced0dcf4629837c402b3594040812955bf1a 100644 (file)
@@ -10,7 +10,6 @@
 #endif
 
 #include <fcntl.h>
-#include <getopt.h>
 #include <linux/loop.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
@@ -26,6 +25,7 @@
 #include "fd-util.h"
 #include "initrd-util.h"
 #include "gpt.h"
+#include "options.h"
 #include "parse-util.h"
 #include "string-util.h"
 #include "strv.h"
@@ -506,13 +506,6 @@ static int builtin_blkid(UdevEvent *event, int argc, char *argv[]) {
         int64_t offset = 0;
         int r;
 
-        static const struct option options[] = {
-                { "offset", required_argument, NULL, 'o' },
-                { "hint",   required_argument, NULL, 'H' },
-                { "noraid", no_argument,       NULL, 'R' },
-                {}
-        };
-
         r = dlopen_libblkid(LOG_DEBUG);
         if (r < 0)
                 return log_device_debug_errno(dev, r, "blkid not available: %m");
@@ -522,32 +515,32 @@ static int builtin_blkid(UdevEvent *event, int argc, char *argv[]) {
         if (!pr)
                 return log_device_debug_errno(dev, errno_or_else(ENOMEM), "Failed to create blkid prober: %m");
 
-        for (;;) {
-                int option;
+        OptionParser opts = { argc, argv, .namespace = "udev-builtin-blkid" };
 
-                option = getopt_long(argc, argv, "o:H:R", options, NULL);
-                if (option == -1)
-                        break;
+        FOREACH_OPTION(c, &opts, /* on_error= */ return c)
+                switch (c) {
+
+                OPTION_NAMESPACE("udev-builtin-blkid"): {}
 
-                switch (option) {
-                case 'H':
+                OPTION('H', "hint", "HINT", NULL):
                         errno = 0;
-                        r = sym_blkid_probe_set_hint(pr, optarg, 0);
+                        r = sym_blkid_probe_set_hint(pr, opts.arg, 0);
                         if (r < 0)
-                                return log_device_error_errno(dev, errno_or_else(ENOMEM), "Failed to use '%s' probing hint: %m", optarg);
+                                return log_device_error_errno(dev, errno_or_else(ENOMEM), "Failed to use '%s' probing hint: %m", opts.arg);
                         break;
-                case 'o':
-                        r = safe_atoi64(optarg, &offset);
+
+                OPTION('o', "offset", "OFFSET", NULL):
+                        r = safe_atoi64(opts.arg, &offset);
                         if (r < 0)
-                                return log_device_error_errno(dev, r, "Failed to parse '%s' as an integer: %m", optarg);
+                                return log_device_error_errno(dev, r, "Failed to parse '%s' as an integer: %m", opts.arg);
                         if (offset < 0)
                                 return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Invalid offset %"PRIi64".", offset);
                         break;
-                case 'R':
+
+                OPTION('R', "noraid", NULL, NULL):
                         noraid = true;
                         break;
                 }
-        }
 
         r = sd_device_get_devname(dev, &devnode);
         if (r < 0)