liolib.c 18 KB

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