aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2007-12-08 09:54:32 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2007-12-08 09:54:32 -0200
commitdae850e0eecb3e3a4d3c08201876ae475936e285 (patch)
treed919128849e9841ed841769926015d2bd45ca0d2 /liolib.c
parentabcc124df05fe19470abdb9d665160a7e3b01495 (diff)
downloadlua-dae850e0eecb3e3a4d3c08201876ae475936e285.tar.gz
lua-dae850e0eecb3e3a4d3c08201876ae475936e285.tar.bz2
lua-dae850e0eecb3e3a4d3c08201876ae475936e285.zip
details
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c19
1 files changed, 9 insertions, 10 deletions
diff --git a/liolib.c b/liolib.c
index 6ad58f31..3ecff0bc 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 2.75 2006/09/18 14:03:18 roberto Exp roberto $ 2** $Id: liolib.c,v 2.76 2007/04/19 20:22:32 roberto Exp roberto $
3** Standard I/O (and system) library 3** Standard I/O (and system) library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -51,15 +51,14 @@ static void fileerror (lua_State *L, int arg, const char *filename) {
51} 51}
52 52
53 53
54#define topfile(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE)) 54#define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
55 55
56 56
57static int io_type (lua_State *L) { 57static int io_type (lua_State *L) {
58 void *ud; 58 void *ud;
59 luaL_checkany(L, 1); 59 luaL_checkany(L, 1);
60 ud = lua_touserdata(L, 1); 60 ud = luaL_testudata(L, 1, LUA_FILEHANDLE);
61 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); 61 if (ud == NULL)
62 if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
63 lua_pushnil(L); /* not a file */ 62 lua_pushnil(L); /* not a file */
64 else if (*((FILE **)ud) == NULL) 63 else if (*((FILE **)ud) == NULL)
65 lua_pushliteral(L, "closed file"); 64 lua_pushliteral(L, "closed file");
@@ -70,7 +69,7 @@ static int io_type (lua_State *L) {
70 69
71 70
72static FILE *tofile (lua_State *L) { 71static FILE *tofile (lua_State *L) {
73 FILE **f = topfile(L); 72 FILE **f = tofilep(L);
74 if (*f == NULL) 73 if (*f == NULL)
75 luaL_error(L, "attempt to use a closed file"); 74 luaL_error(L, "attempt to use a closed file");
76 return *f; 75 return *f;
@@ -106,7 +105,7 @@ static int io_noclose (lua_State *L) {
106** function to close 'popen' files 105** function to close 'popen' files
107*/ 106*/
108static int io_pclose (lua_State *L) { 107static int io_pclose (lua_State *L) {
109 FILE **p = topfile(L); 108 FILE **p = tofilep(L);
110 int ok = lua_pclose(L, *p); 109 int ok = lua_pclose(L, *p);
111 *p = NULL; 110 *p = NULL;
112 return pushresult(L, ok, NULL); 111 return pushresult(L, ok, NULL);
@@ -117,7 +116,7 @@ static int io_pclose (lua_State *L) {
117** function to close regular files 116** function to close regular files
118*/ 117*/
119static int io_fclose (lua_State *L) { 118static int io_fclose (lua_State *L) {
120 FILE **p = topfile(L); 119 FILE **p = tofilep(L);
121 int ok = (fclose(*p) == 0); 120 int ok = (fclose(*p) == 0);
122 *p = NULL; 121 *p = NULL;
123 return pushresult(L, ok, NULL); 122 return pushresult(L, ok, NULL);
@@ -140,7 +139,7 @@ static int io_close (lua_State *L) {
140 139
141 140
142static int io_gc (lua_State *L) { 141static int io_gc (lua_State *L) {
143 FILE *f = *topfile(L); 142 FILE *f = *tofilep(L);
144 /* ignore closed files */ 143 /* ignore closed files */
145 if (f != NULL) 144 if (f != NULL)
146 aux_close(L); 145 aux_close(L);
@@ -149,7 +148,7 @@ static int io_gc (lua_State *L) {
149 148
150 149
151static int io_tostring (lua_State *L) { 150static int io_tostring (lua_State *L) {
152 FILE *f = *topfile(L); 151 FILE *f = *tofilep(L);
153 if (f == NULL) 152 if (f == NULL)
154 lua_pushliteral(L, "file (closed)"); 153 lua_pushliteral(L, "file (closed)");
155 else 154 else