static int io_gc (lua_State *L) { FILE *f = *tofilep(L); /* ignore closed files */ if (f != NULL) aux_close(L); return 0; }
/* ** function to close 'popen' files */ static int io_pclose(lua_State *L) { FILE **p = tofilep(L); int ok = lua_pclose(L, *p); *p = NULL; return pushresult(L, ok, NULL); }
static FILE *tofile(lua_State *L) { FILE **f = tofilep(L); if (*f == NULL) luaL_error(L, "attempt to use a closed file"); return *f; }
/* ** function to close regular files */ static int io_fclose(lua_State *L) { FILE **p = tofilep(L); int ok = (fclose(*p) == 0); *p = NULL; return pushresult(L, ok, NULL); }
static int io_gc (lua_State *L) { int f = *tofilep(L); /* ignore closed files */ if (f != FS_OPEN_OK - 1) aux_close(L); return 0; }
static int io_tostring (lua_State *L) { FILE *f = *tofilep(L); if (f == NULL) lua_pushliteral(L, "file (closed)"); else lua_pushfstring(L, "file (%p)", f); return 1; }
static int io_tostring (lua_State *L) { int f = *tofilep(L); if (f == FS_OPEN_OK - 1) lua_pushliteral(L, "file (closed)"); else lua_pushfstring(L, "file (%i)", f); return 1; }
static int io_close(lua_State *L) { FILE **p = tofilep(L); if (*p != NULL){ int ok = (fclose(*p) == 0); *p = NULL; return pushresult(L, ok, NULL); } else { return 0; } }
/* ** function to close regular files and tcp sockets */ static int io_fclose (lua_State *L) { FILE **p = tofilep(L); #ifdef NUTLUA_IOLIB_TCP NUTFILE *nf = (NUTFILE *) (uintptr_t) _fileno(*p); #endif int ok = (fclose(*p) == 0); #ifdef NUTLUA_IOLIB_TCP if (nf->nf_dev == NULL) { NutTcpCloseSocket((TCPSOCKET *) nf); } #endif *p = NULL; return pushresult(L, ok, NULL); }
static int aux_close (lua_State *L) { #if LUA_OPTIMIZE_MEMORY != 2 lua_getfenv(L, 1); lua_getfield(L, -1, "__close"); return (lua_tocfunction(L, -1))(L); #else FILE **p = tofilep(L); if (*p == stdin || *p == stdout || *p == stderr) { lua_pushnil(L); lua_pushliteral(L, "cannot close standard file"); return 2; } int ok = (fclose(*p) == 0); *p = NULL; return pushresult(L, ok, NULL); #endif }
/* ** function to close 'popen' files */ static int pipe_close (lua_State *L) { FILE **p = tofilep(L); int ok = fclose(*p); *p = NULL; return pushresult(L, ok, NULL); }
static int tofile (lua_State *L) { int *f = tofilep(L); if (*f < FS_OPEN_OK) luaL_error(L, "attempt to use a closed file"); return *f; }
/* ** function to close regular files */ static int io_fclose (lua_State *L) { int *p = tofilep(L); int ok = (fs_close(*p) == 0); *p = FS_OPEN_OK - 1; return pushresult(L, ok, NULL); }
/* ** function to close 'popen' files */ static int io_pclose (lua_State *L) { int *p = tofilep(L); int ok = lua_pclose(L, *p); *p = FS_OPEN_OK - 1; return pushresult(L, ok, NULL); }