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.
22 #include <sys/select.h>
25 static int nixio_select(lua_State
*L
) {
26 int nfds
= 0, tmpfd
= -1, i
= 0, j
= 0, o
= 0, k
, tlen
;
27 fd_set rfds
, wfds
, xfds
;
28 fd_set
*fdsets
[3] = {&rfds
, &wfds
, &xfds
};
33 struct timeval timeout
;
34 timeout
.tv_sec
= luaL_optinteger(L
, 4, 0);
35 timeout
.tv_usec
= luaL_optinteger(L
, 5, 0);
40 if (lua_isnoneornil(L
, o
)) {
45 luaL_checktype(L
, o
, LUA_TTABLE
);
46 tlen
= lua_objlen(L
, o
);
47 luaL_argcheck(L
, tlen
<= FD_SETSIZE
, o
, "too many fds");
49 for (j
=1; j
<=tlen
; j
++) {
51 tmpfd
= nixio__checkfd(L
, -1);
52 FD_SET(tmpfd
, fdsets
[i
]);
60 int stat
= select(nfds
, fdsets
[0], fdsets
[1], fdsets
[2], &timeout
);
63 return nixio__perror(L
);
64 } else if (stat
== 0) {
65 lua_pushinteger(L
, stat
);
66 for (i
=1; i
<=3; i
++) {
67 if (lua_isnoneornil(L
, i
)) {
74 lua_pushinteger(L
, stat
);
76 /* create return tables */
79 if (lua_isnoneornil(L
, o
)) {
85 tlen
= lua_objlen(L
, o
);
88 for (j
=1; j
<=tlen
; j
++) {
90 tmpfd
= nixio__tofd(L
, -1);
91 if (FD_ISSET(tmpfd
, fdsets
[i
])) {
92 lua_rawseti(L
, -2, k
++);
103 static const luaL_reg R
[] = {
104 {"select", nixio_select
},
108 void nixio_open_select(lua_State
*L
) {
109 luaL_register(L
, NULL
, R
);