diff options
author | Ed Maste <emaste@FreeBSD.org> | 2020-03-13 15:40:35 +0000 |
---|---|---|
committer | Ed Maste <emaste@FreeBSD.org> | 2020-03-13 15:40:35 +0000 |
commit | 405e3338ac841999673056a2b5537b4c0ad677db (patch) | |
tree | 13e7d1e2c8a93685a6f025b276d2633afed7d007 /libexec/flua/modules | |
parent | a2386b6f6a872873609354f6099ae97f2de5356b (diff) | |
download | src-405e3338ac841999673056a2b5537b4c0ad677db.tar.gz src-405e3338ac841999673056a2b5537b4c0ad677db.zip |
flua: implement chmod
Lua does not provide a native way to change the permission of a file.
Submitted by: Yang Wang <2333@outlook.jp>
Reviewed by: kevans
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D24036
Notes
Notes:
svn path=/head/; revision=358960
Diffstat (limited to 'libexec/flua/modules')
-rw-r--r-- | libexec/flua/modules/lposix.c | 38 | ||||
-rw-r--r-- | libexec/flua/modules/lposix.h | 1 |
2 files changed, 39 insertions, 0 deletions
diff --git a/libexec/flua/modules/lposix.c b/libexec/flua/modules/lposix.c index 208802456fed..adf3a7bb9a1f 100644 --- a/libexec/flua/modules/lposix.c +++ b/libexec/flua/modules/lposix.c @@ -27,6 +27,10 @@ #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); +#include <sys/stat.h> + +#include <errno.h> +#include <string.h> #include <unistd.h> #include <lua.h> @@ -38,6 +42,28 @@ __FBSDID("$FreeBSD$"); */ static int +lua_chmod(lua_State *L) +{ + int n; + const char *path; + mode_t mode; + + n = lua_gettop(L); + luaL_argcheck(L, n == 2, n > 2 ? 3 : n, + "chmod takes exactly two arguments"); + path = luaL_checkstring(L, 1); + mode = (mode_t)luaL_checkinteger(L, 2); + if (chmod(path, mode) == -1) { + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); + lua_pushinteger(L, errno); + return 3; + } + lua_pushinteger(L, 0); + return 1; +} + +static int lua_getpid(lua_State *L) { int n; @@ -49,6 +75,11 @@ lua_getpid(lua_State *L) } #define REG_SIMPLE(n) { #n, lua_ ## n } +static const struct luaL_Reg sys_statlib[] = { + REG_SIMPLE(chmod), + { NULL, NULL }, +}; + static const struct luaL_Reg unistdlib[] = { REG_SIMPLE(getpid), { NULL, NULL }, @@ -56,6 +87,13 @@ static const struct luaL_Reg unistdlib[] = { #undef REG_SIMPLE int +luaopen_posix_sys_stat(lua_State *L) +{ + luaL_newlib(L, sys_statlib); + return 1; +} + +int luaopen_posix_unistd(lua_State *L) { luaL_newlib(L, unistdlib); diff --git a/libexec/flua/modules/lposix.h b/libexec/flua/modules/lposix.h index 4c771d79769f..d2d9ec0cd677 100644 --- a/libexec/flua/modules/lposix.h +++ b/libexec/flua/modules/lposix.h @@ -8,4 +8,5 @@ #include <lua.h> +int luaopen_posix_sys_stat(lua_State *L); int luaopen_posix_unistd(lua_State *L); |