aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-07-12 11:59:14 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-07-12 11:59:14 -0300
commita264fd089e1814bc13d6de924b928959af2e38d9 (patch)
tree2668bdef22b7b0bdde6f5beced2285ca1e7e5569
parenta3d03ff6b68a9ba5351d88225283fcad9e80be60 (diff)
downloadlua-a264fd089e1814bc13d6de924b928959af2e38d9.tar.gz
lua-a264fd089e1814bc13d6de924b928959af2e38d9.tar.bz2
lua-a264fd089e1814bc13d6de924b928959af2e38d9.zip
small bug in read_chars (fread x eof)
-rw-r--r--liolib.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/liolib.c b/liolib.c
index 56a0ee5a..b0cb6381 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.116 2001/06/22 13:49:42 roberto Exp roberto $ 2** $Id: liolib.c,v 1.117 2001/06/28 14:45:44 roberto Exp roberto $
3** Standard I/O (and system) library 3** Standard I/O (and system) library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -263,7 +263,7 @@ static int read_until (lua_State *L, FILE *f, const l_char *p, int pl) {
263 263
264static int read_number (lua_State *L, FILE *f) { 264static int read_number (lua_State *L, FILE *f) {
265 double d; 265 double d;
266 if (fscanf(f, l_s(LUA_SCAN_NUMBER), &d) == 1) { 266 if (fscanf(f, l_s(LUA_NUMBER_SCAN), &d) == 1) {
267 lua_pushnumber(L, d); 267 lua_pushnumber(L, d);
268 return 1; 268 return 1;
269 } 269 }
@@ -280,17 +280,18 @@ static int test_eof (lua_State *L, FILE *f) {
280 280
281 281
282static int read_chars (lua_State *L, FILE *f, size_t n) { 282static int read_chars (lua_State *L, FILE *f, size_t n) {
283 size_t rlen; 283 size_t rlen; /* how much to read */
284 size_t nr; /* number of chars actually read */
284 luaL_Buffer b; 285 luaL_Buffer b;
285 luaL_buffinit(L, &b); 286 luaL_buffinit(L, &b);
286 rlen = LUAL_BUFFERSIZE; 287 rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
287 do { 288 do {
288 l_char *p = luaL_prepbuffer(&b); 289 l_char *p = luaL_prepbuffer(&b);
289 if (rlen > n) rlen = n; 290 if (rlen > n) rlen = n; /* cannot read more than asked */
290 rlen = fread(p, sizeof(l_char), rlen, f); 291 nr = fread(p, sizeof(l_char), rlen, f);
291 luaL_addsize(&b, rlen); 292 luaL_addsize(&b, nr);
292 n -= rlen; 293 n -= nr; /* still have to read `n' chars */
293 } while (n > 0 && rlen > 0); /* until end of count or eof */ 294 } while (n > 0 && nr == rlen); /* until end of count or eof */
294 luaL_pushresult(&b); /* close buffer */ 295 luaL_pushresult(&b); /* close buffer */
295 return (n == 0 || lua_strlen(L, -1) > 0); 296 return (n == 0 || lua_strlen(L, -1) > 0);
296} 297}