Browse Source

writeto, readfrom, and closefile must return an error code when
closing a file.

Roberto Ierusalimschy 26 years ago
parent
commit
21843f022a
1 changed files with 17 additions and 11 deletions
  1. 17 11
      liolib.c

+ 17 - 11
liolib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: liolib.c,v 1.41 1999/06/23 13:48:39 roberto Exp roberto $
+** $Id: liolib.c,v 1.42 1999/07/22 19:35:50 roberto Exp roberto $
 ** Standard I/O (and system) library
 ** Standard I/O (and system) library
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -46,11 +46,11 @@
 #ifdef POPEN
 #ifdef POPEN
 FILE *popen();
 FILE *popen();
 int pclose();
 int pclose();
-#define CLOSEFILE(f)    {if (pclose(f) == -1) fclose(f);}
+#define CLOSEFILE(f)    ((pclose(f) == -1) ? fclose(f) : 0)
 #else
 #else
 /* no support for popen */
 /* no support for popen */
 #define popen(x,y) NULL  /* that is, popen always fails */
 #define popen(x,y) NULL  /* that is, popen always fails */
-#define CLOSEFILE(f)    {fclose(f);}
+#define CLOSEFILE(f)    (fclose(f))
 #endif
 #endif
 
 
 
 
@@ -120,18 +120,20 @@ static FILE *getfileparam (char *name, int *arg) {
 }
 }
 
 
 
 
-static void closefile (FILE *f) {
-  if (f != stdin && f != stdout) {
+static int closefile (FILE *f) {
+  if (f == stdin || f == stdout)
+    return 1;
+  else {
     int tag = gettag();
     int tag = gettag();
-    CLOSEFILE(f);
     lua_pushusertag(f, tag);
     lua_pushusertag(f, tag);
     lua_settag(CLOSEDTAG(tag));
     lua_settag(CLOSEDTAG(tag));
+    return (CLOSEFILE(f) == 0);
   }
   }
 }
 }
 
 
 
 
 static void io_close (void) {
 static void io_close (void) {
-  closefile(getnonullfile(FIRSTARG));
+  pushresult(closefile(getnonullfile(FIRSTARG)));
 }
 }
 
 
 
 
@@ -171,8 +173,10 @@ static void io_readfrom (void) {
   FILE *current;
   FILE *current;
   lua_Object f = lua_getparam(FIRSTARG);
   lua_Object f = lua_getparam(FIRSTARG);
   if (f == LUA_NOOBJECT) {
   if (f == LUA_NOOBJECT) {
-    closefile(getfilebyname(FINPUT));
-    current = stdin;
+    if (closefile(getfilebyname(FINPUT)))
+      current = stdin;
+    else
+      current = NULL;  /* to signal error */
   }
   }
   else if (lua_tag(f) == gettag())  /* deprecated option */
   else if (lua_tag(f) == gettag())  /* deprecated option */
     current = lua_getuserdata(f);
     current = lua_getuserdata(f);
@@ -188,8 +192,10 @@ static void io_writeto (void) {
   FILE *current;
   FILE *current;
   lua_Object f = lua_getparam(FIRSTARG);
   lua_Object f = lua_getparam(FIRSTARG);
   if (f == LUA_NOOBJECT) {
   if (f == LUA_NOOBJECT) {
-    closefile(getfilebyname(FOUTPUT));
-    current = stdout;
+    if (closefile(getfilebyname(FOUTPUT)))
+      current = stdout;
+    else
+      current = NULL;  /* to signal error */
   }
   }
   else if (lua_tag(f) == gettag())  /* deprecated option */
   else if (lua_tag(f) == gettag())  /* deprecated option */
     current = lua_getuserdata(f);
     current = lua_getuserdata(f);