commit 90d7892007e399765cf8d14f65f7e3bc4a96e087
parent 041cb9699284399a5068c91ced2df3f298276c0b
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Wed, 4 Dec 2002 13:26:55 -0200
new function `io.type'
Diffstat:
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/liolib.c b/liolib.c
@@ -1,5 +1,5 @@
/*
-** $Id: liolib.c,v 2.25 2002/11/25 15:05:39 roberto Exp roberto $
+** $Id: liolib.c,v 2.26 2002/12/04 15:17:36 roberto Exp roberto $
** Standard I/O (and system) library
** See Copyright Notice in lua.h
*/
@@ -65,6 +65,19 @@ static FILE **topfile (lua_State *L, int findex) {
}
+static int io_type (lua_State *L) {
+ FILE **f = (FILE **)lua_touserdata(L, 1);
+ if (f == NULL || !lua_getmetatable(L, 1) ||
+ !lua_rawequal(L, -1, lua_upvalueindex(1)))
+ lua_pushnil(L);
+ else if (*f == NULL)
+ lua_pushliteral(L, "closed file");
+ else
+ lua_pushliteral(L, "file");
+ return 1;
+}
+
+
static FILE *tofile (lua_State *L, int findex) {
FILE **f = topfile(L, findex);
if (*f == NULL)
@@ -464,6 +477,7 @@ static const luaL_reg iolib[] = {
{"popen", io_popen},
{"read", io_read},
{"tmpfile", io_tmpfile},
+ {"type", io_type},
{"write", io_write},
{NULL, NULL}
};