From 41f59d8bf427d97a26816810be71e39492b55f1e Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Mon, 1 May 2006 16:51:42 +0200 Subject: [PATCH] [PATCH] splice-in: accept block device as input --- splice-in.c | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/splice-in.c b/splice-in.c index 14b31f1..3c01cc3 100644 --- a/splice-in.c +++ b/splice-in.c @@ -7,18 +7,40 @@ #include #include #include +#include #include "splice.h" +#ifndef BLKGETSIZE64 +#define BLKGETSIZE64 _IOR(0x12,114,size_t) +#endif + static int usage(char *name) { fprintf(stderr, "%s: infile | ...\n", name); return 1; } -int main(int argc, char *argv[]) +static long long in_size(int fd) { + unsigned long long bytes; struct stat sb; + + if (fstat(fd, &sb) < 0) + return error("fstat"); + + if (sb.st_size) + return sb.st_size; + + if (ioctl(fd, BLKGETSIZE64, &bytes) < 0) + return error("BLKGETSIZE64"); + + return bytes; +} + +int main(int argc, char *argv[]) +{ + long long isize; int fd; if (argc < 2) @@ -31,19 +53,20 @@ int main(int argc, char *argv[]) if (fd < 0) return error("open input"); - if (fstat(fd, &sb) < 0) - return error("stat input"); + isize = in_size(fd); + if (isize < 0) + return isize; - do { - int ret = splice(fd, NULL, STDOUT_FILENO, NULL, sb.st_size, 0); + while (isize) { + int ret = splice(fd, NULL, STDOUT_FILENO, NULL, isize, 0); if (ret < 0) return error("splice"); else if (!ret) break; - sb.st_size -= ret; - } while (1); + isize -= ret; + } close(fd); return 0; -- 2.25.1