From d8775ec1e6b5d0b8750dc6bf379a76c01ac10e5f Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Tue, 4 Mar 2025 02:05:50 -0300 Subject: miniposix: minimal subset of luaposix API for LuaRocks --- vendor/miniposix/Makefile | 10 +++ vendor/miniposix/miniposix-dev-1.rockspec | 15 +++++ vendor/miniposix/miniposix.c | 106 ++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 vendor/miniposix/Makefile create mode 100644 vendor/miniposix/miniposix-dev-1.rockspec create mode 100644 vendor/miniposix/miniposix.c (limited to 'vendor') diff --git a/vendor/miniposix/Makefile b/vendor/miniposix/Makefile new file mode 100644 index 00000000..09b550ce --- /dev/null +++ b/vendor/miniposix/Makefile @@ -0,0 +1,10 @@ +all: miniposix.a + +miniposix.a: miniposix.o + $(AR) rcu miniposix.a miniposix.o + +miniposix.o: miniposix.c + $(CC) -c miniposix.c + +clean: + rm *.o *.a diff --git a/vendor/miniposix/miniposix-dev-1.rockspec b/vendor/miniposix/miniposix-dev-1.rockspec new file mode 100644 index 00000000..a74af5f4 --- /dev/null +++ b/vendor/miniposix/miniposix-dev-1.rockspec @@ -0,0 +1,15 @@ +package = "miniposix" +version = "dev-1" +source = { + url = "./miniposix.c", +} +description = { + summary = "Minimal set of posix functions used by LuaRocks", + license = "MIT/X11", +} +build = { + type = "builtin", + modules = { + ["miniposix"] = "miniposix.c" + } +} diff --git a/vendor/miniposix/miniposix.c b/vendor/miniposix/miniposix.c new file mode 100644 index 00000000..21052f4d --- /dev/null +++ b/vendor/miniposix/miniposix.c @@ -0,0 +1,106 @@ +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +static int miniposix_chmod(lua_State* L) { + const char* filename = luaL_checkstring(L, 1); + const char* permissions = luaL_checkstring(L, 2); + mode_t mode = 0; + + if (strlen(permissions) != 9) { + return luaL_error(L, "invalid permissions string (expected \"rwxrwxrwx\" format)"); + } + + if (permissions[0] == 'r') { mode |= S_IRUSR; } + if (permissions[1] == 'w') { mode |= S_IWUSR; } + if (permissions[2] == 'x') { mode |= S_IXUSR; } + if (permissions[3] == 'r') { mode |= S_IRGRP; } + if (permissions[4] == 'w') { mode |= S_IWGRP; } + if (permissions[5] == 'x') { mode |= S_IXGRP; } + if (permissions[6] == 'r') { mode |= S_IROTH; } + if (permissions[7] == 'w') { mode |= S_IWOTH; } + if (permissions[8] == 'x') { mode |= S_IXOTH; } + + int err = chmod(filename, mode); + + if (err == 0) { + lua_pushboolean(L, 1); + return 1; + } + + lua_pushboolean(L, 0); + lua_pushstring(L, strerror(errno)); + return 2; +} + +static int miniposix_umask(lua_State* L) { + mode_t mode = umask(0); + mode = umask(mode); + + lua_pushinteger(L, mode); + return 1; +} + +static int miniposix_getpwuid(lua_State* L) { + const char* filename = luaL_checkstring(L, 1); + + struct passwd* data = getpwnam(filename); + + if (!data) { + lua_pushboolean(L, 0); + lua_pushstring(L, strerror(errno)); + return 2; + } + + lua_newtable(L); + lua_pushstring(L, data->pw_name); + lua_setfield(L, -2, "pw_name"); + + return 1; +} + +static int miniposix_geteuid(lua_State* L) { + lua_pushinteger(L, geteuid()); + return 1; +} + +static int miniposix_mkdtemp(lua_State* L) { + char* template = strdup(luaL_checkstring(L, 1)); + + char* updated = mkdtemp(template); + + if (!updated) { + free(template); + lua_pushboolean(L, 0); + lua_pushstring(L, strerror(errno)); + return 2; + } + + lua_pushstring(L, updated); + free(updated); + return 1; +} + +static struct luaL_Reg miniposix_lib[] = { + {"chmod", miniposix_chmod}, + {"umask", miniposix_umask}, + {"getpwuid", miniposix_getpwuid}, + {"geteuid", miniposix_geteuid}, + {"mkdtemp", miniposix_mkdtemp}, + {NULL, NULL} +}; + +int luaopen_miniposix(lua_State* L) { + lua_newtable(L); + luaL_setfuncs(L, miniposix_lib, 0); + return 1; +} + -- cgit v1.2.3-55-g6feb