liolib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. ** $Id: liolib.c,v 2.20 2002/10/11 20:40:32 roberto Exp roberto $
  3. ** Standard I/O (and system) library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <errno.h>
  7. #include <locale.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include "lua.h"
  13. #include "lauxlib.h"
  14. #include "lualib.h"
  15. /*
  16. ** {======================================================
  17. ** FILE Operations
  18. ** =======================================================
  19. */
  20. #ifndef USE_POPEN
  21. #define pclose(f) (-1)
  22. #endif
  23. #define FILEHANDLE "FileHandle"
  24. #define IO_INPUT "_input"
  25. #define IO_OUTPUT "_output"
  26. static int pushresult (lua_State *L, int i) {
  27. if (i) {
  28. lua_pushboolean(L, 1);
  29. return 1;
  30. }
  31. else {
  32. lua_pushnil(L);
  33. lua_pushstring(L, strerror(errno));
  34. lua_pushnumber(L, errno);
  35. return 3;
  36. }
  37. }
  38. static FILE *tofile (lua_State *L, int findex) {
  39. FILE **f = (FILE **)lua_touserdata(L, findex);
  40. if (f && *f && lua_getmetatable(L, findex) &&
  41. lua_rawequal(L, -1, lua_upvalueindex(1))) {
  42. lua_pop(L, 1);
  43. return *f;
  44. }
  45. if (findex > 0) {
  46. if (f && *f == NULL)
  47. luaL_error(L, "attempt to use a closed file");
  48. else
  49. luaL_argerror(L, findex, "bad file");
  50. }
  51. return NULL;
  52. }
  53. /*
  54. ** When creating file handles, always creates a `closed' file handle
  55. ** before opening the actual file; so, if there is a memory error, the
  56. ** file is not left opened.
  57. */
  58. static FILE **newfile (lua_State *L) {
  59. FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
  60. *pf = NULL; /* file handle is currently `closed' */
  61. lua_pushliteral(L, FILEHANDLE);
  62. lua_rawget(L, LUA_REGISTRYINDEX);
  63. lua_setmetatable(L, -2);
  64. return pf;
  65. }
  66. static void registerfile (lua_State *L, FILE *f, const char *name,
  67. const char *impname) {
  68. lua_pushstring(L, name);
  69. *newfile(L) = f;
  70. if (impname) {
  71. lua_pushstring(L, impname);
  72. lua_pushvalue(L, -2);
  73. lua_settable(L, -6);
  74. }
  75. lua_settable(L, -3);
  76. }
  77. static int aux_close (lua_State *L) {
  78. FILE *f = tofile(L, 1);
  79. if (f == stdin || f == stdout || f == stderr)
  80. return 0; /* file cannot be closed */
  81. *(FILE **)lua_touserdata(L, 1) = NULL; /* mark file as closed */
  82. return (pclose(f) != -1) || (fclose(f) == 0);
  83. }
  84. static int io_close (lua_State *L) {
  85. if (lua_isnone(L, 1)) {
  86. lua_pushstring(L, IO_OUTPUT);
  87. lua_rawget(L, lua_upvalueindex(1));
  88. }
  89. return pushresult(L, aux_close(L));
  90. }
  91. static int io_gc (lua_State *L) {
  92. FILE **f = (FILE **)lua_touserdata(L, 1);
  93. if (!(f && *f == NULL)) /* ignore closed files */
  94. aux_close(L);
  95. return 0;
  96. }
  97. static int io_open (lua_State *L) {
  98. const char *filename = luaL_check_string(L, 1);
  99. const char *mode = luaL_opt_string(L, 2, "r");
  100. FILE **pf = newfile(L);
  101. *pf = fopen(filename, mode);
  102. return (*pf == NULL) ? pushresult(L, 0) : 1;
  103. }
  104. static int io_popen (lua_State *L) {
  105. #ifndef USE_POPEN
  106. luaL_error(L, "`popen' not supported");
  107. return 0;
  108. #else
  109. const char *filename = luaL_check_string(L, 1);
  110. const char *mode = luaL_opt_string(L, 2, "r");
  111. FILE **pf = newfile(L);
  112. *pf = popen(filename, mode);
  113. return (*pf == NULL) ? pushresult(L, 0) : 1;
  114. #endif
  115. }
  116. static int io_tmpfile (lua_State *L) {
  117. FILE **pf = newfile(L);
  118. *pf = tmpfile();
  119. return (*pf == NULL) ? pushresult(L, 0) : 1;
  120. }
  121. static FILE *getiofile (lua_State *L, const char *name) {
  122. FILE *f;
  123. lua_pushstring(L, name);
  124. lua_rawget(L, lua_upvalueindex(1));
  125. f = tofile(L, -1);
  126. if (f == NULL)
  127. luaL_error(L, "%s is closed", name);
  128. return f;
  129. }
  130. static int g_iofile (lua_State *L, const char *name, const char *mode) {
  131. if (lua_isnoneornil(L, 1)) {
  132. lua_pushstring(L, name);
  133. lua_rawget(L, lua_upvalueindex(1));
  134. return 1;
  135. }
  136. else {
  137. const char *filename = lua_tostring(L, 1);
  138. lua_pushstring(L, name);
  139. if (filename) {
  140. FILE **pf = newfile(L);
  141. *pf = fopen(filename, mode);
  142. luaL_arg_check(L, *pf, 1, strerror(errno));
  143. }
  144. else {
  145. tofile(L, 1); /* check that it's a valid file handle */
  146. lua_pushvalue(L, 1);
  147. }
  148. lua_rawset(L, lua_upvalueindex(1));
  149. return 0;
  150. }
  151. }
  152. static int io_input (lua_State *L) {
  153. return g_iofile(L, IO_INPUT, "r");
  154. }
  155. static int io_output (lua_State *L) {
  156. return g_iofile(L, IO_OUTPUT, "w");
  157. }
  158. static int io_readline (lua_State *L);
  159. static void aux_lines (lua_State *L, int index, int close) {
  160. lua_pushliteral(L, FILEHANDLE);
  161. lua_rawget(L, LUA_REGISTRYINDEX);
  162. lua_pushvalue(L, index);
  163. lua_pushboolean(L, close); /* close/not close file when finished */
  164. lua_pushcclosure(L, io_readline, 3);
  165. }
  166. static int f_lines (lua_State *L) {
  167. tofile(L, 1); /* check that it's a valid file handle */
  168. aux_lines(L, 1, 0);
  169. return 1;
  170. }
  171. static int io_lines (lua_State *L) {
  172. if (lua_isnoneornil(L, 1)) { /* no arguments? */
  173. lua_pushstring(L, IO_INPUT);
  174. lua_rawget(L, lua_upvalueindex(1)); /* will iterate over default input */
  175. return f_lines(L);
  176. }
  177. else {
  178. const char *filename = luaL_check_string(L, 1);
  179. FILE **pf = newfile(L);
  180. *pf = fopen(filename, "r");
  181. luaL_arg_check(L, *pf, 1, strerror(errno));
  182. aux_lines(L, lua_gettop(L), 1);
  183. return 1;
  184. }
  185. }
  186. /*
  187. ** {======================================================
  188. ** READ
  189. ** =======================================================
  190. */
  191. static int read_number (lua_State *L, FILE *f) {
  192. lua_Number d;
  193. if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
  194. lua_pushnumber(L, d);
  195. return 1;
  196. }
  197. else return 0; /* read fails */
  198. }
  199. static int test_eof (lua_State *L, FILE *f) {
  200. int c = getc(f);
  201. ungetc(c, f);
  202. lua_pushlstring(L, NULL, 0);
  203. return (c != EOF);
  204. }
  205. static int read_line (lua_State *L, FILE *f) {
  206. luaL_Buffer b;
  207. luaL_buffinit(L, &b);
  208. for (;;) {
  209. size_t l;
  210. char *p = luaL_prepbuffer(&b);
  211. if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
  212. luaL_pushresult(&b); /* close buffer */
  213. return (lua_strlen(L, -1) > 0); /* check whether read something */
  214. }
  215. l = strlen(p);
  216. if (p[l-1] != '\n')
  217. luaL_addsize(&b, l);
  218. else {
  219. luaL_addsize(&b, l - 1); /* do not include `eol' */
  220. luaL_pushresult(&b); /* close buffer */
  221. return 1; /* read at least an `eol' */
  222. }
  223. }
  224. }
  225. static int read_chars (lua_State *L, FILE *f, size_t n) {
  226. size_t rlen; /* how much to read */
  227. size_t nr; /* number of chars actually read */
  228. luaL_Buffer b;
  229. luaL_buffinit(L, &b);
  230. rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
  231. do {
  232. char *p = luaL_prepbuffer(&b);
  233. if (rlen > n) rlen = n; /* cannot read more than asked */
  234. nr = fread(p, sizeof(char), rlen, f);
  235. luaL_addsize(&b, nr);
  236. n -= nr; /* still have to read `n' chars */
  237. } while (n > 0 && nr == rlen); /* until end of count or eof */
  238. luaL_pushresult(&b); /* close buffer */
  239. return (n == 0 || lua_strlen(L, -1) > 0);
  240. }
  241. static int g_read (lua_State *L, FILE *f, int first) {
  242. int nargs = lua_gettop(L) - 1;
  243. int success;
  244. int n;
  245. if (nargs == 0) { /* no arguments? */
  246. success = read_line(L, f);
  247. n = first+1; /* to return 1 result */
  248. }
  249. else { /* ensure stack space for all results and for auxlib's buffer */
  250. luaL_check_stack(L, nargs+LUA_MINSTACK, "too many arguments");
  251. success = 1;
  252. for (n = first; nargs-- && success; n++) {
  253. if (lua_type(L, n) == LUA_TNUMBER) {
  254. size_t l = (size_t)lua_tonumber(L, n);
  255. success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
  256. }
  257. else {
  258. const char *p = lua_tostring(L, n);
  259. if (!p || p[0] != '*')
  260. return luaL_error(L, "invalid `read' option");
  261. switch (p[1]) {
  262. case 'n': /* number */
  263. success = read_number(L, f);
  264. break;
  265. case 'l': /* line */
  266. success = read_line(L, f);
  267. break;
  268. case 'a': /* file */
  269. read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
  270. success = 1; /* always success */
  271. break;
  272. case 'w': /* word */
  273. return luaL_error(L, "obsolete option `*w'");
  274. default:
  275. return luaL_argerror(L, n, "invalid format");
  276. }
  277. }
  278. }
  279. }
  280. if (!success) {
  281. lua_pop(L, 1); /* remove last result */
  282. lua_pushnil(L); /* push nil instead */
  283. }
  284. return n - first;
  285. }
  286. static int io_read (lua_State *L) {
  287. return g_read(L, getiofile(L, IO_INPUT), 1);
  288. }
  289. static int f_read (lua_State *L) {
  290. return g_read(L, tofile(L, 1), 2);
  291. }
  292. static int io_readline (lua_State *L) {
  293. FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(2));
  294. if (f == NULL) /* file is already closed? */
  295. luaL_error(L, "file is already closed");
  296. if (read_line(L, f)) return 1;
  297. else { /* EOF */
  298. if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */
  299. lua_settop(L, 0);
  300. lua_pushvalue(L, lua_upvalueindex(2));
  301. aux_close(L); /* close it */
  302. }
  303. return 0;
  304. }
  305. }
  306. /* }====================================================== */
  307. static int g_write (lua_State *L, FILE *f, int arg) {
  308. int nargs = lua_gettop(L) - 1;
  309. int status = 1;
  310. for (; nargs--; arg++) {
  311. if (lua_type(L, arg) == LUA_TNUMBER) {
  312. /* optimization: could be done exactly as for strings */
  313. status = status &&
  314. fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
  315. }
  316. else {
  317. size_t l;
  318. const char *s = luaL_check_lstr(L, arg, &l);
  319. status = status && (fwrite(s, sizeof(char), l, f) == l);
  320. }
  321. }
  322. pushresult(L, status);
  323. return 1;
  324. }
  325. static int io_write (lua_State *L) {
  326. return g_write(L, getiofile(L, IO_OUTPUT), 1);
  327. }
  328. static int f_write (lua_State *L) {
  329. return g_write(L, tofile(L, 1), 2);
  330. }
  331. static int f_seek (lua_State *L) {
  332. static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
  333. static const char *const modenames[] = {"set", "cur", "end", NULL};
  334. FILE *f = tofile(L, 1);
  335. int op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames);
  336. long offset = luaL_opt_long(L, 3, 0);
  337. luaL_arg_check(L, op != -1, 2, "invalid mode");
  338. op = fseek(f, offset, mode[op]);
  339. if (op)
  340. return pushresult(L, 0); /* error */
  341. else {
  342. lua_pushnumber(L, ftell(f));
  343. return 1;
  344. }
  345. }
  346. static int io_flush (lua_State *L) {
  347. return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0);
  348. }
  349. static int f_flush (lua_State *L) {
  350. return pushresult(L, fflush(tofile(L, 1)) == 0);
  351. }
  352. static const luaL_reg iolib[] = {
  353. {"input", io_input},
  354. {"output", io_output},
  355. {"lines", io_lines},
  356. {"close", io_close},
  357. {"flush", io_flush},
  358. {"open", io_open},
  359. {"popen", io_popen},
  360. {"read", io_read},
  361. {"tmpfile", io_tmpfile},
  362. {"write", io_write},
  363. {NULL, NULL}
  364. };
  365. static const luaL_reg flib[] = {
  366. {"flush", f_flush},
  367. {"read", f_read},
  368. {"lines", f_lines},
  369. {"seek", f_seek},
  370. {"write", f_write},
  371. {"close", io_close},
  372. {NULL, NULL}
  373. };
  374. static void createmeta (lua_State *L) {
  375. lua_pushliteral(L, FILEHANDLE);
  376. lua_newtable(L); /* push new metatable for file handles */
  377. /* close files when collected */
  378. lua_pushliteral(L, "__gc");
  379. lua_pushvalue(L, -2); /* push metatable (will be upvalue for `gc' method) */
  380. lua_pushcclosure(L, io_gc, 1);
  381. lua_rawset(L, -3); /* metatable.__gc = io_gc */
  382. /* file methods */
  383. lua_pushliteral(L, "__index");
  384. lua_pushvalue(L, -2); /* push metatable */
  385. lua_rawset(L, -3); /* metatable.__index = metatable */
  386. lua_pushvalue(L, -1); /* push metatable (will be upvalue for library) */
  387. luaL_openlib(L, flib, 1);
  388. lua_rawset(L, LUA_REGISTRYINDEX); /* registry.FILEHANDLE = metatable */
  389. }
  390. /* }====================================================== */
  391. /*
  392. ** {======================================================
  393. ** Other O.S. Operations
  394. ** =======================================================
  395. */
  396. static int io_execute (lua_State *L) {
  397. lua_pushnumber(L, system(luaL_check_string(L, 1)));
  398. return 1;
  399. }
  400. static int io_remove (lua_State *L) {
  401. return pushresult(L, remove(luaL_check_string(L, 1)) == 0);
  402. }
  403. static int io_rename (lua_State *L) {
  404. return pushresult(L, rename(luaL_check_string(L, 1),
  405. luaL_check_string(L, 2)) == 0);
  406. }
  407. static int io_tmpname (lua_State *L) {
  408. char buff[L_tmpnam];
  409. if (tmpnam(buff) != buff)
  410. return luaL_error(L, "unable to generate a unique filename");
  411. lua_pushstring(L, buff);
  412. return 1;
  413. }
  414. static int io_getenv (lua_State *L) {
  415. lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */
  416. return 1;
  417. }
  418. static int io_clock (lua_State *L) {
  419. lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
  420. return 1;
  421. }
  422. /*
  423. ** {======================================================
  424. ** Time/Date operations
  425. ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
  426. ** wday=%w+1, yday=%j, isdst=? }
  427. ** =======================================================
  428. */
  429. static void setfield (lua_State *L, const char *key, int value) {
  430. lua_pushstring(L, key);
  431. lua_pushnumber(L, value);
  432. lua_rawset(L, -3);
  433. }
  434. static void setboolfield (lua_State *L, const char *key, int value) {
  435. lua_pushstring(L, key);
  436. lua_pushboolean(L, value);
  437. lua_rawset(L, -3);
  438. }
  439. static int getboolfield (lua_State *L, const char *key) {
  440. int res;
  441. lua_pushstring(L, key);
  442. lua_gettable(L, -2);
  443. res = lua_toboolean(L, -1);
  444. lua_pop(L, 1);
  445. return res;
  446. }
  447. static int getfield (lua_State *L, const char *key, int d) {
  448. int res;
  449. lua_pushstring(L, key);
  450. lua_gettable(L, -2);
  451. if (lua_isnumber(L, -1))
  452. res = (int)(lua_tonumber(L, -1));
  453. else {
  454. if (d == -2)
  455. return luaL_error(L, "field `%s' missing in date table", key);
  456. res = d;
  457. }
  458. lua_pop(L, 1);
  459. return res;
  460. }
  461. static int io_date (lua_State *L) {
  462. const char *s = luaL_opt_string(L, 1, "%c");
  463. time_t t = (time_t)(luaL_opt_number(L, 2, -1));
  464. struct tm *stm;
  465. if (t == (time_t)(-1)) /* no time given? */
  466. t = time(NULL); /* use current time */
  467. if (*s == '!') { /* UTC? */
  468. stm = gmtime(&t);
  469. s++; /* skip `!' */
  470. }
  471. else
  472. stm = localtime(&t);
  473. if (stm == NULL) /* invalid date? */
  474. lua_pushnil(L);
  475. else if (strcmp(s, "*t") == 0) {
  476. lua_newtable(L);
  477. setfield(L, "sec", stm->tm_sec);
  478. setfield(L, "min", stm->tm_min);
  479. setfield(L, "hour", stm->tm_hour);
  480. setfield(L, "day", stm->tm_mday);
  481. setfield(L, "month", stm->tm_mon+1);
  482. setfield(L, "year", stm->tm_year+1900);
  483. setfield(L, "wday", stm->tm_wday+1);
  484. setfield(L, "yday", stm->tm_yday+1);
  485. setboolfield(L, "isdst", stm->tm_isdst);
  486. }
  487. else {
  488. char b[256];
  489. if (strftime(b, sizeof(b), s, stm))
  490. lua_pushstring(L, b);
  491. else
  492. return luaL_error(L, "`date' format too long");
  493. }
  494. return 1;
  495. }
  496. static int io_time (lua_State *L) {
  497. if (lua_isnoneornil(L, 1)) /* called without args? */
  498. lua_pushnumber(L, time(NULL)); /* return current time */
  499. else {
  500. time_t t;
  501. struct tm ts;
  502. luaL_check_type(L, 1, LUA_TTABLE);
  503. lua_settop(L, 1); /* make sure table is at the top */
  504. ts.tm_sec = getfield(L, "sec", 0);
  505. ts.tm_min = getfield(L, "min", 0);
  506. ts.tm_hour = getfield(L, "hour", 12);
  507. ts.tm_mday = getfield(L, "day", -2);
  508. ts.tm_mon = getfield(L, "month", -2) - 1;
  509. ts.tm_year = getfield(L, "year", -2) - 1900;
  510. ts.tm_isdst = getboolfield(L, "isdst");
  511. t = mktime(&ts);
  512. if (t == (time_t)(-1))
  513. lua_pushnil(L);
  514. else
  515. lua_pushnumber(L, t);
  516. }
  517. return 1;
  518. }
  519. static int io_difftime (lua_State *L) {
  520. lua_pushnumber(L, difftime((time_t)(luaL_check_number(L, 1)),
  521. (time_t)(luaL_opt_number(L, 2, 0))));
  522. return 1;
  523. }
  524. /* }====================================================== */
  525. static int io_setloc (lua_State *L) {
  526. static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
  527. LC_NUMERIC, LC_TIME};
  528. static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
  529. "numeric", "time", NULL};
  530. const char *l = lua_tostring(L, 1);
  531. int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames);
  532. luaL_arg_check(L, l || lua_isnoneornil(L, 1), 1, "string expected");
  533. luaL_arg_check(L, op != -1, 2, "invalid option");
  534. lua_pushstring(L, setlocale(cat[op], l));
  535. return 1;
  536. }
  537. static int io_exit (lua_State *L) {
  538. exit(luaL_opt_int(L, 1, EXIT_SUCCESS));
  539. return 0; /* to avoid warnings */
  540. }
  541. static const luaL_reg syslib[] = {
  542. {"clock", io_clock},
  543. {"date", io_date},
  544. {"difftime", io_difftime},
  545. {"execute", io_execute},
  546. {"exit", io_exit},
  547. {"getenv", io_getenv},
  548. {"remove", io_remove},
  549. {"rename", io_rename},
  550. {"setlocale", io_setloc},
  551. {"time", io_time},
  552. {"tmpname", io_tmpname},
  553. {NULL, NULL}
  554. };
  555. /* }====================================================== */
  556. LUALIB_API int lua_iolibopen (lua_State *L) {
  557. createmeta(L);
  558. luaL_opennamedlib(L, LUA_OSLIBNAME, syslib, 0);
  559. lua_pushliteral(L, FILEHANDLE); /* S: FH */
  560. lua_rawget(L, LUA_REGISTRYINDEX); /* S: mt */
  561. lua_pushvalue(L, -1); /* S: mt mt */
  562. luaL_opennamedlib(L, LUA_IOLIBNAME, iolib, 1); /* S: mt */
  563. lua_pushliteral(L, LUA_IOLIBNAME); /* S: `io' mt */
  564. lua_gettable(L, LUA_GLOBALSINDEX); /* S: io mt */
  565. /* put predefined file handles into `io' table */
  566. registerfile(L, stdin, "stdin", IO_INPUT);
  567. registerfile(L, stdout, "stdout", IO_OUTPUT);
  568. registerfile(L, stderr, "stderr", NULL);
  569. lua_pop(L, 2); /* S: empty */
  570. return 0;
  571. }