From f6b0ddc0b31f9ca96b9f4a334b3f84bc3e2f8cf9 Mon Sep 17 00:00:00 2001 From: Alexander Perlis Date: Thu, 1 Aug 2024 00:36:02 +0000 Subject: [PATCH] Support block devices in @-syntax --- src/main/options.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main/options.c b/src/main/options.c index fe8af2d..a10710a 100644 --- a/src/main/options.c +++ b/src/main/options.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #ifdef HAVE_GETOPT_H #include @@ -382,9 +383,7 @@ opts_t opts_parse(unsigned int argc, char **argv) rc = 0; memset(&sb, 0, sizeof(sb)); rc = stat(size_file, &sb); - if (0 == rc) { - opts->size = (off_t) (sb.st_size); - } else { + if (0 != rc) { /*@-mustfreefresh@ *//* see above */ fprintf(stderr, "%s: %s %s: %s\n", opts->program_name, @@ -393,6 +392,31 @@ opts_t opts_parse(unsigned int argc, char **argv) return NULL; /*@+mustfreefresh@ */ } + opts->size = (off_t) (sb.st_size); + if (0 == opts->size && S_ISBLK(sb.st_mode)) { + int fd = open(size_file, O_RDONLY); + if (fd < 0) { + /*@-mustfreefresh@ *//* see above */ + fprintf(stderr, "%s: %s %s: %s\n", + opts->program_name, + _("failed to open block device"), size_file, strerror(errno)); + opts_free(opts); + return NULL; + /*@+mustfreefresh@ */ + } + off_t size = lseek(fd, 0, SEEK_END); + close(fd); + if (size < 0) { + /*@-mustfreefresh@ *//* see above */ + fprintf(stderr, "%s: %s %s: %s\n", + opts->program_name, + _("failed to seek size of block device"), size_file, strerror(errno)); + opts_free(opts); + return NULL; + /*@+mustfreefresh@ */ + } + opts->size = size; + } } else { opts->size = pv_getnum_size(optarg, opts->decimal_units); }