refactor ex_lock/ex_unlock to a single function

master
mark 19 years ago
parent 7f295f8d22
commit 4f5b35bb26

@ -289,7 +289,7 @@ static int ex_dir(lua_State *L)
case LUA_TUSERDATA: case LUA_TUSERDATA:
pd = luaL_checkudata(L, 1, DIR_HANDLE); pd = luaL_checkudata(L, 1, DIR_HANDLE);
do d = readdir(*pd); do d = readdir(*pd);
while (d && isdotfile(d->cFileName)) continue; while (d && isdotfile(d->cFileName));
if (!d) return push_error(L); if (!d) return push_error(L);
new_dirent(L); /* diriter ... entry */ new_dirent(L); /* diriter ... entry */
diriter_getpathname(L, 1); /* diriter ... entry dir */ diriter_getpathname(L, 1); /* diriter ... entry dir */
@ -332,31 +332,31 @@ static int file_lock(lua_State *L,
default: default:
return luaL_error(L, "invalid mode"); return luaL_error(L, "invalid mode");
} }
if (!ret) return push_error(L); if (!ret)
return push_error(L);
/* return the file */ /* return the file */
lua_settop(L, 1); lua_settop(L, 1);
return 1; return 1;
} }
/* file mode [offset [length]] -- file/nil error */ static const char *opt_mode(lua_State *L, int *pidx)
static int ex_lock(lua_State *L)
{ {
FILE *f = check_file(L, 1, NULL); if (lua_type(L, *pidx) != LUA_TSTRING)
const char *mode = luaL_checkstring(L, 2); return "u";
long offset = luaL_optnumber(L, 3, 0); return lua_tostring(L, (*pidx)++);
long length = luaL_optnumber(L, 4, 0);
return file_lock(L, f, mode, offset, length);
} }
/* file [offset [length]] -- file/nil error */ /* file [mode] [offset [length]] -- file/nil error */
static int ex_unlock(lua_State *L) static int ex_lock(lua_State *L)
{ {
lua_pushliteral(L, "u"); FILE *f = check_file(L, 1, NULL);
lua_insert(L, 2); int argi = 2;
return ex_lock(L); const char *mode = opt_mode(L, &argi);
long offset = luaL_optnumber(L, argi, 0);
long length = luaL_optnumber(L, argi + 1, 0);
return file_lock(L, f, mode, offset, length);
} }
/* -- in out/nil error */ /* -- in out/nil error */
static int ex_pipe(lua_State *L) static int ex_pipe(lua_State *L)
{ {
@ -491,7 +491,7 @@ int luaopen_ex(lua_State *L)
{"pipe", ex_pipe}, {"pipe", ex_pipe},
#define ex_iofile_methods (ex_iolib + 1) #define ex_iofile_methods (ex_iolib + 1)
{"lock", ex_lock}, {"lock", ex_lock},
{"unlock", ex_unlock}, {"unlock", ex_lock},
{0,0} }; {0,0} };
const luaL_reg ex_oslib[] = { const luaL_reg ex_oslib[] = {
/* environment */ /* environment */

Loading…
Cancel
Save