| 598 | |
| 599 | |
| 600 | LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { |
| 601 | LoadF lf; |
| 602 | int status, readstatus; |
| 603 | int c; |
| 604 | int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ |
| 605 | lf.extraline = 0; |
| 606 | if (filename == NULL) { |
| 607 | lua_pushliteral(L, "=stdin"); |
| 608 | lf.f = stdin; |
| 609 | } |
| 610 | else { |
| 611 | lua_pushfstring(L, "@%s", filename); |
| 612 | lf.f = fopen(filename, "r"); |
| 613 | if (lf.f == NULL) return errfile(L, "open", fnameindex); |
| 614 | } |
| 615 | c = getc(lf.f); |
| 616 | if (c == '#') { /* Unix exec. file? */ |
| 617 | lf.extraline = 1; |
| 618 | while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ |
| 619 | if (c == '\n') c = getc(lf.f); |
| 620 | } |
| 621 | if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ |
| 622 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ |
| 623 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); |
| 624 | /* skip eventual `#!...' */ |
| 625 | while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; |
| 626 | lf.extraline = 0; |
| 627 | } |
| 628 | ungetc(c, lf.f); |
| 629 | status = lua_load(L, getF, &lf, lua_tostring(L, -1)); |
| 630 | readstatus = ferror(lf.f); |
| 631 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ |
| 632 | if (readstatus) { |
| 633 | lua_settop(L, fnameindex); /* ignore results from `lua_load' */ |
| 634 | return errfile(L, "read", fnameindex); |
| 635 | } |
| 636 | lua_remove(L, fnameindex); |
| 637 | return status; |
| 638 | } |
| 639 | |
| 640 | |
| 641 | typedef struct LoadS { |
no test coverage detected