Fix various compile warnings
[splice.git] / splice-in.c
1 /*
2  * Splice argument file to stdout
3  */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/ioctl.h>
11
12 #include "splice.h"
13
14 #ifndef BLKGETSIZE64
15 #define BLKGETSIZE64    _IOR(0x12,114,size_t)
16 #endif
17
18 static int usage(char *name)
19 {
20         fprintf(stderr, "%s: infile | ...\n", name);
21         return 1;
22 }
23
24 static long long in_size(int fd)
25 {
26         unsigned long long bytes;
27         struct stat sb;
28
29         if (fstat(fd, &sb) < 0)
30                 return error("fstat");
31
32         if (sb.st_size)
33                 return sb.st_size;
34
35         if (ioctl(fd, BLKGETSIZE64, &bytes) < 0)
36                 return error("BLKGETSIZE64");
37
38         return bytes;
39 }
40
41 int main(int argc, char *argv[])
42 {
43         long long isize;
44         int fd;
45
46         if (argc < 2)
47                 return usage(argv[0]);
48
49         if (check_output_pipe())
50                 return usage(argv[0]);
51
52         fd = open(argv[1], O_RDONLY);
53         if (fd < 0)
54                 return error("open input");
55
56         isize = in_size(fd);
57         if (isize < 0)
58                 return isize;
59
60         while (isize) {
61                 int ret = ssplice(fd, NULL, STDOUT_FILENO, NULL, isize, 0);
62
63                 if (ret < 0)
64                         return error("splice");
65                 else if (!ret)
66                         break;
67
68                 isize -= ret;
69         }
70
71         close(fd);
72         return 0;
73 }