From 2d8b099274699286eed4ebe8a45900de1b28a398 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 13 Aug 2004 16:52:13 -0300 Subject: [PATCH] =?UTF-8?q?better=20checking=20for=20read=20errors=20(with?= =?UTF-8?q?=20`ferror=C2=B4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- liolib.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/liolib.c b/liolib.c index 505ad752..4663dd78 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.55 2004/07/09 16:01:38 roberto Exp roberto $ +** $Id: liolib.c,v 2.56 2004/08/09 14:35:59 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -96,7 +96,7 @@ static int aux_close (lua_State *L) { if (f == stdin || f == stdout || f == stderr) return 0; /* file cannot be closed */ else { - int ok = (pclose(f) != -1) || (fclose(f) == 0); + int ok = (fclose(f) == 0); if (ok) *(FILE **)lua_touserdata(L, 1) = NULL; /* mark file as closed */ return ok; @@ -293,6 +293,7 @@ static int g_read (lua_State *L, FILE *f, int first) { int nargs = lua_gettop(L) - 1; int success; int n; + clearerr(f); if (nargs == 0) { /* no arguments? */ success = read_line(L, f); n = first+1; /* to return 1 result */ @@ -327,6 +328,8 @@ static int g_read (lua_State *L, FILE *f, int first) { } } } + if (ferror(f)) + return pushresult(L, 0, NULL); if (!success) { lua_pop(L, 1); /* remove last result */ lua_pushnil(L); /* push nil instead */ @@ -347,9 +350,13 @@ static int f_read (lua_State *L) { static int io_readline (lua_State *L) { FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(2)); + int sucess; if (f == NULL) /* file is already closed? */ luaL_error(L, "file is already closed"); - if (read_line(L, f)) return 1; + sucess = read_line(L, f); + if (ferror(f)) + luaL_error(L, "%s", strerror(errno)); + if (sucess) return 1; else { /* EOF */ if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ lua_settop(L, 0);