summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README8
-rw-r--r--lua-uniso.c26
2 files changed, 32 insertions, 2 deletions
diff --git a/README b/README
index 1393d64..00e0bde 100644
--- a/README
+++ b/README
@@ -18,4 +18,12 @@ To install:
make install
+Example how to use Lua module:
+
+require("uniso")
+
+-- Use 0 for stdin
+print(uniso.uniso(arg[1] or 0, function(current, total, filename)
+ print(current, total, filename)
+end))
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 <fcntl.h>
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
@@ -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;
}