Skip to content

Commit

Permalink
Merge pull request #52 from siffiejoe/checkfile
Browse files Browse the repository at this point in the history
Fix detection of closed files on Lua 5.2/5.3.
  • Loading branch information
hishamhm committed Jun 15, 2015
2 parents 5f8b646 + c315150 commit b6d5b37
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down

0 comments on commit b6d5b37

Please sign in to comment.