aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c14
1 files changed, 7 insertions, 7 deletions
diff --git a/liolib.c b/liolib.c
index 4e5f861a..e4851209 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.99 2001/01/18 15:59:09 roberto Exp roberto $ 2** $Id: liolib.c,v 1.100 2001/01/25 16:45:36 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*/
@@ -86,13 +86,14 @@ static int pushresult (lua_State *L, int i) {
86static FILE *getopthandle (lua_State *L, int inout) { 86static FILE *getopthandle (lua_State *L, int inout) {
87 FILE *p = (FILE *)lua_touserdata(L, 1); 87 FILE *p = (FILE *)lua_touserdata(L, 1);
88 if (p != NULL) { /* is it a userdata ? */ 88 if (p != NULL) { /* is it a userdata ? */
89 if (!checkfile(L,1)) { 89 if (!checkfile(L, 1)) {
90 if (strcmp(lua_xtype(L, 1), "ClosedFileHandle") == 0) 90 if (strcmp(lua_xtype(L, 1), "ClosedFileHandle") == 0)
91 luaL_argerror(L, 1, "file is closed"); 91 luaL_argerror(L, 1, "file is closed");
92 else 92 else
93 luaL_argerror(L, 1, "(invalid value)"); 93 luaL_argerror(L, 1, "(invalid value)");
94 } 94 }
95 lua_remove(L, 1); /* remove it from stack */ 95 /* move it to stack top */
96 lua_pushvalue(L, 1); lua_remove(L, 1);
96 } 97 }
97 else if (inout != NOFILE) { /* try global value */ 98 else if (inout != NOFILE) { /* try global value */
98 lua_getglobal(L, filenames[inout]); 99 lua_getglobal(L, filenames[inout]);
@@ -100,9 +101,8 @@ static FILE *getopthandle (lua_State *L, int inout) {
100 luaL_verror(L, "global variable `%.10s' is not a valid file handle", 101 luaL_verror(L, "global variable `%.10s' is not a valid file handle",
101 filenames[inout]); 102 filenames[inout]);
102 p = (FILE *)lua_touserdata(L, -1); 103 p = (FILE *)lua_touserdata(L, -1);
103 lua_pop(L, 1); /* remove global value from stack */
104 } 104 }
105 return p; 105 return p; /* leave handle at stack top to avoid GC */
106} 106}
107 107
108 108
@@ -295,7 +295,7 @@ static int read_chars (lua_State *L, FILE *f, size_t n) {
295 295
296static int io_read (lua_State *L) { 296static int io_read (lua_State *L) {
297 FILE *f = getopthandle(L, INFILE); 297 FILE *f = getopthandle(L, INFILE);
298 int nargs = lua_gettop(L); 298 int nargs = lua_gettop(L)-1;
299 int success; 299 int success;
300 int n; 300 int n;
301 if (nargs == 0) { /* no arguments? */ 301 if (nargs == 0) { /* no arguments? */
@@ -347,7 +347,7 @@ static int io_read (lua_State *L) {
347 347
348static int io_write (lua_State *L) { 348static int io_write (lua_State *L) {
349 FILE *f = getopthandle(L, OUTFILE); 349 FILE *f = getopthandle(L, OUTFILE);
350 int nargs = lua_gettop(L); 350 int nargs = lua_gettop(L)-1;
351 int arg; 351 int arg;
352 int status = 1; 352 int status = 1;
353 for (arg=1; arg<=nargs; arg++) { 353 for (arg=1; arg<=nargs; arg++) {