bug: cannot reopen stdin (for binary mode)

This commit is contained in:
Roberto Ierusalimschy 1999-12-30 16:40:57 -02:00
parent aee07c6599
commit b3aaa048b0
2 changed files with 22 additions and 10 deletions

4
bugs
View File

@ -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)

28
ldo.c
View File

@ -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)