liolib.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. ** $Id: liolib.c,v 2.73.1.4 2010/05/14 15:33:51 roberto Exp $
  3. ** Standard I/O (and system) library
  4. ** See Copyright Notice in lua.h
  5. */
  6. // #include "c_errno.h"
  7. #include "c_stdio.h"
  8. #include "c_stdlib.h"
  9. #include "c_string.h"
  10. #include "vfs.h"
  11. #define liolib_c
  12. #define LUA_LIB
  13. #include "lua.h"
  14. #include "lauxlib.h"
  15. #include "lualib.h"
  16. #include "lrotable.h"
  17. #define IO_INPUT 1
  18. #define IO_OUTPUT 2
  19. #define IO_STDERR 0
  20. #if LUA_OPTIMIZE_MEMORY != 2
  21. #define LUA_IO_GETFIELD(f) lua_rawgeti(L, LUA_ENVIRONINDEX, f)
  22. #define LUA_IO_SETFIELD(f) lua_rawseti(L, LUA_ENVIRONINDEX, f)
  23. #else
  24. #define LUA_IO_GETFIELD(f) lua_rawgeti(L, LUA_REGISTRYINDEX, liolib_keys[f])
  25. #define LUA_IO_SETFIELD(f) lua_rawseti(L, LUA_REGISTRYINDEX, liolib_keys[f])
  26. /* "Pseudo-random" keys for the registry */
  27. static const int liolib_keys[] = {(int)&luaL_callmeta, (int)&luaL_typerror, (int)&luaL_argerror};
  28. #endif
  29. static const char *const fnames[] = {"input", "output"};
  30. static int pushresult (lua_State *L, int i, const char *filename) {
  31. int en = vfs_ferrno(0); /* calls to Lua API may change this value */
  32. if (i) {
  33. lua_pushboolean(L, 1);
  34. return 1;
  35. }
  36. else {
  37. lua_pushnil(L);
  38. if (filename)
  39. lua_pushfstring(L, "%s: err(%d)", filename, en);
  40. else
  41. lua_pushfstring(L, "err(%d)", en);
  42. lua_pushinteger(L, en);
  43. return 3;
  44. }
  45. }
  46. static void fileerror (lua_State *L, int arg, const char *filename) {
  47. lua_pushfstring(L, "%s: err(%d)", filename, vfs_ferrno(0));
  48. luaL_argerror(L, arg, lua_tostring(L, -1));
  49. }
  50. #define tofilep(L) ((int *)luaL_checkudata(L, 1, LUA_FILEHANDLE))
  51. static int io_type (lua_State *L) {
  52. void *ud;
  53. luaL_checkany(L, 1);
  54. ud = lua_touserdata(L, 1);
  55. lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);
  56. if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
  57. lua_pushnil(L); /* not a file */
  58. else if (*((int *)ud) < FS_OPEN_OK)
  59. lua_pushliteral(L, "closed file");
  60. else
  61. lua_pushliteral(L, "file");
  62. return 1;
  63. }
  64. static int tofile (lua_State *L) {
  65. int *f = tofilep(L);
  66. if (*f < FS_OPEN_OK)
  67. luaL_error(L, "attempt to use a closed file");
  68. return *f;
  69. }
  70. /*
  71. ** When creating file handles, always creates a `closed' file handle
  72. ** before opening the actual file; so, if there is a memory error, the
  73. ** file is not left opened.
  74. */
  75. static int *newfile (lua_State *L) {
  76. int *pf = (int *)lua_newuserdata(L, sizeof(int));
  77. *pf = FS_OPEN_OK - 1; /* file handle is currently `closed' */
  78. luaL_getmetatable(L, LUA_FILEHANDLE);
  79. lua_setmetatable(L, -2);
  80. return pf;
  81. }
  82. #if LUA_OPTIMIZE_MEMORY != 2
  83. /*
  84. ** function to (not) close the standard files stdin, stdout, and stderr
  85. */
  86. static int io_noclose (lua_State *L) {
  87. lua_pushnil(L);
  88. lua_pushliteral(L, "cannot close standard file");
  89. return 2;
  90. }
  91. #if 0
  92. /*
  93. ** function to close 'popen' files
  94. */
  95. static int io_pclose (lua_State *L) {
  96. int *p = tofilep(L);
  97. int ok = lua_pclose(L, *p);
  98. *p = FS_OPEN_OK - 1;
  99. return pushresult(L, ok, NULL);
  100. }
  101. #endif
  102. /*
  103. ** function to close regular files
  104. */
  105. static int io_fclose (lua_State *L) {
  106. int *p = tofilep(L);
  107. int ok = (vfs_close(*p) == 0);
  108. *p = FS_OPEN_OK - 1;
  109. return pushresult(L, ok, NULL);
  110. }
  111. #endif
  112. static int aux_close (lua_State *L) {
  113. #if LUA_OPTIMIZE_MEMORY != 2
  114. lua_getfenv(L, 1);
  115. lua_getfield(L, -1, "__close");
  116. return (lua_tocfunction(L, -1))(L);
  117. #else
  118. int *p = tofilep(L);
  119. if(*p == c_stdin || *p == c_stdout || *p == c_stderr)
  120. {
  121. lua_pushnil(L);
  122. lua_pushliteral(L, "cannot close standard file");
  123. return 2;
  124. }
  125. int ok = (vfs_close(*p) == 0);
  126. *p = FS_OPEN_OK - 1;
  127. return pushresult(L, ok, NULL);
  128. #endif
  129. }
  130. static int io_close (lua_State *L) {
  131. if (lua_isnone(L, 1))
  132. LUA_IO_GETFIELD(IO_OUTPUT);
  133. tofile(L); /* make sure argument is a file */
  134. return aux_close(L);
  135. }
  136. static int io_gc (lua_State *L) {
  137. int f = *tofilep(L);
  138. /* ignore closed files */
  139. if (f != FS_OPEN_OK - 1)
  140. aux_close(L);
  141. return 0;
  142. }
  143. static int io_tostring (lua_State *L) {
  144. int f = *tofilep(L);
  145. if (f == FS_OPEN_OK - 1)
  146. lua_pushliteral(L, "file (closed)");
  147. else
  148. lua_pushfstring(L, "file (%i)", f);
  149. return 1;
  150. }
  151. static int io_open (lua_State *L) {
  152. const char *filename = luaL_checkstring(L, 1);
  153. const char *mode = luaL_optstring(L, 2, "r");
  154. int *pf = newfile(L);
  155. *pf = vfs_open(filename, mode);
  156. return (*pf == FS_OPEN_OK - 1) ? pushresult(L, 0, filename) : 1;
  157. }
  158. /*
  159. ** this function has a separated environment, which defines the
  160. ** correct __close for 'popen' files
  161. */
  162. #if 0
  163. static int io_popen (lua_State *L) {
  164. const char *filename = luaL_checkstring(L, 1);
  165. const char *mode = luaL_optstring(L, 2, "r");
  166. int *pf = newfile(L);
  167. *pf = lua_popen(L, filename, fs_mode2flags(mode));
  168. return (*pf == FS_OPEN_OK - 1) ? pushresult(L, 0, filename) : 1;
  169. }
  170. static int io_tmpfile (lua_State *L) {
  171. int *pf = newfile(L);
  172. *pf = tmpfile();
  173. return (*pf == FS_OPEN_OK - 1) ? pushresult(L, 0, NULL) : 1;
  174. }
  175. #endif
  176. static int getiofile (lua_State *L, int findex) {
  177. int *pf;
  178. LUA_IO_GETFIELD(findex);
  179. pf = (int *)lua_touserdata(L, -1);
  180. if (pf == NULL || *pf == FS_OPEN_OK - 1){
  181. luaL_error(L, "default %s file is closed", fnames[findex - 1]);
  182. return FS_OPEN_OK - 1;
  183. }
  184. return *pf;
  185. }
  186. static int g_iofile (lua_State *L, int f, const char *mode) {
  187. if (!lua_isnoneornil(L, 1)) {
  188. const char *filename = lua_tostring(L, 1);
  189. if (filename) {
  190. int *pf = newfile(L);
  191. *pf = vfs_open(filename, mode);
  192. if (*pf == FS_OPEN_OK - 1)
  193. fileerror(L, 1, filename);
  194. }
  195. else {
  196. tofile(L); /* check that it's a valid file handle */
  197. lua_pushvalue(L, 1);
  198. }
  199. LUA_IO_SETFIELD(f);
  200. }
  201. /* return current value */
  202. LUA_IO_GETFIELD(f);
  203. return 1;
  204. }
  205. static int io_input (lua_State *L) {
  206. return g_iofile(L, IO_INPUT, "r");
  207. }
  208. static int io_output (lua_State *L) {
  209. return g_iofile(L, IO_OUTPUT, "w");
  210. }
  211. static int io_readline (lua_State *L);
  212. static void aux_lines (lua_State *L, int idx, int toclose) {
  213. lua_pushvalue(L, idx);
  214. lua_pushboolean(L, toclose); /* close/not close file when finished */
  215. lua_pushcclosure(L, io_readline, 2);
  216. }
  217. static int f_lines (lua_State *L) {
  218. tofile(L); /* check that it's a valid file handle */
  219. aux_lines(L, 1, 0);
  220. return 1;
  221. }
  222. static int io_lines (lua_State *L) {
  223. if (lua_isnoneornil(L, 1)) { /* no arguments? */
  224. /* will iterate over default input */
  225. LUA_IO_GETFIELD(IO_INPUT);
  226. return f_lines(L);
  227. }
  228. else {
  229. const char *filename = luaL_checkstring(L, 1);
  230. int *pf = newfile(L);
  231. *pf = vfs_open(filename, "r");
  232. if (*pf == FS_OPEN_OK - 1)
  233. fileerror(L, 1, filename);
  234. aux_lines(L, lua_gettop(L), 1);
  235. return 1;
  236. }
  237. }
  238. /*
  239. ** {======================================================
  240. ** READ
  241. ** =======================================================
  242. */
  243. #if 0
  244. static int read_number (lua_State *L, int f) {
  245. lua_Number d;
  246. if (fs_scanf(f, LUA_NUMBER_SCAN, &d) == 1) {
  247. lua_pushnumber(L, d);
  248. return 1;
  249. }
  250. else {
  251. lua_pushnil(L); /* "result" to be removed */
  252. return 0; /* read fails */
  253. }
  254. }
  255. #endif
  256. static int test_eof (lua_State *L, int f) {
  257. int c = vfs_getc(f);
  258. vfs_ungetc(c, f);
  259. lua_pushlstring(L, NULL, 0);
  260. return (c != EOF);
  261. }
  262. #if 0
  263. static int read_line (lua_State *L, int f) {
  264. luaL_Buffer b;
  265. luaL_buffinit(L, &b);
  266. for (;;) {
  267. size_t l;
  268. char *p = luaL_prepbuffer(&b);
  269. if (fs_gets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
  270. luaL_pushresult(&b); /* close buffer */
  271. return (lua_objlen(L, -1) > 0); /* check whether read something */
  272. }
  273. l = c_strlen(p);
  274. if (l == 0 || p[l-1] != '\n')
  275. luaL_addsize(&b, l);
  276. else {
  277. luaL_addsize(&b, l - 1); /* do not include `eol' */
  278. luaL_pushresult(&b); /* close buffer */
  279. return 1; /* read at least an `eol' */
  280. }
  281. }
  282. }
  283. #else
  284. static int read_line (lua_State *L, int f) {
  285. luaL_Buffer b;
  286. luaL_buffinit(L, &b);
  287. signed char c;
  288. do {
  289. c = (signed char)vfs_getc(f);
  290. if (c==EOF) {
  291. break;
  292. }
  293. if (c != '\n') {
  294. luaL_addchar(&b, c);
  295. }
  296. } while (c != '\n');
  297. luaL_pushresult(&b); /* close buffer */
  298. return (lua_objlen(L, -1) > 0); /* check whether read something */
  299. }
  300. #endif
  301. static int read_chars (lua_State *L, int f, size_t n) {
  302. size_t rlen; /* how much to read */
  303. size_t nr; /* number of chars actually read */
  304. luaL_Buffer b;
  305. luaL_buffinit(L, &b);
  306. rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
  307. do {
  308. char *p = luaL_prepbuffer(&b);
  309. if (rlen > n) rlen = n; /* cannot read more than asked */
  310. nr = vfs_read(f, p, rlen);
  311. luaL_addsize(&b, nr);
  312. n -= nr; /* still have to read `n' chars */
  313. } while (n > 0 && nr == rlen); /* until end of count or eof */
  314. luaL_pushresult(&b); /* close buffer */
  315. return (n == 0 || lua_objlen(L, -1) > 0);
  316. }
  317. static int g_read (lua_State *L, int f, int first) {
  318. int nargs = lua_gettop(L) - 1;
  319. int success;
  320. int n;
  321. //vfs_clearerr(f);
  322. if (nargs == 0) { /* no arguments? */
  323. success = read_line(L, f);
  324. n = first+1; /* to return 1 result */
  325. }
  326. else { /* ensure stack space for all results and for auxlib's buffer */
  327. luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
  328. success = 1;
  329. for (n = first; nargs-- && success; n++) {
  330. if (lua_type(L, n) == LUA_TNUMBER) {
  331. size_t l = (size_t)lua_tointeger(L, n);
  332. success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
  333. }
  334. else {
  335. const char *p = lua_tostring(L, n);
  336. luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
  337. switch (p[1]) {
  338. #if 0
  339. case 'n': /* number */
  340. success = read_number(L, f);
  341. break;
  342. #endif
  343. case 'l': /* line */
  344. success = read_line(L, f);
  345. break;
  346. case 'a': /* file */
  347. read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
  348. success = 1; /* always success */
  349. break;
  350. default:
  351. return luaL_argerror(L, n, "invalid format");
  352. }
  353. }
  354. }
  355. }
  356. if (vfs_ferrno(f))
  357. return pushresult(L, 0, NULL);
  358. if (!success) {
  359. lua_pop(L, 1); /* remove last result */
  360. lua_pushnil(L); /* push nil instead */
  361. }
  362. return n - first;
  363. }
  364. static int io_read (lua_State *L) {
  365. return g_read(L, getiofile(L, IO_INPUT), 1);
  366. }
  367. static int f_read (lua_State *L) {
  368. return g_read(L, tofile(L), 2);
  369. }
  370. static int io_readline (lua_State *L) {
  371. int *pf = (int *)lua_touserdata(L, lua_upvalueindex(1));
  372. int sucess;
  373. if (pf == NULL || *pf == FS_OPEN_OK - 1){ /* file is already closed? */
  374. luaL_error(L, "file is already closed");
  375. return 0;
  376. }
  377. sucess = read_line(L, *pf);
  378. if (vfs_ferrno(*pf))
  379. return luaL_error(L, "err(%d)", vfs_ferrno(*pf));
  380. if (sucess) return 1;
  381. else { /* EOF */
  382. if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */
  383. lua_settop(L, 0);
  384. lua_pushvalue(L, lua_upvalueindex(1));
  385. aux_close(L); /* close it */
  386. }
  387. return 0;
  388. }
  389. }
  390. /* }====================================================== */
  391. static int g_write (lua_State *L, int f, int arg) {
  392. int nargs = lua_gettop(L) - 1;
  393. int status = 1;
  394. for (; nargs--; arg++) {
  395. #if 0
  396. if (lua_type(L, arg) == LUA_TNUMBER) {
  397. /* optimization: could be done exactly as for strings */
  398. status = status &&
  399. fs_printf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
  400. }
  401. else
  402. #endif
  403. {
  404. size_t l;
  405. const char *s = luaL_checklstring(L, arg, &l);
  406. status = status && (vfs_write(f, s, l) == l);
  407. }
  408. }
  409. return pushresult(L, status, NULL);
  410. }
  411. static int io_write (lua_State *L) {
  412. return g_write(L, getiofile(L, IO_OUTPUT), 1);
  413. }
  414. static int f_write (lua_State *L) {
  415. return g_write(L, tofile(L), 2);
  416. }
  417. static int f_seek (lua_State *L) {
  418. static const int mode[] = {VFS_SEEK_SET, VFS_SEEK_CUR, VFS_SEEK_END};
  419. static const char *const modenames[] = {"set", "cur", "end", NULL};
  420. int f = tofile(L);
  421. int op = luaL_checkoption(L, 2, "cur", modenames);
  422. long offset = luaL_optlong(L, 3, 0);
  423. op = vfs_lseek(f, offset, mode[op]);
  424. if (op)
  425. return pushresult(L, 0, NULL); /* error */
  426. else {
  427. lua_pushinteger(L, vfs_tell(f));
  428. return 1;
  429. }
  430. }
  431. #if 0
  432. static int f_setvbuf (lua_State *L) {
  433. static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
  434. static const char *const modenames[] = {"no", "full", "line", NULL};
  435. int f = tofile(L);
  436. int op = luaL_checkoption(L, 2, NULL, modenames);
  437. lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
  438. int res = setvbuf(f, NULL, mode[op], sz);
  439. return pushresult(L, res == 0, NULL);
  440. }
  441. #endif
  442. static int io_flush (lua_State *L) {
  443. return pushresult(L, vfs_flush(getiofile(L, IO_OUTPUT)) == 0, NULL);
  444. }
  445. static int f_flush (lua_State *L) {
  446. return pushresult(L, vfs_flush(tofile(L)) == 0, NULL);
  447. }
  448. #undef MIN_OPT_LEVEL
  449. #define MIN_OPT_LEVEL 2
  450. #include "lrodefs.h"
  451. #if LUA_OPTIMIZE_MEMORY == 2
  452. const LUA_REG_TYPE iolib_funcs[] = {
  453. #else
  454. const LUA_REG_TYPE iolib[] = {
  455. #endif
  456. {LSTRKEY("close"), LFUNCVAL(io_close)},
  457. {LSTRKEY("flush"), LFUNCVAL(io_flush)},
  458. {LSTRKEY("input"), LFUNCVAL(io_input)},
  459. {LSTRKEY("lines"), LFUNCVAL(io_lines)},
  460. {LSTRKEY("open"), LFUNCVAL(io_open)},
  461. {LSTRKEY("output"), LFUNCVAL(io_output)},
  462. // {LSTRKEY("popen"), LFUNCVAL(io_popen)},
  463. {LSTRKEY("read"), LFUNCVAL(io_read)},
  464. // {LSTRKEY("tmpfile"), LFUNCVAL(io_tmpfile)},
  465. {LSTRKEY("type"), LFUNCVAL(io_type)},
  466. {LSTRKEY("write"), LFUNCVAL(io_write)},
  467. {LNILKEY, LNILVAL}
  468. };
  469. #if LUA_OPTIMIZE_MEMORY == 2
  470. static int luaL_index(lua_State *L)
  471. {
  472. return luaR_findfunction(L, iolib_funcs);
  473. }
  474. const luaL_Reg iolib[] = {
  475. {"__index", luaL_index},
  476. {NULL, NULL}
  477. };
  478. #endif
  479. #undef MIN_OPT_LEVEL
  480. #define MIN_OPT_LEVEL 1
  481. #include "lrodefs.h"
  482. const LUA_REG_TYPE flib[] = {
  483. {LSTRKEY("close"), LFUNCVAL(io_close)},
  484. {LSTRKEY("flush"), LFUNCVAL(f_flush)},
  485. {LSTRKEY("lines"), LFUNCVAL(f_lines)},
  486. {LSTRKEY("read"), LFUNCVAL(f_read)},
  487. {LSTRKEY("seek"), LFUNCVAL(f_seek)},
  488. // {LSTRKEY("setvbuf"), LFUNCVAL(f_setvbuf)},
  489. {LSTRKEY("write"), LFUNCVAL(f_write)},
  490. {LSTRKEY("__gc"), LFUNCVAL(io_gc)},
  491. {LSTRKEY("__tostring"), LFUNCVAL(io_tostring)},
  492. #if LUA_OPTIMIZE_MEMORY > 0
  493. {LSTRKEY("__index"), LROVAL(flib)},
  494. #endif
  495. {LNILKEY, LNILVAL}
  496. };
  497. static void createmeta (lua_State *L) {
  498. #if LUA_OPTIMIZE_MEMORY == 0
  499. luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
  500. lua_pushvalue(L, -1); /* push metatable */
  501. lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
  502. luaL_register(L, NULL, flib); /* file methods */
  503. #else
  504. luaL_rometatable(L, LUA_FILEHANDLE, (void*)flib); /* create metatable for file handles */
  505. #endif
  506. }
  507. static void createstdfile (lua_State *L, int f, int k, const char *fname) {
  508. *newfile(L) = f;
  509. #if LUA_OPTIMIZE_MEMORY != 2
  510. if (k > 0) {
  511. lua_pushvalue(L, -1);
  512. lua_rawseti(L, LUA_ENVIRONINDEX, k);
  513. }
  514. lua_pushvalue(L, -2); /* copy environment */
  515. lua_setfenv(L, -2); /* set it */
  516. lua_setfield(L, -3, fname);
  517. #else
  518. lua_pushvalue(L, -1);
  519. lua_rawseti(L, LUA_REGISTRYINDEX, liolib_keys[k]);
  520. lua_setfield(L, -2, fname);
  521. #endif
  522. }
  523. #if LUA_OPTIMIZE_MEMORY != 2
  524. static void newfenv (lua_State *L, lua_CFunction cls) {
  525. lua_createtable(L, 0, 1);
  526. lua_pushcfunction(L, cls);
  527. lua_setfield(L, -2, "__close");
  528. }
  529. #endif
  530. LUALIB_API int luaopen_io (lua_State *L) {
  531. createmeta(L);
  532. #if LUA_OPTIMIZE_MEMORY != 2
  533. /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
  534. newfenv(L, io_fclose);
  535. lua_replace(L, LUA_ENVIRONINDEX);
  536. /* open library */
  537. luaL_register(L, LUA_IOLIBNAME, iolib);
  538. newfenv(L, io_noclose); /* close function for default files */
  539. #else
  540. luaL_register_light(L, LUA_IOLIBNAME, iolib);
  541. lua_pushvalue(L, -1);
  542. lua_setmetatable(L, -2);
  543. #endif
  544. #if 0
  545. /* create (and set) default files */
  546. createstdfile(L, c_stdin, IO_INPUT, "stdin");
  547. createstdfile(L, c_stdout, IO_OUTPUT, "stdout");
  548. createstdfile(L, c_stderr, IO_STDERR, "stderr");
  549. #if LUA_OPTIMIZE_MEMORY != 2
  550. lua_pop(L, 1); /* pop environment for default files */
  551. lua_getfield(L, -1, "popen");
  552. newfenv(L, io_pclose); /* create environment for 'popen' */
  553. lua_setfenv(L, -2); /* set fenv for 'popen' */
  554. lua_pop(L, 1); /* pop 'popen' */
  555. #endif
  556. #endif
  557. return 1;
  558. }