liolib.c 17 KB

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