X-Git-Url: http://git.openwrt.org/?a=blobdiff_plain;f=libs%2Fnixio%2Fsrc%2Ffile.c;fp=libs%2Fnixio%2Fsrc%2Ffile.c;h=4c18eed8b1c573131326303cee1f91b75d98c55a;hb=6b104b9a458db6c2a1624085121814e7c8cbb014;hp=3333e23691575057849b9a5780726b19a7357994;hpb=65b50a8f8a95d0d091a3f16ba5574842840c7289;p=project%2Fluci.git diff --git a/libs/nixio/src/file.c b/libs/nixio/src/file.c index 3333e23691..4c18eed8b1 100644 --- a/libs/nixio/src/file.c +++ b/libs/nixio/src/file.c @@ -22,6 +22,7 @@ #include #include #include +#include static int nixio_file(lua_State *L) { @@ -155,6 +156,29 @@ static int nixio_file_flush(lua_State *L) { return nixio__pstatus(L, !fflush(f)); } +static int nixio_file_lock(lua_State *L) { + int fd = fileno(nixio__checkfile(L)); + + const int j = lua_gettop(L); + int flags = 0; + for (int i=2; i<=j; i++) { + const char *flag = luaL_checkstring(L, i); + if (!strcmp(flag, "sh")) { + flags |= LOCK_SH; + } else if (!strcmp(flag, "ex")) { + flags |= LOCK_EX; + } else if (!strcmp(flag, "un")) { + flags |= LOCK_UN; + } else if (!strcmp(flag, "nb")) { + flags |= LOCK_NB; + } else { + return luaL_argerror(L, i, "supported values: sh, ex, un, nb"); + } + } + + return nixio__pstatus(L, flock(fd, flags)); +} + static int nixio_file_close(lua_State *L) { FILE **fpp = (FILE**)luaL_checkudata(L, 1, NIXIO_FILE_META); luaL_argcheck(L, *fpp, 1, "invalid file object"); @@ -187,6 +211,7 @@ static const luaL_reg M[] = { {"tell", nixio_file_tell}, {"seek", nixio_file_seek}, {"flush", nixio_file_flush}, + {"lock", nixio_file_lock}, {"close", nixio_file_close}, {"__gc", nixio_file__gc}, {"__tostring", nixio_file__tostring},