libs/web: remove debugging code
[project/luci.git] / libs / nixio / src / poll.c
index fdec2caaf0245db5aedba43ca3e9497ada4ebd99..1211bc72ac392471a70586d10a2c4279773f6f07 100644 (file)
  */
 
 #include "nixio.h"
-#include <poll.h>
 #include <time.h>
 #include <errno.h>
 #include <string.h>
 #include <stdlib.h>
-#include "nixio.h"
+#include <sys/time.h>
+
+
+static int nixio_gettimeofday(lua_State *L) {
+       struct timeval tv;
+       gettimeofday(&tv, NULL);
+       nixio__pushnumber(L, tv.tv_sec);
+       nixio__pushnumber(L, tv.tv_usec);
+       return 2;
+}
 
 
 /**
@@ -71,11 +79,13 @@ static int nixio_poll_flags(lua_State *L) {
                flags = luaL_checkinteger(L, 1);
                lua_newtable(L);
                nixio_poll_flags__r(L, &flags, POLLIN, "in");
-               nixio_poll_flags__r(L, &flags, POLLPRI, "pri");
                nixio_poll_flags__r(L, &flags, POLLOUT, "out");
                nixio_poll_flags__r(L, &flags, POLLERR, "err");
+#ifndef __WINNT__
+               nixio_poll_flags__r(L, &flags, POLLPRI, "pri");
                nixio_poll_flags__r(L, &flags, POLLHUP, "hup");
                nixio_poll_flags__r(L, &flags, POLLNVAL, "nval");
+#endif
         } else {
                flags = 0;
                const int j = lua_gettop(L);
@@ -83,16 +93,22 @@ static int nixio_poll_flags(lua_State *L) {
                        const char *flag = luaL_checkstring(L, i);
                        if (!strcmp(flag, "in")) {
                                flags |= POLLIN;
-                       } else if (!strcmp(flag, "pri")) {
-                               flags |= POLLPRI;
                        } else if (!strcmp(flag, "out")) {
                                flags |= POLLOUT;
                        } else if (!strcmp(flag, "err")) {
                                flags |= POLLERR;
+                       } else if (!strcmp(flag, "pri")) {
+#ifndef __WINNT__
+                               flags |= POLLPRI;
+#endif
                        } else if (!strcmp(flag, "hup")) {
+#ifndef __WINNT__
                                flags |= POLLHUP;
+#endif
                        } else if (!strcmp(flag, "nval")) {
+#ifndef __WINNT__
                                flags |= POLLNVAL;
+#endif
                        } else {
                                return luaL_argerror(L, i,
                                 "supported values: in, pri, out, err, hup, nval");
@@ -114,11 +130,19 @@ static int nixio_poll(lua_State *L) {
 
        /* we are being abused as sleep() replacement... */
        if (lua_isnoneornil(L, 1) || len < 1) {
-               return nixio__pstatus(L, !poll(NULL, 0, timeout));
+               if (!poll(NULL, 0, timeout)) {
+                       lua_pushinteger(L, 0);
+                       return 1;
+               } else {
+                       return nixio__perror(L);
+               }
        }
 
        luaL_checktype(L, 1, LUA_TTABLE);
        struct pollfd *fds = calloc(len, sizeof(struct pollfd));
+       if (!fds) {
+               return luaL_error(L, NIXIO_OOM);
+       }
 
        for (i = 0; i < len; i++) {
                lua_rawgeti(L, 1, i+1);
@@ -145,7 +169,11 @@ static int nixio_poll(lua_State *L) {
 
        status = poll(fds, (nfds_t)len, timeout);
 
-       if (status < 1) {
+       if (status == 0) {
+               free(fds);
+               lua_pushboolean(L, 0);
+               return 1;
+       } else if (status < 0) {
                free(fds);
                return nixio__perror(L);
        }
@@ -170,6 +198,7 @@ static int nixio_poll(lua_State *L) {
 
 /* module table */
 static const luaL_reg R[] = {
+       {"gettimeofday", nixio_gettimeofday},
        {"nanosleep",   nixio_nanosleep},
        {"poll",                nixio_poll},
        {"poll_flags",  nixio_poll_flags},