liolib.c 17 KB

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