From 6fecd78ca9dd86675b40938103b5ca83ad17b156 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 13 Mar 2023 10:15:45 +0100 Subject: [PATCH] udev/v4l_id: convert to run() and add error messages Also split out parse_argv(), use "ARG" instead of "". Make the syntax help a bit more precise. --- src/udev/v4l_id/v4l_id.c | 42 +++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/udev/v4l_id/v4l_id.c b/src/udev/v4l_id/v4l_id.c index 4f163c43509..98075db0ce2 100644 --- a/src/udev/v4l_id/v4l_id.c +++ b/src/udev/v4l_id/v4l_id.c @@ -27,50 +27,64 @@ #include #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] \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); -- 2.47.3