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