| 550 | |
| 551 | |
| 552 | LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { |
| 553 | LoadF lf; |
| 554 | int status, readstatus; |
| 555 | int c; |
| 556 | int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ |
| 557 | lf.extraline = 0; |
| 558 | if (filename == NULL) { |
| 559 | lua_pushliteral(L, "=stdin"); |
| 560 | lf.f = stdin; |
| 561 | } |
| 562 | else { |
| 563 | lua_pushfstring(L, "@%s", filename); |
| 564 | lf.f = fopen(filename, "r"); |
| 565 | if (lf.f == NULL) return errfile(L, "open", fnameindex); |
| 566 | } |
| 567 | c = getc(lf.f); |
| 568 | if (c == '#') { /* Unix exec. file? */ |
| 569 | lf.extraline = 1; |
| 570 | while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ |
| 571 | if (c == '\n') c = getc(lf.f); |
| 572 | } |
| 573 | if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ |
| 574 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ |
| 575 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); |
| 576 | /* skip eventual `#!...' */ |
| 577 | while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; |
| 578 | lf.extraline = 0; |
| 579 | } |
| 580 | ungetc(c, lf.f); |
| 581 | status = lua_load(L, getF, &lf, lua_tostring(L, -1)); |
| 582 | readstatus = ferror(lf.f); |
| 583 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ |
| 584 | if (readstatus) { |
| 585 | lua_settop(L, fnameindex); /* ignore results from `lua_load' */ |
| 586 | return errfile(L, "read", fnameindex); |
| 587 | } |
| 588 | lua_remove(L, fnameindex); |
| 589 | return status; |
| 590 | } |
| 591 | |
| 592 | |
| 593 | typedef struct LoadS { |
no test coverage detected