Fix various compile warnings
[splice.git] / splice.h
1 #ifndef SPLICE_H
2 #define SPLICE_H
3
4 #include <errno.h>
5 #include <sys/uio.h>
6 #include <sys/stat.h>
7 #include <linux/unistd.h>
8
9 #if defined(__i386__)
10 #define __NR_sys_splice         313
11 #define __NR_sys_tee            315
12 #define __NR_sys_vmsplice       316
13 #elif defined(__x86_64__)
14 #define __NR_sys_splice         275
15 #define __NR_sys_tee            276
16 #define __NR_sys_vmsplice       278
17 #elif defined(__powerpc__) || defined(__powerpc64__)
18 #define __NR_sys_splice         283
19 #define __NR_sys_tee            284
20 #define __NR_sys_vmsplice       285
21 #elif defined(__ia64__)
22 #define __NR_sys_splice         1297
23 #define __NR_sys_tee            1301
24 #define __NR_sys_vmsplice       1302
25 #else
26 #error unsupported arch
27 #endif
28
29 #ifndef SPLICE_F_MOVE
30
31 #define SPLICE_F_MOVE   (0x01)  /* move pages instead of copying */
32 #define SPLICE_F_NONBLOCK (0x02) /* don't block on the pipe splicing (but */
33                                  /* we may still block on the fd we splice */
34                                  /* from/to, of course */
35 #define SPLICE_F_MORE   (0x04)  /* expect more data */
36 #define SPLICE_F_GIFT   (0x08)  /* pages passed in are a gift */
37
38 #endif /* SPLICE_F_MOVE defined */
39
40 /*
41  * SPLICE_F_UNMAP was introduced later, so check for that seperately
42  */
43 #ifndef SPLICE_F_UNMAP
44 #define SPLICE_F_UNMAP  (0x10)  /* undo vmsplice map */
45 #endif
46
47 static inline int ssplice(int fdin, loff_t *off_in, int fdout, loff_t *off_out,
48                           size_t len, unsigned int flags)
49 {
50         
51         return syscall(__NR_sys_splice, fdin, off_in, fdout, off_out, len, flags);
52 }
53
54 static inline int stee(int fdin, int fdout, size_t len, unsigned int flags)
55 {
56         return syscall(__NR_sys_tee, fdin, fdout, len, flags);
57 }
58
59 static inline int svmsplice(int fd, const struct iovec *iov,
60                             unsigned long nr_segs, unsigned int flags)
61 {
62         return syscall(__NR_sys_vmsplice, fd, iov, nr_segs, flags);
63 }
64
65 #define SPLICE_SIZE     (64*1024)
66
67 #define BUG_ON(c) assert(!(c))
68
69 #define min(x,y) ({ \
70         typeof(x) _x = (x);     \
71         typeof(y) _y = (y);     \
72         (void) (&_x == &_y);            \
73         _x < _y ? _x : _y; })
74
75 #define max(x,y) ({ \
76         typeof(x) _x = (x);     \
77         typeof(y) _y = (y);     \
78         (void) (&_x == &_y);            \
79         _x > _y ? _x : _y; })
80
81 static inline int error(const char *n)
82 {
83         perror(n);
84         return -1;
85 }
86
87 static int __check_pipe(int pfd)
88 {
89         struct stat sb;
90
91         if (fstat(pfd, &sb) < 0)
92                 return error("stat");
93         if (!S_ISFIFO(sb.st_mode))
94                 return 1;
95
96         return 0;
97 }
98
99 static inline int check_input_pipe(void)
100 {
101         if (!__check_pipe(STDIN_FILENO))
102                 return 0;
103
104         fprintf(stderr, "stdin must be a pipe\n");
105         return 1;
106 }
107
108 static inline int check_output_pipe(void)
109 {
110         if (!__check_pipe(STDOUT_FILENO))
111                 return 0;
112
113         fprintf(stderr, "stdout must be a pipe\n");
114         return 1;
115 }
116
117 #endif