2b283706093e999520d03bab8af760ccd4b61cd1
2 * nixio - Linux I/O library for lua
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include <sys/sendfile.h>
25 /* guess what sucks... */
28 #include <sys/syscall.h>
29 ssize_t
splice(int __fdin
, __off64_t
*__offin
, int __fdout
,
30 __off64_t
*__offout
, size_t __len
, unsigned int __flags
) {
31 return syscall(__NR_splice
, __fdin
, __offin
, __fdout
, __offout
, __len
, __flags
);
33 #endif /* __UCLIBC__ */
36 * Checks whether a flag is set in the table and translates it into a bitmap
38 static void nixio_splice_flags__w(lua_State
*L
, int *m
, int f
, const char *t
) {
41 if (lua_toboolean(L
, -1)) {
48 * Translate integer to poll flags and vice versa
50 static int nixio_splice_flags(lua_State
*L
) {
53 luaL_checktype(L
, 1, LUA_TTABLE
);
55 nixio_splice_flags__w(L
, &flags
, SPLICE_F_MOVE
, "move");
56 nixio_splice_flags__w(L
, &flags
, SPLICE_F_NONBLOCK
, "nonblock");
57 nixio_splice_flags__w(L
, &flags
, SPLICE_F_MORE
, "more");
58 lua_pushinteger(L
, flags
);
64 * splice(fd_in, fd_out, length, flags)
66 static int nixio_splice(lua_State
*L
) {
67 int fd_in
= nixio__checkfd(L
, 1);
68 int fd_out
= nixio__checkfd(L
, 2);
69 size_t len
= luaL_checkinteger(L
, 3);
70 int flags
= luaL_optinteger(L
, 4, 0);
73 long spliced
= splice(fd_in
, NULL
, fd_out
, NULL
, len
, flags
);
76 return nixio__perror(L
);
79 lua_pushnumber(L
, spliced
);
84 * sendfile(outfd, infd, length)
86 static int nixio_sendfile(lua_State
*L
) {
87 int sockfd
= nixio__checksockfd(L
);
88 int infd
= nixio__checkfd(L
, 2);
89 size_t len
= luaL_checkinteger(L
, 3);
91 long spliced
= sendfile(sockfd
, infd
, NULL
, len
);
94 return nixio__perror(L
);
97 lua_pushnumber(L
, spliced
);
102 static const luaL_reg R
[] = {
103 {"splice", nixio_splice
},
104 {"splice_flags", nixio_splice_flags
},
105 {"sendfile", nixio_sendfile
},
109 void nixio_open_splice(lua_State
*L
) {
110 luaL_register(L
, NULL
, R
);