liolib.c 18 KB

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