Browse Source

new function `io.type'

Roberto Ierusalimschy 22 years ago
parent
commit
90d7892007
1 changed files with 15 additions and 1 deletions
  1. 15 1
      liolib.c

+ 15 - 1
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}
 };