diff --git a/src/lfs.c b/src/lfs.c index 020267d..8b4cfd2 100644 --- a/src/lfs.c +++ b/src/lfs.c @@ -197,15 +197,23 @@ static int get_dir (lua_State *L) { ** Check if the given element on the stack is a file and returns it. */ static FILE *check_file (lua_State *L, int idx, const char *funcname) { +#if LUA_VERSION_NUM == 501 FILE **fh = (FILE **)luaL_checkudata (L, idx, "FILE*"); - if (fh == NULL) { - luaL_error (L, "%s: not a file", funcname); - return 0; - } else if (*fh == NULL) { + if (*fh == NULL) { luaL_error (L, "%s: closed file", funcname); return 0; } else return *fh; +#elif LUA_VERSION_NUM >= 502 && LUA_VERSION_NUM <= 503 + luaL_Stream *fh = (luaL_Stream *)luaL_checkudata (L, idx, "FILE*"); + if (fh->closef == 0 || fh->f == NULL) { + luaL_error (L, "%s: closed file", funcname); + return 0; + } else + return fh->f; +#else +#error unsupported Lua version +#endif }