Fix various compile warnings
[splice.git] / splice-in.c
CommitLineData
d8525fbd
JA
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>
41f59d8b 10#include <sys/ioctl.h>
d8525fbd
JA
11
12#include "splice.h"
13
41f59d8b
JA
14#ifndef BLKGETSIZE64
15#define BLKGETSIZE64 _IOR(0x12,114,size_t)
16#endif
17
f91353a3
JA
18static int usage(char *name)
19{
20 fprintf(stderr, "%s: infile | ...\n", name);
21 return 1;
22}
23
41f59d8b 24static long long in_size(int fd)
d8525fbd 25{
41f59d8b 26 unsigned long long bytes;
d8525fbd 27 struct stat sb;
41f59d8b
JA
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
41int main(int argc, char *argv[])
42{
43 long long isize;
d8525fbd
JA
44 int fd;
45
f91353a3
JA
46 if (argc < 2)
47 return usage(argv[0]);
48
76797253 49 if (check_output_pipe())
f91353a3 50 return usage(argv[0]);
d8525fbd
JA
51
52 fd = open(argv[1], O_RDONLY);
49b68ef7
JA
53 if (fd < 0)
54 return error("open input");
d8525fbd 55
41f59d8b
JA
56 isize = in_size(fd);
57 if (isize < 0)
58 return isize;
d8525fbd 59
41f59d8b 60 while (isize) {
13b72067 61 int ret = ssplice(fd, NULL, STDOUT_FILENO, NULL, isize, 0);
d8525fbd 62
49b68ef7
JA
63 if (ret < 0)
64 return error("splice");
65 else if (!ret)
d8525fbd
JA
66 break;
67
41f59d8b
JA
68 isize -= ret;
69 }
d8525fbd
JA
70
71 close(fd);
72 return 0;
73}