| 575 | |
| 576 | |
| 577 | LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { |
| 578 | LoadF lf; |
| 579 | int status, readstatus; |
| 580 | int c; |
| 581 | int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ |
| 582 | lf.extraline = 0; |
| 583 | if (filename == NULL) { |
| 584 | lua_pushliteral(L, "=stdin"); |
| 585 | lf.f = stdin; |
| 586 | } |
| 587 | else { |
| 588 | lua_pushfstring(L, "@%s", filename); |
| 589 | lf.f = fopen(filename, "r"); |
| 590 | if (lf.f == NULL) return errfile(L, "open", fnameindex); |
| 591 | } |
| 592 | c = getc(lf.f); |
| 593 | if (c == '#') { /* Unix exec. file? */ |
| 594 | lf.extraline = 1; |
| 595 | while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ |
| 596 | if (c == '\n') c = getc(lf.f); |
| 597 | } |
| 598 | if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ |
| 599 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ |
| 600 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); |
| 601 | /* skip eventual `#!...' */ |
| 602 | while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; |
| 603 | lf.extraline = 0; |
| 604 | } |
| 605 | ungetc(c, lf.f); |
| 606 | status = lua_load(L, getF, &lf, lua_tostring(L, -1)); |
| 607 | readstatus = ferror(lf.f); |
| 608 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ |
| 609 | if (readstatus) { |
| 610 | lua_settop(L, fnameindex); /* ignore results from `lua_load' */ |
| 611 | return errfile(L, "read", fnameindex); |
| 612 | } |
| 613 | lua_remove(L, fnameindex); |
| 614 | return status; |
| 615 | } |
| 616 | |
| 617 | |
| 618 | typedef struct LoadS { |
no test coverage detected