| 82 | } |
| 83 | |
| 84 | LUALIB_API int luaL_loadfilex(lua_State *L, const char *filename, |
| 85 | const char *mode) |
| 86 | { |
| 87 | FileReaderCtx ctx; |
| 88 | int status; |
| 89 | const char *chunkname; |
| 90 | if (filename) { |
| 91 | ctx.fp = fopen(filename, "rb"); |
| 92 | if (ctx.fp == NULL) { |
| 93 | lua_pushfstring(L, "cannot open %s: %s", filename, strerror(errno)); |
| 94 | return LUA_ERRFILE; |
| 95 | } |
| 96 | chunkname = lua_pushfstring(L, "@%s", filename); |
| 97 | } else { |
| 98 | ctx.fp = stdin; |
| 99 | chunkname = "=stdin"; |
| 100 | } |
| 101 | status = lua_loadx(L, reader_file, &ctx, chunkname, mode); |
| 102 | if (ferror(ctx.fp)) { |
| 103 | L->top -= filename ? 2 : 1; |
| 104 | lua_pushfstring(L, "cannot read %s: %s", chunkname+1, strerror(errno)); |
| 105 | if (filename) |
| 106 | fclose(ctx.fp); |
| 107 | return LUA_ERRFILE; |
| 108 | } |
| 109 | if (filename) { |
| 110 | L->top--; |
| 111 | copyTV(L, L->top-1, L->top); |
| 112 | fclose(ctx.fp); |
| 113 | } |
| 114 | return status; |
| 115 | } |
| 116 | |
| 117 | LUALIB_API int luaL_loadfile(lua_State *L, const char *filename) |
| 118 | { |