liolib.c 18 KB

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