liolib.c 18 KB

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