liolib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. ** $Id: liolib.c,v 2.97 2011/02/10 15:35:50 roberto Exp roberto $
  3. ** Standard I/O (and system) library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #define liolib_c
  11. #define LUA_LIB
  12. #include "lua.h"
  13. #include "lauxlib.h"
  14. #include "lualib.h"
  15. #define MAX_SIZE_T (~(size_t)0)
  16. /*
  17. ** lua_popen spawns a new process connected to the current one through
  18. ** the file streams.
  19. */
  20. #if !defined(lua_popen) /* { */
  21. #if defined(LUA_USE_POPEN) /* { */
  22. #define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m))
  23. #define lua_pclose(L,file) ((void)L, pclose(file))
  24. #if defined(LUA_USE_POSIX) /* { */
  25. #include <sys/wait.h>
  26. /*
  27. ** use appropriate macros to interpret 'pclose' return status
  28. */
  29. #define inspectstat(stat,what) \
  30. if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
  31. else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
  32. #endif /* } */
  33. #elif defined(LUA_WIN) /* }{ */
  34. #define lua_popen(L,c,m) ((void)L, _popen(c,m))
  35. #define lua_pclose(L,file) ((void)L, _pclose(file))
  36. #else /* }{ */
  37. #define lua_popen(L,c,m) ((void)((void)c, m), \
  38. luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
  39. #define lua_pclose(L,file) ((void)((void)L, file), -1)
  40. #endif /* } */
  41. #endif /* } */
  42. #if !defined(inspectstat)
  43. #define inspectstat(stat,what) /* no op */
  44. #endif
  45. #define IO_INPUT 1
  46. #define IO_OUTPUT 2
  47. static const char *const fnames[] = {"input", "output"};
  48. static int pushresult (lua_State *L, int i, const char *filename) {
  49. int en = errno; /* calls to Lua API may change this value */
  50. if (i) {
  51. lua_pushboolean(L, 1);
  52. return 1;
  53. }
  54. else {
  55. lua_pushnil(L);
  56. if (filename)
  57. lua_pushfstring(L, "%s: %s", filename, strerror(en));
  58. else
  59. lua_pushfstring(L, "%s", strerror(en));
  60. lua_pushinteger(L, en);
  61. return 3;
  62. }
  63. }
  64. static void fileerror (lua_State *L, int arg, const char *filename) {
  65. lua_pushfstring(L, "%s: %s", filename, strerror(errno));
  66. luaL_argerror(L, arg, lua_tostring(L, -1));
  67. }
  68. #define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
  69. static int io_type (lua_State *L) {
  70. void *ud;
  71. luaL_checkany(L, 1);
  72. ud = luaL_testudata(L, 1, LUA_FILEHANDLE);
  73. if (ud == NULL)
  74. lua_pushnil(L); /* not a file */
  75. else if (*((FILE **)ud) == NULL)
  76. lua_pushliteral(L, "closed file");
  77. else
  78. lua_pushliteral(L, "file");
  79. return 1;
  80. }
  81. static FILE *tofile (lua_State *L) {
  82. FILE **f = tofilep(L);
  83. if (*f == NULL)
  84. luaL_error(L, "attempt to use a closed file");
  85. return *f;
  86. }
  87. /*
  88. ** When creating file handles, always creates a `closed' file handle
  89. ** before opening the actual file; so, if there is a memory error, the
  90. ** file is not left opened.
  91. */
  92. static FILE **newprefile (lua_State *L) {
  93. FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
  94. *pf = NULL; /* file handle is currently `closed' */
  95. luaL_setmetatable(L, LUA_FILEHANDLE);
  96. return pf;
  97. }
  98. static FILE **newfile (lua_State *L) {
  99. FILE **pf = newprefile(L);
  100. lua_pushvalue(L, lua_upvalueindex(1)); /* set upvalue... */
  101. lua_setuservalue(L, -2); /* ... as environment for new file */
  102. return pf;
  103. }
  104. /*
  105. ** function to (not) close the standard files stdin, stdout, and stderr
  106. */
  107. static int io_noclose (lua_State *L) {
  108. lua_pushnil(L);
  109. lua_pushliteral(L, "cannot close standard file");
  110. return 2;
  111. }
  112. /*
  113. ** function to close 'popen' files
  114. */
  115. static int io_pclose (lua_State *L) {
  116. FILE **p = tofilep(L);
  117. int stat = lua_pclose(L, *p);
  118. const char *what = "exit"; /* type of termination */
  119. *p = NULL; /* mark stream as closed (for GC) */
  120. if (stat == -1) /* error? */
  121. return pushresult(L, 0, NULL);
  122. else {
  123. inspectstat(stat, what); /* interpret result from 'pclose' */
  124. if (*what == 'e' && stat == 0) /* successful termination? */
  125. return pushresult(L, 1, NULL);
  126. else { /* return nil,what,code */
  127. lua_pushnil(L);
  128. lua_pushstring(L, what);
  129. lua_pushinteger(L, stat);
  130. return 3;
  131. }
  132. }
  133. }
  134. /*
  135. ** function to close regular files
  136. */
  137. static int io_fclose (lua_State *L) {
  138. FILE **p = tofilep(L);
  139. int ok = (fclose(*p) == 0);
  140. *p = NULL; /* mark stream as closed (for GC) */
  141. return pushresult(L, ok, NULL);
  142. }
  143. static int aux_close (lua_State *L) {
  144. lua_getuservalue(L, 1);
  145. lua_getfield(L, -1, "__close");
  146. return (lua_tocfunction(L, -1))(L);
  147. }
  148. static int io_close (lua_State *L) {
  149. if (lua_isnone(L, 1))
  150. lua_rawgeti(L, lua_upvalueindex(1), IO_OUTPUT);
  151. tofile(L); /* make sure argument is a file */
  152. return aux_close(L);
  153. }
  154. static int io_gc (lua_State *L) {
  155. FILE *f = *tofilep(L);
  156. if (f != NULL) /* ignore closed files */
  157. aux_close(L);
  158. return 0;
  159. }
  160. static int io_tostring (lua_State *L) {
  161. FILE *f = *tofilep(L);
  162. if (f == NULL)
  163. lua_pushliteral(L, "file (closed)");
  164. else
  165. lua_pushfstring(L, "file (%p)", f);
  166. return 1;
  167. }
  168. static int io_open (lua_State *L) {
  169. const char *filename = luaL_checkstring(L, 1);
  170. const char *mode = luaL_optstring(L, 2, "r");
  171. FILE **pf;
  172. int i = 0;
  173. /* check whether 'mode' matches '[rwa]%+?b?' */
  174. if (!(mode[i] != '\0' && strchr("rwa", mode[i++]) != NULL &&
  175. (mode[i] != '+' || ++i) && /* skip if char is '+' */
  176. (mode[i] != 'b' || ++i) && /* skip if char is 'b' */
  177. (mode[i] == '\0')))
  178. luaL_error(L, "invalid mode " LUA_QL("%s")
  179. " (should match " LUA_QL("[rwa]%%+?b?") ")", mode);
  180. pf = newfile(L);
  181. *pf = fopen(filename, mode);
  182. return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
  183. }
  184. /*
  185. ** this function has a separated environment, which defines the
  186. ** correct __close for 'popen' files
  187. */
  188. static int io_popen (lua_State *L) {
  189. const char *filename = luaL_checkstring(L, 1);
  190. const char *mode = luaL_optstring(L, 2, "r");
  191. FILE **pf = newfile(L);
  192. *pf = lua_popen(L, filename, mode);
  193. return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
  194. }
  195. static int io_tmpfile (lua_State *L) {
  196. FILE **pf = newfile(L);
  197. *pf = tmpfile();
  198. return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
  199. }
  200. static FILE *getiofile (lua_State *L, int findex) {
  201. FILE *f;
  202. lua_rawgeti(L, lua_upvalueindex(1), findex);
  203. f = *(FILE **)lua_touserdata(L, -1);
  204. if (f == NULL)
  205. luaL_error(L, "standard %s file is closed", fnames[findex - 1]);
  206. return f;
  207. }
  208. static int g_iofile (lua_State *L, int f, const char *mode) {
  209. if (!lua_isnoneornil(L, 1)) {
  210. const char *filename = lua_tostring(L, 1);
  211. if (filename) {
  212. FILE **pf = newfile(L);
  213. *pf = fopen(filename, mode);
  214. if (*pf == NULL)
  215. fileerror(L, 1, filename);
  216. }
  217. else {
  218. tofile(L); /* check that it's a valid file handle */
  219. lua_pushvalue(L, 1);
  220. }
  221. lua_rawseti(L, lua_upvalueindex(1), f);
  222. }
  223. /* return current value */
  224. lua_rawgeti(L, lua_upvalueindex(1), f);
  225. return 1;
  226. }
  227. static int io_input (lua_State *L) {
  228. return g_iofile(L, IO_INPUT, "r");
  229. }
  230. static int io_output (lua_State *L) {
  231. return g_iofile(L, IO_OUTPUT, "w");
  232. }
  233. static int io_readline (lua_State *L);
  234. static void aux_lines (lua_State *L, int toclose) {
  235. int i;
  236. int n = lua_gettop(L) - 1; /* number of arguments to read */
  237. /* ensure that arguments will fit here and into 'io_readline' stack */
  238. luaL_argcheck(L, n <= LUA_MINSTACK - 3, LUA_MINSTACK - 3, "too many options");
  239. lua_pushvalue(L, 1); /* file handle */
  240. lua_pushinteger(L, n); /* number of arguments to read */
  241. lua_pushboolean(L, toclose); /* close/not close file when finished */
  242. for (i = 1; i <= n; i++) lua_pushvalue(L, i + 1); /* copy arguments */
  243. lua_pushcclosure(L, io_readline, 3 + n);
  244. }
  245. static int f_lines (lua_State *L) {
  246. tofile(L); /* check that it's a valid file handle */
  247. aux_lines(L, 0);
  248. return 1;
  249. }
  250. static int io_lines (lua_State *L) {
  251. int toclose;
  252. if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */
  253. if (lua_isnil(L, 1)) { /* no file name? */
  254. lua_rawgeti(L, lua_upvalueindex(1), IO_INPUT); /* get default input */
  255. lua_replace(L, 1); /* put it at index 1 */
  256. tofile(L); /* check that it's a valid file handle */
  257. toclose = 0; /* do not close it after iteration */
  258. }
  259. else { /* open a new file */
  260. const char *filename = luaL_checkstring(L, 1);
  261. FILE **pf = newfile(L);
  262. *pf = fopen(filename, "r");
  263. if (*pf == NULL)
  264. fileerror(L, 1, filename);
  265. lua_replace(L, 1); /* put file at index 1 */
  266. toclose = 1; /* close it after iteration */
  267. }
  268. aux_lines(L, toclose);
  269. return 1;
  270. }
  271. /*
  272. ** {======================================================
  273. ** READ
  274. ** =======================================================
  275. */
  276. static int read_number (lua_State *L, FILE *f) {
  277. lua_Number d;
  278. if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
  279. lua_pushnumber(L, d);
  280. return 1;
  281. }
  282. else {
  283. lua_pushnil(L); /* "result" to be removed */
  284. return 0; /* read fails */
  285. }
  286. }
  287. static int test_eof (lua_State *L, FILE *f) {
  288. int c = getc(f);
  289. ungetc(c, f);
  290. lua_pushlstring(L, NULL, 0);
  291. return (c != EOF);
  292. }
  293. static int read_line (lua_State *L, FILE *f, int chop) {
  294. luaL_Buffer b;
  295. luaL_buffinit(L, &b);
  296. for (;;) {
  297. size_t l;
  298. char *p = luaL_prepbuffer(&b);
  299. if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
  300. luaL_pushresult(&b); /* close buffer */
  301. return (lua_rawlen(L, -1) > 0); /* check whether read something */
  302. }
  303. l = strlen(p);
  304. if (l == 0 || p[l-1] != '\n')
  305. luaL_addsize(&b, l);
  306. else {
  307. luaL_addsize(&b, l - chop); /* chop 'eol' if needed */
  308. luaL_pushresult(&b); /* close buffer */
  309. return 1; /* read at least an `eol' */
  310. }
  311. }
  312. }
  313. static void read_all (lua_State *L, FILE *f) {
  314. size_t rlen = LUAL_BUFFERSIZE; /* how much to read in each cycle */
  315. luaL_Buffer b;
  316. luaL_buffinit(L, &b);
  317. for (;;) {
  318. char *p = luaL_prepbuffsize(&b, rlen);
  319. size_t nr = fread(p, sizeof(char), rlen, f);
  320. luaL_addsize(&b, nr);
  321. if (nr < rlen) break; /* eof? */
  322. else if (rlen <= (MAX_SIZE_T / 4)) /* avoid buffers too large */
  323. rlen *= 2; /* double buffer size at each iteration */
  324. }
  325. luaL_pushresult(&b); /* close buffer */
  326. }
  327. static int read_chars (lua_State *L, FILE *f, size_t n) {
  328. size_t nr; /* number of chars actually read */
  329. char *p;
  330. luaL_Buffer b;
  331. luaL_buffinit(L, &b);
  332. p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */
  333. nr = fread(p, sizeof(char), n, f); /* try to read 'n' chars */
  334. luaL_addsize(&b, nr);
  335. luaL_pushresult(&b); /* close buffer */
  336. return (nr > 0); /* true iff read something */
  337. }
  338. static int g_read (lua_State *L, FILE *f, int first) {
  339. int nargs = lua_gettop(L) - 1;
  340. int success;
  341. int n;
  342. clearerr(f);
  343. if (nargs == 0) { /* no arguments? */
  344. success = read_line(L, f, 1);
  345. n = first+1; /* to return 1 result */
  346. }
  347. else { /* ensure stack space for all results and for auxlib's buffer */
  348. luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
  349. success = 1;
  350. for (n = first; nargs-- && success; n++) {
  351. if (lua_type(L, n) == LUA_TNUMBER) {
  352. size_t l = (size_t)lua_tointeger(L, n);
  353. success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
  354. }
  355. else {
  356. const char *p = lua_tostring(L, n);
  357. luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
  358. switch (p[1]) {
  359. case 'n': /* number */
  360. success = read_number(L, f);
  361. break;
  362. case 'l': /* line */
  363. success = read_line(L, f, 1);
  364. break;
  365. case 'L': /* line with end-of-line */
  366. success = read_line(L, f, 0);
  367. break;
  368. case 'a': /* file */
  369. read_all(L, f); /* read entire file */
  370. success = 1; /* always success */
  371. break;
  372. default:
  373. return luaL_argerror(L, n, "invalid format");
  374. }
  375. }
  376. }
  377. }
  378. if (ferror(f))
  379. return pushresult(L, 0, NULL);
  380. if (!success) {
  381. lua_pop(L, 1); /* remove last result */
  382. lua_pushnil(L); /* push nil instead */
  383. }
  384. return n - first;
  385. }
  386. static int io_read (lua_State *L) {
  387. return g_read(L, getiofile(L, IO_INPUT), 1);
  388. }
  389. static int f_read (lua_State *L) {
  390. return g_read(L, tofile(L), 2);
  391. }
  392. static int io_readline (lua_State *L) {
  393. FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1));
  394. int i;
  395. int n = (int)lua_tointeger(L, lua_upvalueindex(2));
  396. if (f == NULL) /* file is already closed? */
  397. luaL_error(L, "file is already closed");
  398. lua_settop(L , 1);
  399. for (i = 1; i <= n; i++) /* push arguments to 'g_read' */
  400. lua_pushvalue(L, lua_upvalueindex(3 + i));
  401. n = g_read(L, f, 2); /* 'n' is number of results */
  402. lua_assert(n > 0); /* should return at least a nil */
  403. if (!lua_isnil(L, -n)) /* read at least one value? */
  404. return n; /* return them */
  405. else { /* first result is nil: EOF or error */
  406. if (!lua_isnil(L, -1)) /* is there error information? */
  407. return luaL_error(L, "%s", lua_tostring(L, -1)); /* error */
  408. /* else EOF */
  409. if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */
  410. lua_settop(L, 0);
  411. lua_pushvalue(L, lua_upvalueindex(1));
  412. aux_close(L); /* close it */
  413. }
  414. return 0;
  415. }
  416. }
  417. /* }====================================================== */
  418. static int g_write (lua_State *L, FILE *f, int arg) {
  419. int nargs = lua_gettop(L) - arg;
  420. int status = 1;
  421. for (; nargs--; arg++) {
  422. if (lua_type(L, arg) == LUA_TNUMBER) {
  423. /* optimization: could be done exactly as for strings */
  424. status = status &&
  425. fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
  426. }
  427. else {
  428. size_t l;
  429. const char *s = luaL_checklstring(L, arg, &l);
  430. status = status && (fwrite(s, sizeof(char), l, f) == l);
  431. }
  432. }
  433. if (status) return 1; /* file handle already on stack top */
  434. else return pushresult(L, status, NULL);
  435. }
  436. static int io_write (lua_State *L) {
  437. return g_write(L, getiofile(L, IO_OUTPUT), 1);
  438. }
  439. static int f_write (lua_State *L) {
  440. FILE * f = tofile(L);
  441. lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */
  442. return g_write(L, f, 2);
  443. }
  444. static int f_seek (lua_State *L) {
  445. static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
  446. static const char *const modenames[] = {"set", "cur", "end", NULL};
  447. FILE *f = tofile(L);
  448. int op = luaL_checkoption(L, 2, "cur", modenames);
  449. long offset = luaL_optlong(L, 3, 0);
  450. op = fseek(f, offset, mode[op]);
  451. if (op)
  452. return pushresult(L, 0, NULL); /* error */
  453. else {
  454. lua_pushinteger(L, ftell(f));
  455. return 1;
  456. }
  457. }
  458. static int f_setvbuf (lua_State *L) {
  459. static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
  460. static const char *const modenames[] = {"no", "full", "line", NULL};
  461. FILE *f = tofile(L);
  462. int op = luaL_checkoption(L, 2, NULL, modenames);
  463. lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
  464. int res = setvbuf(f, NULL, mode[op], sz);
  465. return pushresult(L, res == 0, NULL);
  466. }
  467. static int io_flush (lua_State *L) {
  468. return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
  469. }
  470. static int f_flush (lua_State *L) {
  471. return pushresult(L, fflush(tofile(L)) == 0, NULL);
  472. }
  473. static const luaL_Reg iolib[] = {
  474. {"close", io_close},
  475. {"flush", io_flush},
  476. {"input", io_input},
  477. {"lines", io_lines},
  478. {"open", io_open},
  479. {"output", io_output},
  480. {"popen", io_popen},
  481. {"read", io_read},
  482. {"tmpfile", io_tmpfile},
  483. {"type", io_type},
  484. {"write", io_write},
  485. {NULL, NULL}
  486. };
  487. static const luaL_Reg flib[] = {
  488. {"close", io_close},
  489. {"flush", f_flush},
  490. {"lines", f_lines},
  491. {"read", f_read},
  492. {"seek", f_seek},
  493. {"setvbuf", f_setvbuf},
  494. {"write", f_write},
  495. {"__gc", io_gc},
  496. {"__tostring", io_tostring},
  497. {NULL, NULL}
  498. };
  499. static void createmeta (lua_State *L) {
  500. luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
  501. lua_pushvalue(L, -1); /* push metatable */
  502. lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
  503. luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */
  504. lua_pop(L, 1); /* pop new metatable */
  505. }
  506. static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) {
  507. *newprefile(L) = f;
  508. if (k > 0) {
  509. lua_pushvalue(L, -1); /* copy new file */
  510. lua_rawseti(L, 1, k); /* add it to common upvalue */
  511. }
  512. lua_pushvalue(L, 3); /* get environment for default files */
  513. lua_setuservalue(L, -2); /* set it as environment for file */
  514. lua_setfield(L, 2, fname); /* add file to module */
  515. }
  516. /*
  517. ** pushes a new table with {__close = cls}
  518. */
  519. static void newenv (lua_State *L, lua_CFunction cls) {
  520. lua_createtable(L, 0, 1);
  521. lua_pushcfunction(L, cls);
  522. lua_setfield(L, -2, "__close");
  523. }
  524. LUAMOD_API int luaopen_io (lua_State *L) {
  525. lua_settop(L, 0);
  526. createmeta(L);
  527. /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
  528. newenv(L, io_fclose); /* upvalue for all io functions at index 1 */
  529. luaL_newlibtable(L, iolib); /* new module at index 2 */
  530. lua_pushvalue(L, 1); /* copy of env to be consumed by 'setfuncs' */
  531. luaL_setfuncs(L, iolib, 1);
  532. /* create (and set) default files */
  533. newenv(L, io_noclose); /* environment for default files at index 3 */
  534. createstdfile(L, stdin, IO_INPUT, "stdin");
  535. createstdfile(L, stdout, IO_OUTPUT, "stdout");
  536. createstdfile(L, stderr, 0, "stderr");
  537. lua_pop(L, 1); /* pop environment for default files */
  538. lua_getfield(L, 2, "popen");
  539. newenv(L, io_pclose); /* create environment for 'popen' streams */
  540. lua_setupvalue(L, -2, 1); /* set it as upvalue for 'popen' */
  541. lua_pop(L, 1); /* pop 'popen' */
  542. return 1;
  543. }