lua

A copy of the Lua development repository
Log | Files | Refs | README

commit f87057690b3afa7812d0ef5251ce103902a37c96
parent 9e6aa878c96485ba0658303c0da16adef56ba54c
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Mon,  9 Aug 2004 11:35:37 -0300

`io.lines' also can give the file name in its error message

Diffstat:
Mliolib.c | 17+++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/liolib.c b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.54 2004/07/09 15:47:48 roberto Exp roberto $ +** $Id: liolib.c,v 2.55 2004/07/09 16:01:38 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -44,6 +44,12 @@ static int pushresult (lua_State *L, int i, const char *filename) { } +static void fileerror (lua_State *L, int arg, const char *filename) { + lua_pushfstring(L, "%s: %s", filename, strerror(errno)); + luaL_argerror(L, arg, lua_tostring(L, -1)); +} + + static FILE **topfile (lua_State *L) { FILE **f = (FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE); if (f == NULL) luaL_argerror(L, 1, "bad file"); @@ -156,10 +162,8 @@ static int g_iofile (lua_State *L, int f, const char *mode) { if (filename) { FILE **pf = newfile(L); *pf = fopen(filename, mode); - if (*pf == NULL) { - lua_pushfstring(L, "%s: %s", filename, strerror(errno)); - luaL_argerror(L, 1, lua_tostring(L, -1)); - } + if (*pf == NULL) + fileerror(L, 1, filename); } else { tofile(L); /* check that it's a valid file handle */ @@ -212,7 +216,8 @@ static int io_lines (lua_State *L) { const char *filename = luaL_checkstring(L, 1); FILE **pf = newfile(L); *pf = fopen(filename, "r"); - luaL_argcheck(L, *pf, 1, strerror(errno)); + if (*pf == NULL) + fileerror(L, 1, filename); aux_lines(L, lua_gettop(L), 1); return 1; }