liolib.c 17 KB

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