liolib.c 18 KB

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