liolib.c 18 KB

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