static FILE *getiofile (lua_State *L, int findex) { FILE *f; LUA_IO_GETFIELD(findex); f = *(FILE **)lua_touserdata(L, -1); if (f == NULL) luaL_error(L, "standard %s file is closed", fnames[findex - 1]); return f; }
static int getiofile (lua_State *L, int findex) { int *pf; LUA_IO_GETFIELD(findex); pf = (int *)lua_touserdata(L, -1); if (pf == NULL || *pf == FS_OPEN_OK - 1){ luaL_error(L, "default %s file is closed", fnames[findex - 1]); return FS_OPEN_OK - 1; } return *pf; }
static int io_lines (lua_State *L) { if (lua_isnoneornil(L, 1)) { /* no arguments? */ /* will iterate over default input */ LUA_IO_GETFIELD(IO_INPUT); return f_lines(L); } else { const char *filename = luaL_checkstring(L, 1); FILE **pf = newfile(L); *pf = fopen(filename, "r"); if (*pf == NULL) fileerror(L, 1, filename); aux_lines(L, lua_gettop(L), 1); return 1; } }
static int g_iofile (lua_State *L, int f, const char *mode) { if (!lua_isnoneornil(L, 1)) { const char *filename = lua_tostring(L, 1); if (filename) { FILE **pf = newfile(L); *pf = fopen(filename, mode); if (*pf == NULL) fileerror(L, 1, filename); } else { tofile(L); /* check that it's a valid file handle */ lua_pushvalue(L, 1); } LUA_IO_SETFIELD(f); } /* return current value */ LUA_IO_GETFIELD(f); return 1; }
static int io_close (lua_State *L) { if (lua_isnone(L, 1)) LUA_IO_GETFIELD(IO_OUTPUT); tofile(L); /* make sure argument is a file */ return aux_close(L); }