lua

A copy of the Lua development repository
Log | Files | Refs | README

commit f63d7753b89e479b20d6d78d05d1039c2ccd2e06
parent 50a82ec1b9adafa108756077b018925131f131e8
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Tue, 30 May 2000 15:54:54 -0300

files are closed when collected (again)

Diffstat:
Mliolib.c | 16++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/liolib.c b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 1.64 2000/05/24 13:54:49 roberto Exp roberto $ +** $Id: liolib.c,v 1.65 2000/05/26 19:17:57 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -160,7 +160,7 @@ static void setreturn (lua_State *L, IOCtrl *ctrl, FILE *f, int inout) { static int closefile (lua_State *L, IOCtrl *ctrl, FILE *f) { - if (f == stdin || f == stdout) + if (f == stdin || f == stdout || f == stderr) return 1; else { if (f == ctrl->file[INFILE]) @@ -180,6 +180,14 @@ static void io_close (lua_State *L) { } +static void file_collect (lua_State *L) { + IOCtrl *ctrl = (IOCtrl *)lua_getuserdata(L, lua_getparam(L, 1)); + FILE *f = getnonullfile(L, ctrl, 2); + if (f != stdin && f != stdout && f != stderr) + CLOSEFILE(L, f); +} + + static void io_open (lua_State *L) { IOCtrl *ctrl = (IOCtrl *)lua_getuserdata(L, lua_getparam(L, 1)); FILE *f = fopen(luaL_check_string(L, 2), luaL_check_string(L, 3)); @@ -649,6 +657,10 @@ static void openwithcontrol (lua_State *L) { lua_pushusertag(L, ctrl, ctrltag); lua_pushcclosure(L, ctrl_collect, 1); lua_settagmethod(L, ctrltag, "gc"); + /* close files when collected */ + lua_pushusertag(L, ctrl, ctrltag); + lua_pushcclosure(L, file_collect, 1); + lua_settagmethod(L, ctrl->iotag, "gc"); } void lua_iolibopen (lua_State *L) {