| 806 | |
| 807 | |
| 808 | LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, |
| 809 | const char *mode) { |
| 810 | LoadF lf; |
| 811 | int status, readstatus; |
| 812 | int c; |
| 813 | int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ |
| 814 | if (filename == NULL) { |
| 815 | lua_pushliteral(L, "=stdin"); |
| 816 | lf.f = stdin; |
| 817 | } |
| 818 | else { |
| 819 | lua_pushfstring(L, "@%s", filename); |
| 820 | errno = 0; |
| 821 | lf.f = fopen(filename, "r"); |
| 822 | if (lf.f == NULL) return errfile(L, "open", fnameindex); |
| 823 | } |
| 824 | lf.n = 0; |
| 825 | if (skipcomment(lf.f, &c)) /* read initial portion */ |
| 826 | lf.buff[lf.n++] = '\n'; /* add newline to correct line numbers */ |
| 827 | if (c == LUA_SIGNATURE[0]) { /* binary file? */ |
| 828 | lf.n = 0; /* remove possible newline */ |
| 829 | if (filename) { /* "real" file? */ |
| 830 | errno = 0; |
| 831 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ |
| 832 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); |
| 833 | skipcomment(lf.f, &c); /* re-read initial portion */ |
| 834 | } |
| 835 | } |
| 836 | if (c != EOF) |
| 837 | lf.buff[lf.n++] = cast_char(c); /* 'c' is the first character */ |
| 838 | status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); |
| 839 | readstatus = ferror(lf.f); |
| 840 | errno = 0; /* no useful error number until here */ |
| 841 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ |
| 842 | if (readstatus) { |
| 843 | lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ |
| 844 | return errfile(L, "read", fnameindex); |
| 845 | } |
| 846 | lua_remove(L, fnameindex); |
| 847 | return status; |
| 848 | } |
| 849 | |
| 850 | |
| 851 | typedef struct LoadS { |
no test coverage detected