static int io_popen(lua_State *L) { const char *filename = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newprefile(L); p->f = lua_popen(L, filename, mode); p->closef = &io_pclose; return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; }
static LStream *newfile (lua_State *L) { LStream *p = newprefile(L); #if !defined(USE_FATFS) p->f = NULL; p->closef = &io_fclose; #endif return p; }
static void createstdfile(lua_State *L, FILE *f, const char *k, const char *fname) { LStream *p = newprefile(L); p->f = f; p->closef = &io_noclose; if (k != NULL) { lua_pushvalue(L, -1); lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */ } lua_setfield(L, -2, fname); /* add file to module */ }
static int io_popen(lua_State * L) { int ret = 1; char *safecmd = NULL; char *cmdname = NULL; int allow = 0; const char *filename = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newprefile(L); if (shellenabledp <= 0) { lua_pushnil(L); lua_pushliteral(L, "All command execution is disabled"); return 2; } /* If restrictedshell == 0, any command is allowed. */ if (restrictedshell == 0) allow = 1; else allow = shell_cmd_is_allowed(filename, &safecmd, &cmdname); if (allow == 1) { p->f = lua_popen(L, filename, mode); p->closef = &io_pclose; ret = (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; } else if (allow == 2) { p->f = lua_popen(L, safecmd, mode); p->closef = &io_pclose; ret = (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; } else if (allow == 0) { lua_pushnil(L); lua_pushliteral(L, "Command execution disabled via shell_escape='p'"); ret = 2; } else { lua_pushnil(L); lua_pushliteral(L, "Bad command line quoting"); ret = 2; } return ret; }
static LStream *newfile(lua_State *L) { LStream *p = newprefile(L); p->f = NULL; p->closef = &io_fclose; return p; }