From cd54c95ee179dd1578e127745354aa6a59b72eb7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 12 Jan 1996 15:00:30 -0200 Subject: bug: "read" with format MUST read given number of chars (if file ends before that reports an error, returning nil). "readuntil" can be called with nil or no parameters to read until EOF. --- iolib.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/iolib.c b/iolib.c index 37ad50ce..25bf4fd0 100644 --- a/iolib.c +++ b/iolib.c @@ -3,7 +3,7 @@ ** Input/output library to LUA */ -char *rcs_iolib="$Id: iolib.c,v 1.28 1995/11/10 17:55:48 roberto Exp roberto $"; +char *rcs_iolib="$Id: iolib.c,v 1.29 1995/11/10 18:32:59 roberto Exp roberto $"; #include #include @@ -289,12 +289,19 @@ static void io_read (void) switch (getformat(lua_check_string(1, "read"), &dummy1, &m, &dummy2)) { case 's': + { + char *s; if (m < 0) read_until_blank(); else read_m(m); - lua_pushstring(add_char(0)); + s = add_char(0); + if ((m >= 0 && strlen(s) == m) || (m < 0 && strlen(s) > 0)) + lua_pushstring(s); + else + lua_pushnil(); break; + } case 'i': /* can read as float, since it makes no difference to Lua */ case 'f': @@ -324,8 +331,13 @@ static void io_read (void) */ static void io_readuntil (void) { - int del = *lua_check_string(1, "readuntil"); - int c = read_until_char(del); + int del, c; + lua_Object p = lua_getparam(1); + if (p == LUA_NOOBJECT || lua_isnil(p)) + del = EOF; + else + del = *lua_check_string(1, "readuntil"); + c = read_until_char(del); if (c != EOF) ungetc(c,in); lua_pushstring(add_char(0)); } @@ -560,7 +572,7 @@ void lua_printstack (FILE *f) char *name; int currentline; fprintf(f, "\t"); - switch (*getobjname(func, &name)) + switch (*lua_getobjname(func, &name)) { case 'g': fprintf(f, "function %s", name); -- cgit v1.2.3-55-g6feb