diff options
author | Lua Team <team@lua.org> | 2013-03-21 12:00:00 +0000 |
---|---|---|
committer | repogen <> | 2013-03-21 12:00:00 +0000 |
commit | dc27609467d2699ac9252e89d632432ac5f798f2 (patch) | |
tree | b934d79a77a76df417078dc6ac162bb0012708bb /src/liolib.c | |
parent | a101faf3e9db5b8b47cb0bcfb973b13d46ca304a (diff) | |
download | lua-github-5.2.2-rc4.tar.gz |
Diffstat (limited to 'src/liolib.c')
-rw-r--r-- | src/liolib.c | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/src/liolib.c b/src/liolib.c index 4814aa2c..3f80db19 100644 --- a/src/liolib.c +++ b/src/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.108 2011/11/25 12:50:03 roberto Exp $ +** $Id: liolib.c,v 2.111 2013/03/21 13:57:27 roberto Exp $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -29,6 +29,20 @@ #include "lualib.h" +#if !defined(lua_checkmode) + +/* +** Check whether 'mode' matches '[rwa]%+?b?'. +** Change this macro to accept other modes for 'fopen' besides +** the standard ones. +*/ +#define lua_checkmode(mode) \ + (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \ + (*mode != '+' || ++mode) && /* skip if char is '+' */ \ + (*mode != 'b' || ++mode) && /* skip if char is 'b' */ \ + (*mode == '\0')) + +#endif /* ** {====================================================== @@ -212,14 +226,8 @@ static int io_open (lua_State *L) { const char *filename = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newfile(L); - int i = 0; - /* check whether 'mode' matches '[rwa]%+?b?' */ - if (!(mode[i] != '\0' && strchr("rwa", mode[i++]) != NULL && - (mode[i] != '+' || ++i) && /* skip if char is '+' */ - (mode[i] != 'b' || ++i) && /* skip if char is 'b' */ - (mode[i] == '\0'))) - return luaL_error(L, "invalid mode " LUA_QS - " (should match " LUA_QL("[rwa]%%+?b?") ")", mode); + const char *md = mode; /* to traverse/check mode */ + luaL_argcheck(L, lua_checkmode(md), 2, "invalid mode"); p->f = fopen(filename, mode); return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; } |