liolib.c 17 KB

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