liolib.c 18 KB

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