| 774 | |
| 775 | |
| 776 | LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, |
| 777 | const char *mode) { |
| 778 | LoadF lf; |
| 779 | int status, readstatus; |
| 780 | int c; |
| 781 | int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ |
| 782 | if (filename == NULL) { |
| 783 | lua_pushliteral(L, "=stdin"); |
| 784 | lf.f = stdin; |
| 785 | } |
| 786 | else { |
| 787 | lua_pushfstring(L, "@%s", filename); |
| 788 | lf.f = fopen(filename, "r"); |
| 789 | if (lf.f == NULL) return errfile(L, "open", fnameindex); |
| 790 | } |
| 791 | if (skipcomment(&lf, &c)) /* read initial portion */ |
| 792 | lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ |
| 793 | if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ |
| 794 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ |
| 795 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); |
| 796 | skipcomment(&lf, &c); /* re-read initial portion */ |
| 797 | } |
| 798 | if (c != EOF) |
| 799 | lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ |
| 800 | status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); |
| 801 | readstatus = ferror(lf.f); |
| 802 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ |
| 803 | if (readstatus) { |
| 804 | lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ |
| 805 | return errfile(L, "read", fnameindex); |
| 806 | } |
| 807 | lua_remove(L, fnameindex); |
| 808 | return status; |
| 809 | } |
| 810 | |
| 811 | |
| 812 | typedef struct LoadS { |
no test coverage detected