aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--liolib.c28
1 files changed, 17 insertions, 11 deletions
diff --git a/liolib.c b/liolib.c
index 2adc399b..27f0afbe 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.41 1999/06/23 13:48:39 roberto Exp roberto $ 2** $Id: liolib.c,v 1.42 1999/07/22 19:35:50 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*/
@@ -46,11 +46,11 @@
46#ifdef POPEN 46#ifdef POPEN
47FILE *popen(); 47FILE *popen();
48int pclose(); 48int pclose();
49#define CLOSEFILE(f) {if (pclose(f) == -1) fclose(f);} 49#define CLOSEFILE(f) ((pclose(f) == -1) ? fclose(f) : 0)
50#else 50#else
51/* no support for popen */ 51/* no support for popen */
52#define popen(x,y) NULL /* that is, popen always fails */ 52#define popen(x,y) NULL /* that is, popen always fails */
53#define CLOSEFILE(f) {fclose(f);} 53#define CLOSEFILE(f) (fclose(f))
54#endif 54#endif
55 55
56 56
@@ -120,18 +120,20 @@ static FILE *getfileparam (char *name, int *arg) {
120} 120}
121 121
122 122
123static void closefile (FILE *f) { 123static int closefile (FILE *f) {
124 if (f != stdin && f != stdout) { 124 if (f == stdin || f == stdout)
125 return 1;
126 else {
125 int tag = gettag(); 127 int tag = gettag();
126 CLOSEFILE(f);
127 lua_pushusertag(f, tag); 128 lua_pushusertag(f, tag);
128 lua_settag(CLOSEDTAG(tag)); 129 lua_settag(CLOSEDTAG(tag));
130 return (CLOSEFILE(f) == 0);
129 } 131 }
130} 132}
131 133
132 134
133static void io_close (void) { 135static void io_close (void) {
134 closefile(getnonullfile(FIRSTARG)); 136 pushresult(closefile(getnonullfile(FIRSTARG)));
135} 137}
136 138
137 139
@@ -171,8 +173,10 @@ static void io_readfrom (void) {
171 FILE *current; 173 FILE *current;
172 lua_Object f = lua_getparam(FIRSTARG); 174 lua_Object f = lua_getparam(FIRSTARG);
173 if (f == LUA_NOOBJECT) { 175 if (f == LUA_NOOBJECT) {
174 closefile(getfilebyname(FINPUT)); 176 if (closefile(getfilebyname(FINPUT)))
175 current = stdin; 177 current = stdin;
178 else
179 current = NULL; /* to signal error */
176 } 180 }
177 else if (lua_tag(f) == gettag()) /* deprecated option */ 181 else if (lua_tag(f) == gettag()) /* deprecated option */
178 current = lua_getuserdata(f); 182 current = lua_getuserdata(f);
@@ -188,8 +192,10 @@ static void io_writeto (void) {
188 FILE *current; 192 FILE *current;
189 lua_Object f = lua_getparam(FIRSTARG); 193 lua_Object f = lua_getparam(FIRSTARG);
190 if (f == LUA_NOOBJECT) { 194 if (f == LUA_NOOBJECT) {
191 closefile(getfilebyname(FOUTPUT)); 195 if (closefile(getfilebyname(FOUTPUT)))
192 current = stdout; 196 current = stdout;
197 else
198 current = NULL; /* to signal error */
193 } 199 }
194 else if (lua_tag(f) == gettag()) /* deprecated option */ 200 else if (lua_tag(f) == gettag()) /* deprecated option */
195 current = lua_getuserdata(f); 201 current = lua_getuserdata(f);