| 750 | |
| 751 | |
| 752 | LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, |
| 753 | const char *mode) { |
| 754 | LoadF lf; |
| 755 | int status, readstatus; |
| 756 | int c; |
| 757 | int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ |
| 758 | if (filename == NULL) { |
| 759 | lua_pushliteral(L, "=stdin"); |
| 760 | lf.f = stdin; |
| 761 | } |
| 762 | else { |
| 763 | lua_pushfstring(L, "@%s", filename); |
| 764 | lf.f = fopen(filename, "r"); |
| 765 | if (lf.f == NULL) return errfile(L, "open", fnameindex); |
| 766 | } |
| 767 | if (skipcomment(&lf, &c)) /* read initial portion */ |
| 768 | lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ |
| 769 | if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ |
| 770 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ |
| 771 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); |
| 772 | skipcomment(&lf, &c); /* re-read initial portion */ |
| 773 | } |
| 774 | if (c != EOF) |
| 775 | lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ |
| 776 | status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); |
| 777 | readstatus = ferror(lf.f); |
| 778 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ |
| 779 | if (readstatus) { |
| 780 | lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ |
| 781 | return errfile(L, "read", fnameindex); |
| 782 | } |
| 783 | lua_remove(L, fnameindex); |
| 784 | return status; |
| 785 | } |
| 786 | |
| 787 | |
| 788 | typedef struct LoadS { |
no test coverage detected