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

Loading…
Cancel
Save