commit b3aaa048b05e62d14431f1f45d17856df47a88ae
parent aee07c65991621497c005f47e1b7284851ae9fb9
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Thu, 30 Dec 1999 16:40:35 -0200
bug: cannot reopen stdin (for binary mode)
Diffstat:
M | bugs | | | 4 | ++++ |
M | ldo.c | | | 28 | ++++++++++++++++++---------- |
2 files changed, 22 insertions(+), 10 deletions(-)
diff --git a/bugs b/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)
diff --git a/ldo.c b/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)