mirror of
https://github.com/lunarmodules/luafilesystem.git
synced 2026-07-22 11:03:07 +00:00
Merge pull request #52 from siffiejoe/checkfile
Fix detection of closed files on Lua 5.2/5.3.
This commit is contained in:
16
src/lfs.c
16
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
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user