summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--liolib.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/liolib.c b/liolib.c
index f44a1f76..ad32d073 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: $ 2** $Id: liolib.c,v 1.1 1997/09/16 19:25:59 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*/
@@ -42,6 +42,7 @@ int pclose();
42 42
43 43
44int lua_tagio; 44int lua_tagio;
45static int closedtag;
45 46
46 47
47static void pushresult (int i) 48static void pushresult (int i)
@@ -59,8 +60,12 @@ static void pushresult (int i)
59static FILE *getfile (char *name) 60static FILE *getfile (char *name)
60{ 61{
61 lua_Object f = lua_getglobal(name); 62 lua_Object f = lua_getglobal(name);
62 if (!lua_isuserdata(f) || lua_tag(f) != lua_tagio) 63 if (!lua_isuserdata(f) || lua_tag(f) != lua_tagio) {
63 luaL_verror("global variable %s is not a file handle", name); 64 if (lua_tag(f) == closedtag)
65 luaL_verror("file %s has been closed", name);
66 else
67 luaL_verror("global variable %s is not a file handle", name);
68 }
64 return lua_getuserdata(f); 69 return lua_getuserdata(f);
65} 70}
66 71
@@ -71,6 +76,8 @@ static void closefile (char *name)
71 if (f == stdin || f == stdout) return; 76 if (f == stdin || f == stdout) return;
72 if (pclose(f) == -1) 77 if (pclose(f) == -1)
73 fclose(f); 78 fclose(f);
79 lua_pushobject(lua_getglobal(name));
80 lua_settag(closedtag);
74} 81}
75 82
76 83
@@ -348,6 +355,7 @@ static struct luaL_reg iolib[] = {
348void lua_iolibopen (void) 355void lua_iolibopen (void)
349{ 356{
350 lua_tagio = lua_newtag(); 357 lua_tagio = lua_newtag();
358 closedtag = lua_newtag();
351 setfile(stdin, "_INPUT"); 359 setfile(stdin, "_INPUT");
352 setfile(stdout, "_OUTPUT"); 360 setfile(stdout, "_OUTPUT");
353 setfile(stdin, "_STDIN"); 361 setfile(stdin, "_STDIN");