liolib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. ** $Id: liolib.c,v 1.102 2001/01/26 12:12:16 roberto Exp roberto $
  3. ** Standard I/O (and system) library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <time.h>
  11. #include "lua.h"
  12. #include "lauxlib.h"
  13. #include "luadebug.h"
  14. #include "lualib.h"
  15. #ifndef OLD_ANSI
  16. #include <errno.h>
  17. #include <locale.h>
  18. #define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
  19. #define free(b) if (b) (free)(b)
  20. #else
  21. /* no support for locale and for strerror: fake them */
  22. #define setlocale(a,b) ((void)a, strcmp((b),"C")==0?"C":NULL)
  23. #define LC_ALL 0
  24. #define LC_COLLATE 0
  25. #define LC_CTYPE 0
  26. #define LC_MONETARY 0
  27. #define LC_NUMERIC 0
  28. #define LC_TIME 0
  29. #define strerror(e) "I/O error"
  30. #define errno (-1)
  31. #endif
  32. #ifdef POPEN
  33. /* FILE *popen();
  34. int pclose(); */
  35. #define CLOSEFILE(L, f) ((pclose(f) == -1) ? fclose(f) : 0)
  36. #else
  37. /* no support for popen */
  38. #define popen(x,y) NULL /* that is, popen always fails */
  39. #define CLOSEFILE(L, f) (fclose(f))
  40. #endif
  41. #define INFILE 0
  42. #define OUTFILE 1
  43. #define NOFILE 2
  44. #define FILEHANDLE "FileHandle"
  45. static const char *const filenames[] = {"_INPUT", "_OUTPUT"};
  46. static int pushresult (lua_State *L, int i) {
  47. if (i) {
  48. lua_pushuserdata(L, NULL);
  49. return 1;
  50. }
  51. else {
  52. lua_pushnil(L);
  53. lua_pushstring(L, strerror(errno));
  54. lua_pushnumber(L, errno);
  55. return 3;
  56. }
  57. }
  58. /*
  59. ** {======================================================
  60. ** FILE Operations
  61. ** =======================================================
  62. */
  63. #define checkfile(L,f) (strcmp(lua_xtype(L,(f)), FILEHANDLE) == 0)
  64. static FILE *getopthandle (lua_State *L, int inout) {
  65. FILE *p = (FILE *)lua_touserdata(L, 1);
  66. if (p != NULL) { /* is it a userdata ? */
  67. if (!checkfile(L, 1)) {
  68. if (strcmp(lua_xtype(L, 1), "ClosedFileHandle") == 0)
  69. luaL_argerror(L, 1, "file is closed");
  70. else
  71. luaL_argerror(L, 1, "(invalid value)");
  72. }
  73. /* move it to stack top */
  74. lua_pushvalue(L, 1); lua_remove(L, 1);
  75. }
  76. else if (inout != NOFILE) { /* try global value */
  77. lua_getglobal(L, filenames[inout]);
  78. if (!checkfile(L,-1))
  79. luaL_verror(L, "global variable `%.10s' is not a valid file handle",
  80. filenames[inout]);
  81. p = (FILE *)lua_touserdata(L, -1);
  82. }
  83. return p; /* leave handle at stack top to avoid GC */
  84. }
  85. static void pushfile (lua_State *L, FILE *f) {
  86. lua_pushusertag(L, f, lua_type2tag(L, FILEHANDLE));
  87. }
  88. static void setfilebyname (lua_State *L, FILE *f, const char *name) {
  89. pushfile(L, f);
  90. lua_setglobal(L, name);
  91. }
  92. #define setfile(L,f,inout) (setfilebyname(L,f,filenames[inout]))
  93. static int setreturn (lua_State *L, FILE *f, int inout) {
  94. if (f == NULL)
  95. return pushresult(L, 0);
  96. else {
  97. if (inout != NOFILE)
  98. setfile(L, f, inout);
  99. pushfile(L, f);
  100. return 1;
  101. }
  102. }
  103. static int closefile (lua_State *L, FILE *f) {
  104. if (f == stdin || f == stdout || f == stderr)
  105. return 1;
  106. else {
  107. pushfile(L, f);
  108. lua_settag(L, lua_type2tag(L, "ClosedFileHandle"));
  109. return (CLOSEFILE(L, f) == 0);
  110. }
  111. }
  112. static int io_close (lua_State *L) {
  113. FILE *f = (FILE *)luaL_check_userdata(L, 1, FILEHANDLE);
  114. return pushresult(L, closefile(L, f));
  115. }
  116. static int file_collect (lua_State *L) {
  117. FILE *f = (FILE *)luaL_check_userdata(L, 1, FILEHANDLE);
  118. if (f != stdin && f != stdout && f != stderr)
  119. CLOSEFILE(L, f);
  120. return 0;
  121. }
  122. static int io_open (lua_State *L) {
  123. FILE *f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2));
  124. return setreturn(L, f, NOFILE);
  125. }
  126. static int io_tmpfile (lua_State *L) {
  127. return setreturn(L, tmpfile(), NOFILE);
  128. }
  129. static int io_fromto (lua_State *L, int inout, const char *mode) {
  130. FILE *current;
  131. if (lua_isnull(L, 1)) {
  132. closefile(L, getopthandle(L, inout));
  133. current = (inout == 0) ? stdin : stdout;
  134. }
  135. else if (checkfile(L, 1)) /* deprecated option */
  136. current = (FILE *)lua_touserdata(L, 1);
  137. else {
  138. const char *s = luaL_check_string(L, 1);
  139. current = (*s == '|') ? popen(s+1, mode) : fopen(s, mode);
  140. }
  141. return setreturn(L, current, inout);
  142. }
  143. static int io_readfrom (lua_State *L) {
  144. return io_fromto(L, INFILE, "r");
  145. }
  146. static int io_writeto (lua_State *L) {
  147. return io_fromto(L, OUTFILE, "w");
  148. }
  149. static int io_appendto (lua_State *L) {
  150. FILE *current = fopen(luaL_check_string(L, 1), "a");
  151. return setreturn(L, current, OUTFILE);
  152. }
  153. /*
  154. ** {======================================================
  155. ** READ
  156. ** =======================================================
  157. */
  158. static int read_number (lua_State *L, FILE *f) {
  159. double d;
  160. if (fscanf(f, "%lf", &d) == 1) {
  161. lua_pushnumber(L, d);
  162. return 1;
  163. }
  164. else return 0; /* read fails */
  165. }
  166. static int read_word (lua_State *L, FILE *f) {
  167. int c;
  168. luaL_Buffer b;
  169. luaL_buffinit(L, &b);
  170. do { c = fgetc(f); } while (isspace(c)); /* skip spaces */
  171. while (c != EOF && !isspace(c)) {
  172. luaL_putchar(&b, c);
  173. c = fgetc(f);
  174. }
  175. ungetc(c, f);
  176. luaL_pushresult(&b); /* close buffer */
  177. return (lua_strlen(L, -1) > 0);
  178. }
  179. static int read_line (lua_State *L, FILE *f) {
  180. int n = 0;
  181. luaL_Buffer b;
  182. luaL_buffinit(L, &b);
  183. for (;;) {
  184. char *p = luaL_prepbuffer(&b);
  185. if (!fgets(p, LUAL_BUFFERSIZE, f)) /* read fails? */
  186. break;
  187. n = strlen(p);
  188. if (p[n-1] != '\n')
  189. luaL_addsize(&b, n);
  190. else {
  191. luaL_addsize(&b, n-1); /* do not add the `\n' */
  192. break;
  193. }
  194. }
  195. luaL_pushresult(&b); /* close buffer */
  196. return (n > 0); /* read something? */
  197. }
  198. static void read_file (lua_State *L, FILE *f) {
  199. size_t len = 0;
  200. size_t size = LUAL_BUFFERSIZE;
  201. char *buffer = NULL;
  202. for (;;) {
  203. char *newbuffer = (char *)realloc(buffer, size);
  204. if (newbuffer == NULL) {
  205. free(buffer);
  206. lua_error(L, "not enough memory to read a file");
  207. }
  208. buffer = newbuffer;
  209. len += fread(buffer+len, sizeof(char), size-len, f);
  210. if (len < size) break; /* did not read all it could */
  211. size *= 2;
  212. }
  213. lua_pushlstring(L, buffer, len);
  214. free(buffer);
  215. }
  216. static int read_chars (lua_State *L, FILE *f, size_t n) {
  217. char *buffer;
  218. size_t n1;
  219. char statbuff[LUAL_BUFFERSIZE];
  220. if (n <= LUAL_BUFFERSIZE)
  221. buffer = statbuff;
  222. else {
  223. buffer = (char *)malloc(n);
  224. if (buffer == NULL)
  225. lua_error(L, "not enough memory to read a file");
  226. }
  227. n1 = fread(buffer, sizeof(char), n, f);
  228. lua_pushlstring(L, buffer, n1);
  229. if (buffer != statbuff) free(buffer);
  230. return (n1 > 0 || n == 0);
  231. }
  232. static int io_read (lua_State *L) {
  233. FILE *f = getopthandle(L, INFILE);
  234. int nargs = lua_gettop(L)-1;
  235. int success;
  236. int n;
  237. if (nargs == 0) { /* no arguments? */
  238. success = read_line(L, f);
  239. n = 2; /* will return n-1 results */
  240. }
  241. else { /* ensure stack space for all results and for auxlib's buffer */
  242. luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
  243. success = 1;
  244. for (n = 1; n<=nargs && success; n++) {
  245. if (lua_type(L, n) == LUA_TNUMBER)
  246. success = read_chars(L, f, (size_t)lua_tonumber(L, n));
  247. else {
  248. const char *p = lua_tostring(L, n);
  249. if (!p || p[0] != '*')
  250. lua_error(L, "invalid `read' option");
  251. switch (p[1]) {
  252. case 'n': /* number */
  253. success = read_number(L, f);
  254. break;
  255. case 'l': /* line */
  256. success = read_line(L, f);
  257. break;
  258. case 'a': /* file */
  259. read_file(L, f);
  260. success = 1; /* always success */
  261. break;
  262. case 'w': /* word */
  263. success = read_word(L, f);
  264. break;
  265. default:
  266. luaL_argerror(L, n, "invalid format");
  267. success = 0; /* to avoid warnings */
  268. }
  269. }
  270. }
  271. }
  272. if (!success) {
  273. lua_pop(L, 1); /* remove last result */
  274. lua_pushnil(L); /* push nil instead */
  275. }
  276. return n - 1;
  277. }
  278. /* }====================================================== */
  279. static int io_write (lua_State *L) {
  280. FILE *f = getopthandle(L, OUTFILE);
  281. int nargs = lua_gettop(L)-1;
  282. int arg;
  283. int status = 1;
  284. for (arg=1; arg<=nargs; arg++) {
  285. if (lua_type(L, arg) == LUA_TNUMBER) { /* LUA_NUMBER */
  286. /* optimization: could be done exactly as for strings */
  287. status = status && fprintf(f, "%.16g", lua_tonumber(L, arg)) > 0;
  288. }
  289. else {
  290. size_t l;
  291. const char *s = luaL_check_lstr(L, arg, &l);
  292. status = status && (fwrite(s, sizeof(char), l, f) == l);
  293. }
  294. }
  295. pushresult(L, status);
  296. return 1;
  297. }
  298. static int io_seek (lua_State *L) {
  299. static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
  300. static const char *const modenames[] = {"set", "cur", "end", NULL};
  301. FILE *f = (FILE *)luaL_check_userdata(L, 1, FILEHANDLE);
  302. int op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames);
  303. long offset = luaL_opt_long(L, 3, 0);
  304. luaL_arg_check(L, op != -1, 2, "invalid mode");
  305. op = fseek(f, offset, mode[op]);
  306. if (op)
  307. return pushresult(L, 0); /* error */
  308. else {
  309. lua_pushnumber(L, ftell(f));
  310. return 1;
  311. }
  312. }
  313. static int io_flush (lua_State *L) {
  314. FILE *f = getopthandle(L, NOFILE);
  315. luaL_arg_check(L, f || lua_isnull(L, 1), 1, "invalid file handle");
  316. return pushresult(L, fflush(f) == 0);
  317. }
  318. /* }====================================================== */
  319. /*
  320. ** {======================================================
  321. ** Other O.S. Operations
  322. ** =======================================================
  323. */
  324. static int io_execute (lua_State *L) {
  325. lua_pushnumber(L, system(luaL_check_string(L, 1)));
  326. return 1;
  327. }
  328. static int io_remove (lua_State *L) {
  329. return pushresult(L, remove(luaL_check_string(L, 1)) == 0);
  330. }
  331. static int io_rename (lua_State *L) {
  332. return pushresult(L, rename(luaL_check_string(L, 1),
  333. luaL_check_string(L, 2)) == 0);
  334. }
  335. static int io_tmpname (lua_State *L) {
  336. char buff[L_tmpnam];
  337. if (tmpnam(buff) != buff)
  338. lua_error(L, "unable to generate a unique filename");
  339. lua_pushstring(L, buff);
  340. return 1;
  341. }
  342. static int io_getenv (lua_State *L) {
  343. lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */
  344. return 1;
  345. }
  346. static int io_clock (lua_State *L) {
  347. lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
  348. return 1;
  349. }
  350. /*
  351. ** {======================================================
  352. ** Time/Date operations
  353. ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
  354. ** wday=%w+1, yday=%j, isdst=? }
  355. ** =======================================================
  356. */
  357. static void setfield (lua_State *L, const char *key, int value) {
  358. lua_pushstring(L, key);
  359. lua_pushnumber(L, value);
  360. lua_rawset(L, -3);
  361. }
  362. static int getfield (lua_State *L, const char *key, int d) {
  363. int res;
  364. lua_pushstring(L, key);
  365. lua_rawget(L, -2);
  366. if (lua_isnumber(L, -1))
  367. res = (int)lua_tonumber(L, -1);
  368. else {
  369. if (d == -2)
  370. luaL_verror(L, "field `%.20s' missing in date table", key);
  371. res = d;
  372. }
  373. lua_pop(L, 1);
  374. return res;
  375. }
  376. static int io_date (lua_State *L) {
  377. const char *s = luaL_opt_string(L, 1, "%c");
  378. time_t t = (time_t)luaL_opt_number(L, 2, -1);
  379. struct tm *stm;
  380. if (t == (time_t)-1) /* no time given? */
  381. t = time(NULL); /* use current time */
  382. if (*s == '!') { /* UTC? */
  383. stm = gmtime(&t);
  384. s++; /* skip `!' */
  385. }
  386. else
  387. stm = localtime(&t);
  388. if (stm == NULL) /* invalid date? */
  389. lua_pushnil(L);
  390. else if (strcmp(s, "*t") == 0) {
  391. lua_newtable(L);
  392. setfield(L, "sec", stm->tm_sec);
  393. setfield(L, "min", stm->tm_min);
  394. setfield(L, "hour", stm->tm_hour);
  395. setfield(L, "day", stm->tm_mday);
  396. setfield(L, "month", stm->tm_mon+1);
  397. setfield(L, "year", stm->tm_year+1900);
  398. setfield(L, "wday", stm->tm_wday+1);
  399. setfield(L, "yday", stm->tm_yday+1);
  400. setfield(L, "isdst", stm->tm_isdst);
  401. }
  402. else {
  403. char b[256];
  404. if (strftime(b, sizeof(b), s, stm))
  405. lua_pushstring(L, b);
  406. else
  407. lua_error(L, "invalid `date' format");
  408. }
  409. return 1;
  410. }
  411. static int io_time (lua_State *L) {
  412. if (lua_isnull(L, 1)) /* called without args? */
  413. lua_pushnumber(L, time(NULL)); /* return current time */
  414. else {
  415. time_t t;
  416. struct tm ts;
  417. luaL_checktype(L, 1, LUA_TTABLE);
  418. lua_settop(L, 1); /* make sure table is at the top */
  419. ts.tm_sec = getfield(L, "sec", 0);
  420. ts.tm_min = getfield(L, "min", 0);
  421. ts.tm_hour = getfield(L, "hour", 12);
  422. ts.tm_mday = getfield(L, "day", -2);
  423. ts.tm_mon = getfield(L, "month", -2)-1;
  424. ts.tm_year = getfield(L, "year", -2)-1900;
  425. ts.tm_isdst = getfield(L, "isdst", -1);
  426. t = mktime(&ts);
  427. if (t == (time_t)-1)
  428. lua_pushnil(L);
  429. else
  430. lua_pushnumber(L, t);
  431. }
  432. return 1;
  433. }
  434. static int io_difftime (lua_State *L) {
  435. lua_pushnumber(L, difftime((time_t)luaL_check_number(L, 1),
  436. (time_t)luaL_opt_number(L, 2, 0)));
  437. return 1;
  438. }
  439. /* }====================================================== */
  440. static int io_setloc (lua_State *L) {
  441. static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
  442. LC_NUMERIC, LC_TIME};
  443. static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
  444. "numeric", "time", NULL};
  445. int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames);
  446. luaL_arg_check(L, op != -1, 2, "invalid option");
  447. lua_pushstring(L, setlocale(cat[op], luaL_check_string(L, 1)));
  448. return 1;
  449. }
  450. static int io_exit (lua_State *L) {
  451. exit(luaL_opt_int(L, 1, EXIT_SUCCESS));
  452. return 0; /* to avoid warnings */
  453. }
  454. /* }====================================================== */
  455. static int io_debug (lua_State *L) {
  456. for (;;) {
  457. char buffer[250];
  458. fprintf(stderr, "lua_debug> ");
  459. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  460. strcmp(buffer, "cont\n") == 0)
  461. return 0;
  462. lua_dostring(L, buffer);
  463. lua_settop(L, 0); /* remove eventual returns */
  464. }
  465. }
  466. #define LEVELS1 12 /* size of the first part of the stack */
  467. #define LEVELS2 10 /* size of the second part of the stack */
  468. static int errorfb (lua_State *L) {
  469. int level = 1; /* skip level 0 (it's this function) */
  470. int firstpart = 1; /* still before eventual `...' */
  471. lua_Debug ar;
  472. luaL_Buffer b;
  473. luaL_buffinit(L, &b);
  474. luaL_addstring(&b, "error: ");
  475. luaL_addstring(&b, luaL_check_string(L, 1));
  476. luaL_addstring(&b, "\n");
  477. while (lua_getstack(L, level++, &ar)) {
  478. char buff[120]; /* enough to fit following `sprintf's */
  479. if (level == 2)
  480. luaL_addstring(&b, "stack traceback:\n");
  481. else if (level > LEVELS1 && firstpart) {
  482. /* no more than `LEVELS2' more levels? */
  483. if (!lua_getstack(L, level+LEVELS2, &ar))
  484. level--; /* keep going */
  485. else {
  486. luaL_addstring(&b, " ...\n"); /* too many levels */
  487. while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */
  488. level++;
  489. }
  490. firstpart = 0;
  491. continue;
  492. }
  493. sprintf(buff, "%4d: ", level-1);
  494. luaL_addstring(&b, buff);
  495. lua_getinfo(L, "Snl", &ar);
  496. switch (*ar.namewhat) {
  497. case 'g': case 'l': /* global, local */
  498. sprintf(buff, "function `%.50s'", ar.name);
  499. break;
  500. case 'f': /* field */
  501. sprintf(buff, "method `%.50s'", ar.name);
  502. break;
  503. case 't': /* tag method */
  504. sprintf(buff, "`%.50s' tag method", ar.name);
  505. break;
  506. default: {
  507. if (*ar.what == 'm') /* main? */
  508. sprintf(buff, "main of %.70s", ar.short_src);
  509. else if (*ar.what == 'C') /* C function? */
  510. sprintf(buff, "%.70s", ar.short_src);
  511. else
  512. sprintf(buff, "function <%d:%.70s>", ar.linedefined, ar.short_src);
  513. ar.source = NULL; /* do not print source again */
  514. }
  515. }
  516. luaL_addstring(&b, buff);
  517. if (ar.currentline > 0) {
  518. sprintf(buff, " at line %d", ar.currentline);
  519. luaL_addstring(&b, buff);
  520. }
  521. if (ar.source) {
  522. sprintf(buff, " [%.70s]", ar.short_src);
  523. luaL_addstring(&b, buff);
  524. }
  525. luaL_addstring(&b, "\n");
  526. }
  527. luaL_pushresult(&b);
  528. lua_getglobal(L, LUA_ALERT);
  529. if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */
  530. lua_pushvalue(L, -2); /* error message */
  531. lua_rawcall(L, 1, 0);
  532. }
  533. return 0;
  534. }
  535. static const luaL_reg iolib[] = {
  536. {"appendto", io_appendto},
  537. {"clock", io_clock},
  538. {"closefile", io_close},
  539. {"date", io_date},
  540. {"debug", io_debug},
  541. {"difftime", io_difftime},
  542. {"execute", io_execute},
  543. {"exit", io_exit},
  544. {"flush", io_flush},
  545. {"getenv", io_getenv},
  546. {"openfile", io_open},
  547. {"read", io_read},
  548. {"readfrom", io_readfrom},
  549. {"remove", io_remove},
  550. {"rename", io_rename},
  551. {"seek", io_seek},
  552. {"setlocale", io_setloc},
  553. {"time", io_time},
  554. {"tmpfile", io_tmpfile},
  555. {"tmpname", io_tmpname},
  556. {"write", io_write},
  557. {"writeto", io_writeto},
  558. {LUA_ERRORMESSAGE, errorfb}
  559. };
  560. LUALIB_API void lua_iolibopen (lua_State *L) {
  561. int iotag = lua_newtype(L, FILEHANDLE, LUA_TUSERDATA);
  562. lua_newtype(L, "ClosedFileHandle", LUA_TUSERDATA);
  563. luaL_openl(L, iolib);
  564. /* predefined file handles */
  565. setfile(L, stdin, INFILE);
  566. setfile(L, stdout, OUTFILE);
  567. setfilebyname(L, stdin, "_STDIN");
  568. setfilebyname(L, stdout, "_STDOUT");
  569. setfilebyname(L, stderr, "_STDERR");
  570. /* close files when collected */
  571. lua_pushcfunction(L, file_collect);
  572. lua_settagmethod(L, iotag, "gc");
  573. }