]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev/v4l_id: convert to run() and add error messages
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 13 Mar 2023 09:15:45 +0000 (10:15 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 13 Mar 2023 17:14:55 +0000 (18:14 +0100)
Also split out parse_argv(), use "ARG" instead of "<arg>".
Make the syntax help a bit more precise.

src/udev/v4l_id/v4l_id.c

index 4f163c43509e66567bd120b1274084da9cae6fc0..98075db0ce23f93f04b3ceff7e8cdbe04cba513b 100644 (file)
 #include <linux/videodev2.h>
 
 #include "fd-util.h"
+#include "main-func.h"
 
-int main(int argc, char *argv[]) {
+static const char *arg_device = NULL;
+
+static int parse_argv(int argc, char *argv[]) {
         static const struct option options[] = {
                 { "help", no_argument, NULL, 'h' },
                 {}
         };
-        _cleanup_close_ int fd = -EBADF;
-        char *device;
-        struct v4l2_capability v2cap;
         int c;
 
         while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
-
                 switch (c) {
                 case 'h':
-                        printf("%s [-h,--help] <device file>\n\n"
+                        printf("%s [OPTIONS...] DEVICE\n\n"
                                "Video4Linux device identification.\n\n"
-                               "  -h  Print this message\n",
+                               "  -h --help     Show this help text\n",
                                program_invocation_short_name);
                         return 0;
                 case '?':
                         return -EINVAL;
-
                 default:
                         assert_not_reached();
                 }
 
-        device = argv[optind];
-        if (!device)
-                return 2;
+        if (!argv[optind])
+                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+                                       "DEVICE argument missing.");
+
+        arg_device = argv[optind];
+        return 1;
+}
 
-        fd = open(device, O_RDONLY);
+static int run(int argc, char *argv[]) {
+        _cleanup_close_ int fd = -EBADF;
+        struct v4l2_capability v2cap;
+        int r;
+
+        r = parse_argv(argc, argv);
+        if (r <= 0)
+                return r;
+
+        fd = open(arg_device, O_RDONLY);
         if (fd < 0)
-                return 3;
+                return log_error_errno(errno, "Failed to open %s: %m", arg_device);
 
         if (ioctl(fd, VIDIOC_QUERYCAP, &v2cap) == 0) {
                 int capabilities;
+
                 printf("ID_V4L_VERSION=2\n");
                 printf("ID_V4L_PRODUCT=%s\n", v2cap.card);
                 printf("ID_V4L_CAPABILITIES=:");
+
                 if (v2cap.capabilities & V4L2_CAP_DEVICE_CAPS)
                         capabilities = v2cap.device_caps;
                 else
                         capabilities = v2cap.capabilities;
+
                 if ((capabilities & V4L2_CAP_VIDEO_CAPTURE) > 0 ||
                     (capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) > 0)
                         printf("capture:");
@@ -90,3 +104,5 @@ int main(int argc, char *argv[]) {
 
         return 0;
 }
+
+DEFINE_MAIN_FUNCTION(run);