From 90d7892007e399765cf8d14f65f7e3bc4a96e087 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 4 Dec 2002 13:27:17 -0200 Subject: new function `io.type' --- liolib.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/liolib.c b/liolib.c index 4bf2d5bc..2d881875 100644 --- 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} }; -- cgit v1.2.3-55-g6feb