liolib.c 18 KB

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