From eb202135397e457baa58423faec4d8d0441c2292 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Thu, 24 Nov 2011 15:59:52 +0100 Subject: lua: accept both file descriptor or filename as parameter --- lua-uniso.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'lua-uniso.c') diff --git a/lua-uniso.c b/lua-uniso.c index 97a1e8c..ec1cb91 100644 --- a/lua-uniso.c +++ b/lua-uniso.c @@ -1,4 +1,9 @@ +#include +#include +#include +#include + #include #include #include @@ -33,9 +38,23 @@ static void l_callback(size_t current, size_t total, const char *filename, static int l_uniso(lua_State *L) { - int fd = luaL_checkinteger(L, 1); - int result; + int fd, result; struct l_uniso_context ctx; + const char *filename = NULL; + + if (lua_isnumber(L, 1)) { + fd =luaL_checkinteger(L, 1); + } else if (lua_isstring(L, 1)) { + filename = luaL_checkstring(L, 1); + fd = open(filename, O_RDONLY); + if (fd < 0) { + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); + return 2; + } + } else { + luaL_typerror(L, 1, "integer or string"); + } if (!lua_isnil(L, 2)) luaL_checktype(L, 2, LUA_TFUNCTION); @@ -43,6 +62,9 @@ static int l_uniso(lua_State *L) ctx.L = L; result = uniso(fd, &l_callback, &ctx); + if (filename != NULL) + close(fd); + lua_pushinteger(L, result); return 1; } -- cgit v1.2.3