aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c56
1 files changed, 26 insertions, 30 deletions
diff --git a/liolib.c b/liolib.c
index 7dc1dab9..0884e9ac 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 2.154 2017/11/16 16:28:36 roberto Exp roberto $ 2** $Id: liolib.c,v 2.155 2018/02/21 13:48:44 roberto Exp roberto $
3** Standard I/O (and system) library 3** Standard I/O (and system) library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -528,44 +528,40 @@ static int read_chars (lua_State *L, FILE *f, size_t n) {
528 528
529static int g_read (lua_State *L, FILE *f, int first) { 529static int g_read (lua_State *L, FILE *f, int first) {
530 int nargs = lua_gettop(L) - 1; 530 int nargs = lua_gettop(L) - 1;
531 int nresults, success; 531 int n, success;
532 clearerr(f); 532 clearerr(f);
533 if (nargs == 0) { /* no arguments? */ 533 if (nargs == 0) { /* no arguments? */
534 success = read_line(L, f, 1); 534 success = read_line(L, f, 1);
535 nresults = 1; 535 n = first + 1; /* to return 1 result */
536 } 536 }
537 else { 537 else {
538 /* ensure stack space for all results and for auxlib's buffer */
539 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
538 success = 1; 540 success = 1;
539 nresults = 0; 541 for (n = first; nargs-- && success; n++) {
540 for (; nargs-- && success; first++) { 542 if (lua_type(L, n) == LUA_TNUMBER) {
541 luaL_checkstack(L, LUA_MINSTACK, "too many arguments"); 543 size_t l = (size_t)luaL_checkinteger(L, n);
542 if (lua_type(L, first) == LUA_TNUMBER) {
543 size_t l = (size_t)luaL_checkinteger(L, first);
544 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); 544 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
545 nresults++;
546 } 545 }
547 else { 546 else {
548 const char *p = luaL_checkstring(L, first); 547 const char *p = luaL_checkstring(L, n);
549 if (*p == '*') p++; /* skip optional '*' (for compatibility) */ 548 if (*p == '*') p++; /* skip optional '*' (for compatibility) */
550 for (; success && *p != '\0'; p++) { 549 switch (*p) {
551 nresults++; 550 case 'n': /* number */
552 switch (*p) { 551 success = read_number(L, f);
553 case 'n': /* number */ 552 break;
554 success = read_number(L, f); 553 case 'l': /* line */
555 break; 554 success = read_line(L, f, 1);
556 case 'l': /* line */ 555 break;
557 success = read_line(L, f, 1); 556 case 'L': /* line with end-of-line */
558 break; 557 success = read_line(L, f, 0);
559 case 'L': /* line with end-of-line */ 558 break;
560 success = read_line(L, f, 0); 559 case 'a': /* file */
561 break; 560 read_all(L, f); /* read entire file */
562 case 'a': /* file */ 561 success = 1; /* always success */
563 read_all(L, f); /* read entire file */ 562 break;
564 success = 1; /* always success */ 563 default:
565 break; 564 return luaL_argerror(L, n, "invalid format");
566 default:
567 return luaL_argerror(L, first, "invalid format");
568 }
569 } 565 }
570 } 566 }
571 } 567 }
@@ -576,7 +572,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
576 lua_pop(L, 1); /* remove last result */ 572 lua_pop(L, 1); /* remove last result */
577 lua_pushnil(L); /* push nil instead */ 573 lua_pushnil(L); /* push nil instead */
578 } 574 }
579 return nresults; 575 return n - first;
580} 576}
581 577
582 578