]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/lua-5.3.1/src/liolib.c
bump version to 12.2.12-pve1
[ceph.git] / ceph / src / civetweb / src / third_party / lua-5.3.1 / src / liolib.c
1 /*
2 ** $Id: liolib.c,v 2.144 2015/04/03 18:41:57 roberto Exp $
3 ** Standard I/O (and system) library
4 ** See Copyright Notice in lua.h
5 */
6
7 #define liolib_c
8 #define LUA_LIB
9
10 #include "lprefix.h"
11
12
13 #include <ctype.h>
14 #include <errno.h>
15 #include <locale.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include "lua.h"
21
22 #include "lauxlib.h"
23 #include "lualib.h"
24
25
26 #if !defined(l_checkmode)
27
28 /*
29 ** Check whether 'mode' matches '[rwa]%+?b?'.
30 ** Change this macro to accept other modes for 'fopen' besides
31 ** the standard ones.
32 */
33 #define l_checkmode(mode) \
34 (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \
35 (*mode != '+' || ++mode) && /* skip if char is '+' */ \
36 (*mode != 'b' || ++mode) && /* skip if char is 'b' */ \
37 (*mode == '\0'))
38
39 #endif
40
41 /*
42 ** {======================================================
43 ** l_popen spawns a new process connected to the current
44 ** one through the file streams.
45 ** =======================================================
46 */
47
48 #if !defined(l_popen) /* { */
49
50 #if defined(LUA_USE_POSIX) /* { */
51
52 #define l_popen(L,c,m) (fflush(NULL), popen(c,m))
53 #define l_pclose(L,file) (pclose(file))
54
55 #elif defined(LUA_USE_WINDOWS) /* }{ */
56
57 #define l_popen(L,c,m) (_popen(c,m))
58 #define l_pclose(L,file) (_pclose(file))
59
60 #else /* }{ */
61
62 /* ISO C definitions */
63 #define l_popen(L,c,m) \
64 ((void)((void)c, m), \
65 luaL_error(L, "'popen' not supported"), \
66 (FILE*)0)
67 #define l_pclose(L,file) ((void)L, (void)file, -1)
68
69 #endif /* } */
70
71 #endif /* } */
72
73 /* }====================================================== */
74
75
76 #if !defined(l_getc) /* { */
77
78 #if defined(LUA_USE_POSIX)
79 #define l_getc(f) getc_unlocked(f)
80 #define l_lockfile(f) flockfile(f)
81 #define l_unlockfile(f) funlockfile(f)
82 #else
83 #define l_getc(f) getc(f)
84 #define l_lockfile(f) ((void)0)
85 #define l_unlockfile(f) ((void)0)
86 #endif
87
88 #endif /* } */
89
90
91 /*
92 ** {======================================================
93 ** l_fseek: configuration for longer offsets
94 ** =======================================================
95 */
96
97 #if !defined(l_fseek) /* { */
98
99 #if defined(LUA_USE_POSIX) /* { */
100
101 #include <sys/types.h>
102
103 #define l_fseek(f,o,w) fseeko(f,o,w)
104 #define l_ftell(f) ftello(f)
105 #define l_seeknum off_t
106
107 #elif defined(LUA_USE_WINDOWS) && !defined(_CRTIMP_TYPEINFO) \
108 && defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */
109
110 /* Windows (but not DDK) and Visual C++ 2005 or higher */
111 #define l_fseek(f,o,w) _fseeki64(f,o,w)
112 #define l_ftell(f) _ftelli64(f)
113 #define l_seeknum __int64
114
115 #else /* }{ */
116
117 /* ISO C definitions */
118 #define l_fseek(f,o,w) fseek(f,o,w)
119 #define l_ftell(f) ftell(f)
120 #define l_seeknum long
121
122 #endif /* } */
123
124 #endif /* } */
125
126 /* }====================================================== */
127
128
129 #define IO_PREFIX "_IO_"
130 #define IOPREF_LEN (sizeof(IO_PREFIX)/sizeof(char) - 1)
131 #define IO_INPUT (IO_PREFIX "input")
132 #define IO_OUTPUT (IO_PREFIX "output")
133
134
135 typedef luaL_Stream LStream;
136
137
138 #define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE))
139
140 #define isclosed(p) ((p)->closef == NULL)
141
142
143 static int io_type (lua_State *L) {
144 LStream *p;
145 luaL_checkany(L, 1);
146 p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE);
147 if (p == NULL)
148 lua_pushnil(L); /* not a file */
149 else if (isclosed(p))
150 lua_pushliteral(L, "closed file");
151 else
152 lua_pushliteral(L, "file");
153 return 1;
154 }
155
156
157 static int f_tostring (lua_State *L) {
158 LStream *p = tolstream(L);
159 if (isclosed(p))
160 lua_pushliteral(L, "file (closed)");
161 else
162 lua_pushfstring(L, "file (%p)", p->f);
163 return 1;
164 }
165
166
167 static FILE *tofile (lua_State *L) {
168 LStream *p = tolstream(L);
169 if (isclosed(p))
170 luaL_error(L, "attempt to use a closed file");
171 lua_assert(p->f);
172 return p->f;
173 }
174
175
176 /*
177 ** When creating file handles, always creates a 'closed' file handle
178 ** before opening the actual file; so, if there is a memory error, the
179 ** file is not left opened.
180 */
181 static LStream *newprefile (lua_State *L) {
182 LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream));
183 p->closef = NULL; /* mark file handle as 'closed' */
184 luaL_setmetatable(L, LUA_FILEHANDLE);
185 return p;
186 }
187
188
189 /*
190 ** Calls the 'close' function from a file handle. The 'volatile' avoids
191 ** a bug in some versions of the Clang compiler (e.g., clang 3.0 for
192 ** 32 bits).
193 */
194 static int aux_close (lua_State *L) {
195 LStream *p = tolstream(L);
196 volatile lua_CFunction cf = p->closef;
197 p->closef = NULL; /* mark stream as closed */
198 return (*cf)(L); /* close it */
199 }
200
201
202 static int io_close (lua_State *L) {
203 if (lua_isnone(L, 1)) /* no argument? */
204 lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */
205 tofile(L); /* make sure argument is an open stream */
206 return aux_close(L);
207 }
208
209
210 static int f_gc (lua_State *L) {
211 LStream *p = tolstream(L);
212 if (!isclosed(p) && p->f != NULL)
213 aux_close(L); /* ignore closed and incompletely open files */
214 return 0;
215 }
216
217
218 /*
219 ** function to close regular files
220 */
221 static int io_fclose (lua_State *L) {
222 LStream *p = tolstream(L);
223 int res = fclose(p->f);
224 return luaL_fileresult(L, (res == 0), NULL);
225 }
226
227
228 static LStream *newfile (lua_State *L) {
229 LStream *p = newprefile(L);
230 p->f = NULL;
231 p->closef = &io_fclose;
232 return p;
233 }
234
235
236 static void opencheck (lua_State *L, const char *fname, const char *mode) {
237 LStream *p = newfile(L);
238 p->f = fopen(fname, mode);
239 if (p->f == NULL)
240 luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno));
241 }
242
243
244 static int io_open (lua_State *L) {
245 const char *filename = luaL_checkstring(L, 1);
246 const char *mode = luaL_optstring(L, 2, "r");
247 LStream *p = newfile(L);
248 const char *md = mode; /* to traverse/check mode */
249 luaL_argcheck(L, l_checkmode(md), 2, "invalid mode");
250 p->f = fopen(filename, mode);
251 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
252 }
253
254
255 /*
256 ** function to close 'popen' files
257 */
258 static int io_pclose (lua_State *L) {
259 LStream *p = tolstream(L);
260 return luaL_execresult(L, l_pclose(L, p->f));
261 }
262
263
264 static int io_popen (lua_State *L) {
265 const char *filename = luaL_checkstring(L, 1);
266 const char *mode = luaL_optstring(L, 2, "r");
267 LStream *p = newprefile(L);
268 p->f = l_popen(L, filename, mode);
269 p->closef = &io_pclose;
270 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
271 }
272
273
274 static int io_tmpfile (lua_State *L) {
275 LStream *p = newfile(L);
276 p->f = tmpfile();
277 return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
278 }
279
280
281 static FILE *getiofile (lua_State *L, const char *findex) {
282 LStream *p;
283 lua_getfield(L, LUA_REGISTRYINDEX, findex);
284 p = (LStream *)lua_touserdata(L, -1);
285 if (isclosed(p))
286 luaL_error(L, "standard %s file is closed", findex + IOPREF_LEN);
287 return p->f;
288 }
289
290
291 static int g_iofile (lua_State *L, const char *f, const char *mode) {
292 if (!lua_isnoneornil(L, 1)) {
293 const char *filename = lua_tostring(L, 1);
294 if (filename)
295 opencheck(L, filename, mode);
296 else {
297 tofile(L); /* check that it's a valid file handle */
298 lua_pushvalue(L, 1);
299 }
300 lua_setfield(L, LUA_REGISTRYINDEX, f);
301 }
302 /* return current value */
303 lua_getfield(L, LUA_REGISTRYINDEX, f);
304 return 1;
305 }
306
307
308 static int io_input (lua_State *L) {
309 return g_iofile(L, IO_INPUT, "r");
310 }
311
312
313 static int io_output (lua_State *L) {
314 return g_iofile(L, IO_OUTPUT, "w");
315 }
316
317
318 static int io_readline (lua_State *L);
319
320
321 static void aux_lines (lua_State *L, int toclose) {
322 int n = lua_gettop(L) - 1; /* number of arguments to read */
323 lua_pushinteger(L, n); /* number of arguments to read */
324 lua_pushboolean(L, toclose); /* close/not close file when finished */
325 lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */
326 lua_pushcclosure(L, io_readline, 3 + n);
327 }
328
329
330 static int f_lines (lua_State *L) {
331 tofile(L); /* check that it's a valid file handle */
332 aux_lines(L, 0);
333 return 1;
334 }
335
336
337 static int io_lines (lua_State *L) {
338 int toclose;
339 if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */
340 if (lua_isnil(L, 1)) { /* no file name? */
341 lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */
342 lua_replace(L, 1); /* put it at index 1 */
343 tofile(L); /* check that it's a valid file handle */
344 toclose = 0; /* do not close it after iteration */
345 }
346 else { /* open a new file */
347 const char *filename = luaL_checkstring(L, 1);
348 opencheck(L, filename, "r");
349 lua_replace(L, 1); /* put file at index 1 */
350 toclose = 1; /* close it after iteration */
351 }
352 aux_lines(L, toclose);
353 return 1;
354 }
355
356
357 /*
358 ** {======================================================
359 ** READ
360 ** =======================================================
361 */
362
363
364 /* maximum length of a numeral */
365 #define MAXRN 200
366
367 /* auxiliary structure used by 'read_number' */
368 typedef struct {
369 FILE *f; /* file being read */
370 int c; /* current character (look ahead) */
371 int n; /* number of elements in buffer 'buff' */
372 char buff[MAXRN + 1]; /* +1 for ending '\0' */
373 } RN;
374
375
376 /*
377 ** Add current char to buffer (if not out of space) and read next one
378 */
379 static int nextc (RN *rn) {
380 if (rn->n >= MAXRN) { /* buffer overflow? */
381 rn->buff[0] = '\0'; /* invalidate result */
382 return 0; /* fail */
383 }
384 else {
385 rn->buff[rn->n++] = rn->c; /* save current char */
386 rn->c = l_getc(rn->f); /* read next one */
387 return 1;
388 }
389 }
390
391
392 /*
393 ** Accept current char if it is in 'set' (of size 1 or 2)
394 */
395 static int test2 (RN *rn, const char *set) {
396 if (rn->c == set[0] || (rn->c == set[1] && rn->c != '\0'))
397 return nextc(rn);
398 else return 0;
399 }
400
401
402 /*
403 ** Read a sequence of (hex)digits
404 */
405 static int readdigits (RN *rn, int hex) {
406 int count = 0;
407 while ((hex ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn))
408 count++;
409 return count;
410 }
411
412
413 /*
414 ** Read a number: first reads a valid prefix of a numeral into a buffer.
415 ** Then it calls 'lua_stringtonumber' to check whether the format is
416 ** correct and to convert it to a Lua number
417 */
418 static int read_number (lua_State *L, FILE *f) {
419 RN rn;
420 int count = 0;
421 int hex = 0;
422 char decp[2];
423 rn.f = f; rn.n = 0;
424 decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */
425 decp[1] = '\0';
426 l_lockfile(rn.f);
427 do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
428 test2(&rn, "-+"); /* optional signal */
429 if (test2(&rn, "0")) {
430 if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */
431 else count = 1; /* count initial '0' as a valid digit */
432 }
433 count += readdigits(&rn, hex); /* integral part */
434 if (test2(&rn, decp)) /* decimal point? */
435 count += readdigits(&rn, hex); /* fractional part */
436 if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */
437 test2(&rn, "-+"); /* exponent signal */
438 readdigits(&rn, 0); /* exponent digits */
439 }
440 ungetc(rn.c, rn.f); /* unread look-ahead char */
441 l_unlockfile(rn.f);
442 rn.buff[rn.n] = '\0'; /* finish string */
443 if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */
444 return 1; /* ok */
445 else { /* invalid format */
446 lua_pushnil(L); /* "result" to be removed */
447 return 0; /* read fails */
448 }
449 }
450
451
452 static int test_eof (lua_State *L, FILE *f) {
453 int c = getc(f);
454 ungetc(c, f); /* no-op when c == EOF */
455 lua_pushliteral(L, "");
456 return (c != EOF);
457 }
458
459
460 static int read_line (lua_State *L, FILE *f, int chop) {
461 luaL_Buffer b;
462 int c = '\0';
463 luaL_buffinit(L, &b);
464 while (c != EOF && c != '\n') { /* repeat until end of line */
465 char *buff = luaL_prepbuffer(&b); /* pre-allocate buffer */
466 int i = 0;
467 l_lockfile(f); /* no memory errors can happen inside the lock */
468 while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n')
469 buff[i++] = c;
470 l_unlockfile(f);
471 luaL_addsize(&b, i);
472 }
473 if (!chop && c == '\n') /* want a newline and have one? */
474 luaL_addchar(&b, c); /* add ending newline to result */
475 luaL_pushresult(&b); /* close buffer */
476 /* return ok if read something (either a newline or something else) */
477 return (c == '\n' || lua_rawlen(L, -1) > 0);
478 }
479
480
481 static void read_all (lua_State *L, FILE *f) {
482 size_t nr;
483 luaL_Buffer b;
484 luaL_buffinit(L, &b);
485 do { /* read file in chunks of LUAL_BUFFERSIZE bytes */
486 char *p = luaL_prepbuffsize(&b, LUAL_BUFFERSIZE);
487 nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f);
488 luaL_addsize(&b, nr);
489 } while (nr == LUAL_BUFFERSIZE);
490 luaL_pushresult(&b); /* close buffer */
491 }
492
493
494 static int read_chars (lua_State *L, FILE *f, size_t n) {
495 size_t nr; /* number of chars actually read */
496 char *p;
497 luaL_Buffer b;
498 luaL_buffinit(L, &b);
499 p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */
500 nr = fread(p, sizeof(char), n, f); /* try to read 'n' chars */
501 luaL_addsize(&b, nr);
502 luaL_pushresult(&b); /* close buffer */
503 return (nr > 0); /* true iff read something */
504 }
505
506
507 static int g_read (lua_State *L, FILE *f, int first) {
508 int nargs = lua_gettop(L) - 1;
509 int success;
510 int n;
511 clearerr(f);
512 if (nargs == 0) { /* no arguments? */
513 success = read_line(L, f, 1);
514 n = first+1; /* to return 1 result */
515 }
516 else { /* ensure stack space for all results and for auxlib's buffer */
517 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
518 success = 1;
519 for (n = first; nargs-- && success; n++) {
520 if (lua_type(L, n) == LUA_TNUMBER) {
521 size_t l = (size_t)luaL_checkinteger(L, n);
522 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
523 }
524 else {
525 const char *p = luaL_checkstring(L, n);
526 if (*p == '*') p++; /* skip optional '*' (for compatibility) */
527 switch (*p) {
528 case 'n': /* number */
529 success = read_number(L, f);
530 break;
531 case 'l': /* line */
532 success = read_line(L, f, 1);
533 break;
534 case 'L': /* line with end-of-line */
535 success = read_line(L, f, 0);
536 break;
537 case 'a': /* file */
538 read_all(L, f); /* read entire file */
539 success = 1; /* always success */
540 break;
541 default:
542 return luaL_argerror(L, n, "invalid format");
543 }
544 }
545 }
546 }
547 if (ferror(f))
548 return luaL_fileresult(L, 0, NULL);
549 if (!success) {
550 lua_pop(L, 1); /* remove last result */
551 lua_pushnil(L); /* push nil instead */
552 }
553 return n - first;
554 }
555
556
557 static int io_read (lua_State *L) {
558 return g_read(L, getiofile(L, IO_INPUT), 1);
559 }
560
561
562 static int f_read (lua_State *L) {
563 return g_read(L, tofile(L), 2);
564 }
565
566
567 static int io_readline (lua_State *L) {
568 LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1));
569 int i;
570 int n = (int)lua_tointeger(L, lua_upvalueindex(2));
571 if (isclosed(p)) /* file is already closed? */
572 return luaL_error(L, "file is already closed");
573 lua_settop(L , 1);
574 luaL_checkstack(L, n, "too many arguments");
575 for (i = 1; i <= n; i++) /* push arguments to 'g_read' */
576 lua_pushvalue(L, lua_upvalueindex(3 + i));
577 n = g_read(L, p->f, 2); /* 'n' is number of results */
578 lua_assert(n > 0); /* should return at least a nil */
579 if (lua_toboolean(L, -n)) /* read at least one value? */
580 return n; /* return them */
581 else { /* first result is nil: EOF or error */
582 if (n > 1) { /* is there error information? */
583 /* 2nd result is error message */
584 return luaL_error(L, "%s", lua_tostring(L, -n + 1));
585 }
586 if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */
587 lua_settop(L, 0);
588 lua_pushvalue(L, lua_upvalueindex(1));
589 aux_close(L); /* close it */
590 }
591 return 0;
592 }
593 }
594
595 /* }====================================================== */
596
597
598 static int g_write (lua_State *L, FILE *f, int arg) {
599 int nargs = lua_gettop(L) - arg;
600 int status = 1;
601 for (; nargs--; arg++) {
602 if (lua_type(L, arg) == LUA_TNUMBER) {
603 /* optimization: could be done exactly as for strings */
604 int len = lua_isinteger(L, arg)
605 ? fprintf(f, LUA_INTEGER_FMT, lua_tointeger(L, arg))
606 : fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg));
607 status = status && (len > 0);
608 }
609 else {
610 size_t l;
611 const char *s = luaL_checklstring(L, arg, &l);
612 status = status && (fwrite(s, sizeof(char), l, f) == l);
613 }
614 }
615 if (status) return 1; /* file handle already on stack top */
616 else return luaL_fileresult(L, status, NULL);
617 }
618
619
620 static int io_write (lua_State *L) {
621 return g_write(L, getiofile(L, IO_OUTPUT), 1);
622 }
623
624
625 static int f_write (lua_State *L) {
626 FILE *f = tofile(L);
627 lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */
628 return g_write(L, f, 2);
629 }
630
631
632 static int f_seek (lua_State *L) {
633 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
634 static const char *const modenames[] = {"set", "cur", "end", NULL};
635 FILE *f = tofile(L);
636 int op = luaL_checkoption(L, 2, "cur", modenames);
637 lua_Integer p3 = luaL_optinteger(L, 3, 0);
638 l_seeknum offset = (l_seeknum)p3;
639 luaL_argcheck(L, (lua_Integer)offset == p3, 3,
640 "not an integer in proper range");
641 op = l_fseek(f, offset, mode[op]);
642 if (op)
643 return luaL_fileresult(L, 0, NULL); /* error */
644 else {
645 lua_pushinteger(L, (lua_Integer)l_ftell(f));
646 return 1;
647 }
648 }
649
650
651 static int f_setvbuf (lua_State *L) {
652 static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
653 static const char *const modenames[] = {"no", "full", "line", NULL};
654 FILE *f = tofile(L);
655 int op = luaL_checkoption(L, 2, NULL, modenames);
656 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
657 int res = setvbuf(f, NULL, mode[op], (size_t)sz);
658 return luaL_fileresult(L, res == 0, NULL);
659 }
660
661
662
663 static int io_flush (lua_State *L) {
664 return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
665 }
666
667
668 static int f_flush (lua_State *L) {
669 return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
670 }
671
672
673 /*
674 ** functions for 'io' library
675 */
676 static const luaL_Reg iolib[] = {
677 {"close", io_close},
678 {"flush", io_flush},
679 {"input", io_input},
680 {"lines", io_lines},
681 {"open", io_open},
682 {"output", io_output},
683 {"popen", io_popen},
684 {"read", io_read},
685 {"tmpfile", io_tmpfile},
686 {"type", io_type},
687 {"write", io_write},
688 {NULL, NULL}
689 };
690
691
692 /*
693 ** methods for file handles
694 */
695 static const luaL_Reg flib[] = {
696 {"close", io_close},
697 {"flush", f_flush},
698 {"lines", f_lines},
699 {"read", f_read},
700 {"seek", f_seek},
701 {"setvbuf", f_setvbuf},
702 {"write", f_write},
703 {"__gc", f_gc},
704 {"__tostring", f_tostring},
705 {NULL, NULL}
706 };
707
708
709 static void createmeta (lua_State *L) {
710 luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
711 lua_pushvalue(L, -1); /* push metatable */
712 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
713 luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */
714 lua_pop(L, 1); /* pop new metatable */
715 }
716
717
718 /*
719 ** function to (not) close the standard files stdin, stdout, and stderr
720 */
721 static int io_noclose (lua_State *L) {
722 LStream *p = tolstream(L);
723 p->closef = &io_noclose; /* keep file opened */
724 lua_pushnil(L);
725 lua_pushliteral(L, "cannot close standard file");
726 return 2;
727 }
728
729
730 static void createstdfile (lua_State *L, FILE *f, const char *k,
731 const char *fname) {
732 LStream *p = newprefile(L);
733 p->f = f;
734 p->closef = &io_noclose;
735 if (k != NULL) {
736 lua_pushvalue(L, -1);
737 lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */
738 }
739 lua_setfield(L, -2, fname); /* add file to module */
740 }
741
742
743 LUAMOD_API int luaopen_io (lua_State *L) {
744 luaL_newlib(L, iolib); /* new module */
745 createmeta(L);
746 /* create (and set) default files */
747 createstdfile(L, stdin, IO_INPUT, "stdin");
748 createstdfile(L, stdout, IO_OUTPUT, "stdout");
749 createstdfile(L, stderr, NULL, "stderr");
750 return 1;
751 }
752