From e70e6a3b7a2dcc2e0cddbfe54ab8cf735a6df735 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 9 Feb 2001 14:25:50 -0200 Subject: [PATCH] tests for EOF --- liolib.c | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/liolib.c b/liolib.c index 530ffa14..80828a4b 100644 --- a/liolib.c +++ b/liolib.c @@ -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 ** 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) { - char *buffer; - 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"); + if (n == 0) { /* test eof? */ + int c = fgetc(f); + ungetc(c, f); + lua_pushlstring(L, NULL, 0); + return (c != EOF); + } + else { + char *buffer; + 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); }