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

This commit is contained in:
Roberto Ierusalimschy 2004-08-09 11:35:59 -03:00
parent 9e6aa878c9
commit f87057690b

View File

@ -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;
}