liolib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /*
  2. ** $Id: liolib.c,v 2.38 2003/03/18 12:25:32 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. luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
  306. switch (p[1]) {
  307. case 'n': /* number */
  308. success = read_number(L, f);
  309. break;
  310. case 'l': /* line */
  311. success = read_line(L, f);
  312. break;
  313. case 'a': /* file */
  314. read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
  315. success = 1; /* always success */
  316. break;
  317. case 'w': /* word */
  318. return luaL_error(L, "obsolete option `*w' to `read'");
  319. default:
  320. return luaL_argerror(L, n, "invalid format");
  321. }
  322. }
  323. }
  324. }
  325. if (!success) {
  326. lua_pop(L, 1); /* remove last result */
  327. lua_pushnil(L); /* push nil instead */
  328. }
  329. return n - first;
  330. }
  331. static int io_read (lua_State *L) {
  332. return g_read(L, getiofile(L, IO_INPUT), 1);
  333. }
  334. static int f_read (lua_State *L) {
  335. return g_read(L, tofile(L, 1), 2);
  336. }
  337. static int io_readline (lua_State *L) {
  338. FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(2));
  339. if (f == NULL) /* file is already closed? */
  340. luaL_error(L, "file is already closed");
  341. if (read_line(L, f)) return 1;
  342. else { /* EOF */
  343. if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */
  344. lua_settop(L, 0);
  345. lua_pushvalue(L, lua_upvalueindex(2));
  346. aux_close(L); /* close it */
  347. }
  348. return 0;
  349. }
  350. }
  351. /* }====================================================== */
  352. static int g_write (lua_State *L, FILE *f, int arg) {
  353. int nargs = lua_gettop(L) - 1;
  354. int status = 1;
  355. for (; nargs--; arg++) {
  356. if (lua_type(L, arg) == LUA_TNUMBER) {
  357. /* optimization: could be done exactly as for strings */
  358. status = status &&
  359. fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
  360. }
  361. else {
  362. size_t l;
  363. const char *s = luaL_checklstring(L, arg, &l);
  364. status = status && (fwrite(s, sizeof(char), l, f) == l);
  365. }
  366. }
  367. return pushresult(L, status, NULL);
  368. }
  369. static int io_write (lua_State *L) {
  370. return g_write(L, getiofile(L, IO_OUTPUT), 1);
  371. }
  372. static int f_write (lua_State *L) {
  373. return g_write(L, tofile(L, 1), 2);
  374. }
  375. static int f_seek (lua_State *L) {
  376. static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
  377. static const char *const modenames[] = {"set", "cur", "end", NULL};
  378. FILE *f = tofile(L, 1);
  379. int op = luaL_findstring(luaL_optstring(L, 2, "cur"), modenames);
  380. long offset = luaL_optlong(L, 3, 0);
  381. luaL_argcheck(L, op != -1, 2, "invalid mode");
  382. op = fseek(f, offset, mode[op]);
  383. if (op)
  384. return pushresult(L, 0, NULL); /* error */
  385. else {
  386. lua_pushnumber(L, ftell(f));
  387. return 1;
  388. }
  389. }
  390. static int io_flush (lua_State *L) {
  391. return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
  392. }
  393. static int f_flush (lua_State *L) {
  394. return pushresult(L, fflush(tofile(L, 1)) == 0, NULL);
  395. }
  396. static const luaL_reg iolib[] = {
  397. {"input", io_input},
  398. {"output", io_output},
  399. {"lines", io_lines},
  400. {"close", io_close},
  401. {"flush", io_flush},
  402. {"open", io_open},
  403. {"popen", io_popen},
  404. {"read", io_read},
  405. {"tmpfile", io_tmpfile},
  406. {"type", io_type},
  407. {"write", io_write},
  408. {NULL, NULL}
  409. };
  410. static const luaL_reg flib[] = {
  411. {"flush", f_flush},
  412. {"read", f_read},
  413. {"lines", f_lines},
  414. {"seek", f_seek},
  415. {"write", f_write},
  416. {"close", io_close},
  417. {"__gc", io_gc},
  418. {"__tostring", io_tostring},
  419. {NULL, NULL}
  420. };
  421. static void createmeta (lua_State *L) {
  422. luaL_newmetatable(L, FILEHANDLE); /* create new metatable for file handles */
  423. /* file methods */
  424. lua_pushliteral(L, "__index");
  425. lua_pushvalue(L, -2); /* push metatable */
  426. lua_rawset(L, -3); /* metatable.__index = metatable */
  427. luaL_openlib(L, NULL, flib, 0);
  428. }
  429. /* }====================================================== */
  430. /*
  431. ** {======================================================
  432. ** Other O.S. Operations
  433. ** =======================================================
  434. */
  435. static int io_execute (lua_State *L) {
  436. lua_pushnumber(L, system(luaL_checkstring(L, 1)));
  437. return 1;
  438. }
  439. static int io_remove (lua_State *L) {
  440. const char *filename = luaL_checkstring(L, 1);
  441. return pushresult(L, remove(filename) == 0, filename);
  442. }
  443. static int io_rename (lua_State *L) {
  444. const char *fromname = luaL_checkstring(L, 1);
  445. const char *toname = luaL_checkstring(L, 2);
  446. return pushresult(L, rename(fromname, toname) == 0, fromname);
  447. }
  448. static int io_tmpname (lua_State *L) {
  449. #if !USE_TMPNAME
  450. luaL_error(L, "`tmpname' not supported");
  451. return 0;
  452. #else
  453. char buff[L_tmpnam];
  454. if (tmpnam(buff) != buff)
  455. return luaL_error(L, "unable to generate a unique filename in `tmpname'");
  456. lua_pushstring(L, buff);
  457. return 1;
  458. #endif
  459. }
  460. static int io_getenv (lua_State *L) {
  461. lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
  462. return 1;
  463. }
  464. static int io_clock (lua_State *L) {
  465. lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
  466. return 1;
  467. }
  468. /*
  469. ** {======================================================
  470. ** Time/Date operations
  471. ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
  472. ** wday=%w+1, yday=%j, isdst=? }
  473. ** =======================================================
  474. */
  475. static void setfield (lua_State *L, const char *key, int value) {
  476. lua_pushstring(L, key);
  477. lua_pushnumber(L, value);
  478. lua_rawset(L, -3);
  479. }
  480. static void setboolfield (lua_State *L, const char *key, int value) {
  481. lua_pushstring(L, key);
  482. lua_pushboolean(L, value);
  483. lua_rawset(L, -3);
  484. }
  485. static int getboolfield (lua_State *L, const char *key) {
  486. int res;
  487. lua_pushstring(L, key);
  488. lua_gettable(L, -2);
  489. res = lua_toboolean(L, -1);
  490. lua_pop(L, 1);
  491. return res;
  492. }
  493. static int getfield (lua_State *L, const char *key, int d) {
  494. int res;
  495. lua_pushstring(L, key);
  496. lua_gettable(L, -2);
  497. if (lua_isnumber(L, -1))
  498. res = (int)(lua_tonumber(L, -1));
  499. else {
  500. if (d == -2)
  501. return luaL_error(L, "field `%s' missing in date table", key);
  502. res = d;
  503. }
  504. lua_pop(L, 1);
  505. return res;
  506. }
  507. static int io_date (lua_State *L) {
  508. const char *s = luaL_optstring(L, 1, "%c");
  509. time_t t = (time_t)(luaL_optnumber(L, 2, -1));
  510. struct tm *stm;
  511. if (t == (time_t)(-1)) /* no time given? */
  512. t = time(NULL); /* use current time */
  513. if (*s == '!') { /* UTC? */
  514. stm = gmtime(&t);
  515. s++; /* skip `!' */
  516. }
  517. else
  518. stm = localtime(&t);
  519. if (stm == NULL) /* invalid date? */
  520. lua_pushnil(L);
  521. else if (strcmp(s, "*t") == 0) {
  522. lua_newtable(L);
  523. setfield(L, "sec", stm->tm_sec);
  524. setfield(L, "min", stm->tm_min);
  525. setfield(L, "hour", stm->tm_hour);
  526. setfield(L, "day", stm->tm_mday);
  527. setfield(L, "month", stm->tm_mon+1);
  528. setfield(L, "year", stm->tm_year+1900);
  529. setfield(L, "wday", stm->tm_wday+1);
  530. setfield(L, "yday", stm->tm_yday+1);
  531. setboolfield(L, "isdst", stm->tm_isdst);
  532. }
  533. else {
  534. char b[256];
  535. if (strftime(b, sizeof(b), s, stm))
  536. lua_pushstring(L, b);
  537. else
  538. return luaL_error(L, "`date' format too long");
  539. }
  540. return 1;
  541. }
  542. static int io_time (lua_State *L) {
  543. if (lua_isnoneornil(L, 1)) /* called without args? */
  544. lua_pushnumber(L, time(NULL)); /* return current time */
  545. else {
  546. time_t t;
  547. struct tm ts;
  548. luaL_checktype(L, 1, LUA_TTABLE);
  549. lua_settop(L, 1); /* make sure table is at the top */
  550. ts.tm_sec = getfield(L, "sec", 0);
  551. ts.tm_min = getfield(L, "min", 0);
  552. ts.tm_hour = getfield(L, "hour", 12);
  553. ts.tm_mday = getfield(L, "day", -2);
  554. ts.tm_mon = getfield(L, "month", -2) - 1;
  555. ts.tm_year = getfield(L, "year", -2) - 1900;
  556. ts.tm_isdst = getboolfield(L, "isdst");
  557. t = mktime(&ts);
  558. if (t == (time_t)(-1))
  559. lua_pushnil(L);
  560. else
  561. lua_pushnumber(L, t);
  562. }
  563. return 1;
  564. }
  565. static int io_difftime (lua_State *L) {
  566. lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
  567. (time_t)(luaL_optnumber(L, 2, 0))));
  568. return 1;
  569. }
  570. /* }====================================================== */
  571. static int io_setloc (lua_State *L) {
  572. static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
  573. LC_NUMERIC, LC_TIME};
  574. static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
  575. "numeric", "time", NULL};
  576. const char *l = lua_tostring(L, 1);
  577. int op = luaL_findstring(luaL_optstring(L, 2, "all"), catnames);
  578. luaL_argcheck(L, l || lua_isnoneornil(L, 1), 1, "string expected");
  579. luaL_argcheck(L, op != -1, 2, "invalid option");
  580. lua_pushstring(L, setlocale(cat[op], l));
  581. return 1;
  582. }
  583. static int io_exit (lua_State *L) {
  584. exit(luaL_optint(L, 1, EXIT_SUCCESS));
  585. return 0; /* to avoid warnings */
  586. }
  587. static const luaL_reg syslib[] = {
  588. {"clock", io_clock},
  589. {"date", io_date},
  590. {"difftime", io_difftime},
  591. {"execute", io_execute},
  592. {"exit", io_exit},
  593. {"getenv", io_getenv},
  594. {"remove", io_remove},
  595. {"rename", io_rename},
  596. {"setlocale", io_setloc},
  597. {"time", io_time},
  598. {"tmpname", io_tmpname},
  599. {NULL, NULL}
  600. };
  601. /* }====================================================== */
  602. LUALIB_API int luaopen_io (lua_State *L) {
  603. luaL_openlib(L, LUA_OSLIBNAME, syslib, 0);
  604. createmeta(L);
  605. lua_pushvalue(L, -1);
  606. luaL_openlib(L, LUA_IOLIBNAME, iolib, 1);
  607. /* put predefined file handles into `io' table */
  608. registerfile(L, stdin, "stdin", IO_INPUT);
  609. registerfile(L, stdout, "stdout", IO_OUTPUT);
  610. registerfile(L, stderr, "stderr", NULL);
  611. return 1;
  612. }