| 10762 | |
| 10763 | |
| 10764 | LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { |
| 10765 | LoadF lf; |
| 10766 | int status, readstatus; |
| 10767 | int c; |
| 10768 | int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ |
| 10769 | lf.extraline = 0; |
| 10770 | if (filename == NULL) { |
| 10771 | lua_pushliteral(L, "=stdin"); |
| 10772 | lf.f = stdin; |
| 10773 | } |
| 10774 | else { |
| 10775 | lua_pushfstring(L, "@%s", filename); |
| 10776 | lf.f = fopen(filename, "r"); |
| 10777 | if (lf.f == NULL) return errfile(L, "open", fnameindex); |
| 10778 | } |
| 10779 | c = getc(lf.f); |
| 10780 | if (c == '#') { /* Unix exec. file? */ |
| 10781 | lf.extraline = 1; |
| 10782 | while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ |
| 10783 | if (c == '\n') c = getc(lf.f); |
| 10784 | } |
| 10785 | if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ |
| 10786 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ |
| 10787 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); |
| 10788 | /* skip eventual `#!...' */ |
| 10789 | while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; |
| 10790 | lf.extraline = 0; |
| 10791 | } |
| 10792 | ungetc(c, lf.f); |
| 10793 | status = lua_load(L, getF, &lf, lua_tostring(L, -1)); |
| 10794 | readstatus = ferror(lf.f); |
| 10795 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ |
| 10796 | if (readstatus) { |
| 10797 | lua_settop(L, fnameindex); /* ignore results from `lua_load' */ |
| 10798 | return errfile(L, "read", fnameindex); |
| 10799 | } |
| 10800 | lua_remove(L, fnameindex); |
| 10801 | return status; |
| 10802 | } |
| 10803 | |
| 10804 | |
| 10805 | typedef struct LoadS { |
no test coverage detected