/* ** function to (not) close the standard files stdin, stdout, and stderr */ static int io_noclose (lua_State *L) { LStream *p = tolstream(L); p->closef = &io_noclose; /* keep file opened */ lua_pushnil(L); lua_pushliteral(L, "cannot close standard file"); return 2; }
static FILE *tofile (lua_State *L) { LStream *p = tolstream(L); if (isclosed(p)) luaL_error(L, "attempt to use a closed file"); lua_assert(p->f); return p->f; }
static int f_tostring(lua_State *L) { LStream *p = tolstream(L); if (isclosed(p)) lua_pushliteral(L, "file (closed)"); else lua_pushfstring(L, "file (%p)", p->f); return 1; }
static int f_gc (lua_State *L) { #if !defined(USE_FATFS) LStream *p = tolstream(L); if (!isclosed(p) && p->f != NULL) aux_close(L); /* ignore closed and incompletely open files */ #endif return 0; }
static int aux_close (lua_State *L) { #if !defined(USE_FATFS) LStream *p = tolstream(L); lua_CFunction cf = p->closef; p->closef = NULL; /* mark stream as closed */ return (*cf)(L); /* close it */ #else return 0; #endif }
static FILE *tofile (lua_State *L) { LStream *p = tolstream(L); #if defined(USE_FATFS) return &p->f; #else if (isclosed(p)) luaL_error(L, "attempt to use a closed file"); lua_assert(p->f); return p->f; #endif }
/* ** function to close 'popen' files */ static int io_pclose(lua_State *L) { LStream *p = tolstream(L); return luaL_execresult(L, lua_pclose(L, p->f)); }
/* ** function to close regular files */ static int io_fclose(lua_State *L) { LStream *p = tolstream(L); int res = fclose(p->f); return luaL_fileresult(L, (res == 0), NULL); }
static int f_gc(lua_State *L) { LStream *p = tolstream(L); if (!isclosed(p) && p->f != NULL) aux_close(L); /* ignore closed and incompletely open files */ return 0; }
static int aux_close(lua_State *L) { LStream *p = tolstream(L); lua_CFunction cf = p->closef; p->closef = NULL; /* mark stream as closed */ return (*cf)(L); /* close it */ }
static int io_read (lua_State *L) { LStream *p = tolstream(L); size_t l = (size_t)lua_tointeger(L, 2); read_chars(L, &p->f, l); return 1; }