liolib.c 18 KB

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