udev/v4l_id: convert to run() and add error messages

Also split out parse_argv(), use "ARG" instead of "<arg>".
Make the syntax help a bit more precise.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2023-03-13 18:14:55 +01:00
parent 560900be67
commit 6fecd78ca9
+29 -13
View File
@@ -27,50 +27,64 @@
#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.");
fd = open(device, O_RDONLY);
arg_device = argv[optind];
return 1;
}
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);