diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-05-30 15:55:16 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-05-30 15:55:16 -0300 |
| commit | f63d7753b89e479b20d6d78d05d1039c2ccd2e06 (patch) | |
| tree | de9d8fd23eb13868bba9fea669779fbc6a4f6a80 | |
| parent | 50a82ec1b9adafa108756077b018925131f131e8 (diff) | |
| download | lua-f63d7753b89e479b20d6d78d05d1039c2ccd2e06.tar.gz lua-f63d7753b89e479b20d6d78d05d1039c2ccd2e06.tar.bz2 lua-f63d7753b89e479b20d6d78d05d1039c2ccd2e06.zip | |
files are closed when collected (again)
Diffstat (limited to '')
| -rw-r--r-- | liolib.c | 16 |
1 files changed, 14 insertions, 2 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 1.64 2000/05/24 13:54:49 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.65 2000/05/26 19:17:57 roberto Exp roberto $ |
| 3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -160,7 +160,7 @@ static void setreturn (lua_State *L, IOCtrl *ctrl, FILE *f, int inout) { | |||
| 160 | 160 | ||
| 161 | 161 | ||
| 162 | static int closefile (lua_State *L, IOCtrl *ctrl, FILE *f) { | 162 | static int closefile (lua_State *L, IOCtrl *ctrl, FILE *f) { |
| 163 | if (f == stdin || f == stdout) | 163 | if (f == stdin || f == stdout || f == stderr) |
| 164 | return 1; | 164 | return 1; |
| 165 | else { | 165 | else { |
| 166 | if (f == ctrl->file[INFILE]) | 166 | if (f == ctrl->file[INFILE]) |
| @@ -180,6 +180,14 @@ static void io_close (lua_State *L) { | |||
| 180 | } | 180 | } |
| 181 | 181 | ||
| 182 | 182 | ||
| 183 | static void file_collect (lua_State *L) { | ||
| 184 | IOCtrl *ctrl = (IOCtrl *)lua_getuserdata(L, lua_getparam(L, 1)); | ||
| 185 | FILE *f = getnonullfile(L, ctrl, 2); | ||
| 186 | if (f != stdin && f != stdout && f != stderr) | ||
| 187 | CLOSEFILE(L, f); | ||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 183 | static void io_open (lua_State *L) { | 191 | static void io_open (lua_State *L) { |
| 184 | IOCtrl *ctrl = (IOCtrl *)lua_getuserdata(L, lua_getparam(L, 1)); | 192 | IOCtrl *ctrl = (IOCtrl *)lua_getuserdata(L, lua_getparam(L, 1)); |
| 185 | FILE *f = fopen(luaL_check_string(L, 2), luaL_check_string(L, 3)); | 193 | FILE *f = fopen(luaL_check_string(L, 2), luaL_check_string(L, 3)); |
| @@ -649,6 +657,10 @@ static void openwithcontrol (lua_State *L) { | |||
| 649 | lua_pushusertag(L, ctrl, ctrltag); | 657 | lua_pushusertag(L, ctrl, ctrltag); |
| 650 | lua_pushcclosure(L, ctrl_collect, 1); | 658 | lua_pushcclosure(L, ctrl_collect, 1); |
| 651 | lua_settagmethod(L, ctrltag, "gc"); | 659 | lua_settagmethod(L, ctrltag, "gc"); |
| 660 | /* close files when collected */ | ||
| 661 | lua_pushusertag(L, ctrl, ctrltag); | ||
| 662 | lua_pushcclosure(L, file_collect, 1); | ||
| 663 | lua_settagmethod(L, ctrl->iotag, "gc"); | ||
| 652 | } | 664 | } |
| 653 | 665 | ||
| 654 | void lua_iolibopen (lua_State *L) { | 666 | void lua_iolibopen (lua_State *L) { |
