liolib.c 18 KB

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