** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
**
** Major portions taken verbatim or adapted from the Lua interpreter.
-** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
+** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h
*/
#include <errno.h>
return (c != EOF);
}
-static int io_file_readline(lua_State *L, FILE *fp)
+static int io_file_readline(lua_State *L, FILE *fp, size_t chop)
{
luaL_Buffer b;
luaL_buffinit(L, &b);
if (len == 0 || p[len-1] != '\n') { /* Partial line? */
luaL_addsize(&b, len);
} else {
- luaL_addsize(&b, len - 1); /* Don't include EOL. */
+ luaL_addsize(&b, len - chop); /* Keep or remove EOL. */
luaL_pushresult(&b);
return 1; /* Got at least an EOL. */
}
int ok, n, nargs = (int)(L->top - L->base) - start;
clearerr(fp);
if (nargs == 0) {
- ok = io_file_readline(L, fp);
+ ok = io_file_readline(L, fp, 1);
n = start+1; /* Return 1 result. */
} else {
/* The results plus the buffers go on top of the args. */
lj_err_arg(L, n+1, LJ_ERR_INVOPT);
if (p[1] == 'n')
ok = io_file_readnum(L, fp);
- else if (p[1] == 'l')
- ok = io_file_readline(L, fp);
+ else if ((p[1] & ~0x20) == 'L')
+ ok = io_file_readline(L, fp, (p[1] == 'l'));
else if (p[1] == 'a')
io_file_readchars(L, fp, ~((size_t)0));
else
LJLIB_NOREG LJLIB_CF(io_lines_iter)
{
IOFileUD *iof = io_tofile(L);
- int ok = io_file_readline(L, iof->fp);
+ int ok = io_file_readline(L, iof->fp, 1);
if (ferror(iof->fp))
lj_err_callermsg(L, strerror(errno));
if (!ok && (iof->type & IOFILE_FLAG_CLOSE))