Selaa lähdekoodia

bug: cannot reopen stdin (for binary mode)

Roberto Ierusalimschy 25 vuotta sitten
vanhempi
commit
b3aaa048b0
2 muutettua tiedostoa jossa 22 lisäystä ja 10 poistoa
  1. 4 0
      bugs
  2. 18 10
      ldo.c

+ 4 - 0
bugs

@@ -133,3 +133,7 @@ Wed Dec 29 16:05:43 EDT 1999
 >> return gives wrong line in debug information
 (by lhf; since 3.2 [at least])
 
+** ldo.c
+Thu Dec 30 16:39:33 EDT 1999
+>> cannot reopen stdin (for binary mode)
+(by lhf & roberto; since 3.1)

+ 18 - 10
ldo.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.c,v 1.62 1999/12/29 16:31:15 roberto Exp roberto $
+** $Id: ldo.c,v 1.63 1999/12/30 18:28:40 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -365,18 +365,26 @@ void luaD_gcIM (lua_State *L, const TObject *o) {
 int lua_dofile (lua_State *L, const char *filename) {
   ZIO z;
   int status;
-  int c;
   int bin;
   char source[MAXFILENAME];
-  FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
-  if (f == NULL)
-    return 2;
-  c = fgetc(f);
-  ungetc(c, f);
-  bin = (c == ID_CHUNK);
-  if (bin)
-    f = freopen(filename, "rb", f);  /* set binary mode */
+  FILE *f;
   luaL_filesource(source, filename, sizeof(source));
+  if (filename == NULL) {
+    f = stdin;
+    bin = 0;  /* cannot handle stdin as a binary file */
+  }
+  else {
+    int c;
+    f = fopen(filename, "r");
+    if (f == NULL) return 2;  /* unable to open file */
+    c = fgetc(f);
+    ungetc(c, f);
+    bin = (c == ID_CHUNK);
+    if (bin) {
+      f = freopen(filename, "rb", f);  /* set binary mode */
+      if (f == NULL) return 2;  /* unable to reopen file */
+    }
+  }
   luaZ_Fopen(&z, f, source);
   status = do_main(L, &z, bin);
   if (f != stdin)