<read(0)> tests for EOF

This commit is contained in:
Roberto Ierusalimschy 2001-02-09 14:25:50 -02:00
parent 6548bf7462
commit e70e6a3b7a

View File

@ -1,5 +1,5 @@
/* /*
** $Id: liolib.c,v 1.103 2001/02/02 19:02:40 roberto Exp roberto $ ** $Id: liolib.c,v 1.104 2001/02/06 16:01:29 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
*/ */
@ -272,20 +272,28 @@ static void read_file (lua_State *L, FILE *f) {
static int read_chars (lua_State *L, FILE *f, size_t n) { static int read_chars (lua_State *L, FILE *f, size_t n) {
char *buffer; if (n == 0) { /* test eof? */
size_t n1; int c = fgetc(f);
char statbuff[LUAL_BUFFERSIZE]; ungetc(c, f);
if (n <= LUAL_BUFFERSIZE) lua_pushlstring(L, NULL, 0);
buffer = statbuff; return (c != EOF);
else { }
buffer = (char *)l_malloc(n); else {
if (buffer == NULL) char *buffer;
lua_error(L, "not enough memory to read a file"); size_t n1;
char statbuff[LUAL_BUFFERSIZE];
if (n <= LUAL_BUFFERSIZE)
buffer = statbuff;
else {
buffer = (char *)l_malloc(n);
if (buffer == NULL)
lua_error(L, "not enough memory to read a file");
}
n1 = fread(buffer, sizeof(char), n, f);
lua_pushlstring(L, buffer, n1);
if (buffer != statbuff) l_free(buffer, n);
return (n1 > 0 || n == 0);
} }
n1 = fread(buffer, sizeof(char), n, f);
lua_pushlstring(L, buffer, n1);
if (buffer != statbuff) l_free(buffer, n);
return (n1 > 0 || n == 0);
} }