diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2025-03-04 02:05:50 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2025-03-10 10:51:04 -0300 |
commit | d8775ec1e6b5d0b8750dc6bf379a76c01ac10e5f (patch) | |
tree | 17d915ad87acdd2b1140460667b203ad145c0de6 /vendor | |
parent | 2c19375164920f7d85fa9f138cd7e52a87f12b58 (diff) | |
download | luarocks-d8775ec1e6b5d0b8750dc6bf379a76c01ac10e5f.tar.gz luarocks-d8775ec1e6b5d0b8750dc6bf379a76c01ac10e5f.tar.bz2 luarocks-d8775ec1e6b5d0b8750dc6bf379a76c01ac10e5f.zip |
miniposix: minimal subset of luaposix API for LuaRocks
Diffstat (limited to 'vendor')
-rw-r--r-- | vendor/miniposix/Makefile | 10 | ||||
-rw-r--r-- | vendor/miniposix/miniposix-dev-1.rockspec | 15 | ||||
-rw-r--r-- | vendor/miniposix/miniposix.c | 106 |
3 files changed, 131 insertions, 0 deletions
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 @@ | |||
1 | all: miniposix.a | ||
2 | |||
3 | miniposix.a: miniposix.o | ||
4 | $(AR) rcu miniposix.a miniposix.o | ||
5 | |||
6 | miniposix.o: miniposix.c | ||
7 | $(CC) -c miniposix.c | ||
8 | |||
9 | clean: | ||
10 | 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 @@ | |||
1 | package = "miniposix" | ||
2 | version = "dev-1" | ||
3 | source = { | ||
4 | url = "./miniposix.c", | ||
5 | } | ||
6 | description = { | ||
7 | summary = "Minimal set of posix functions used by LuaRocks", | ||
8 | license = "MIT/X11", | ||
9 | } | ||
10 | build = { | ||
11 | type = "builtin", | ||
12 | modules = { | ||
13 | ["miniposix"] = "miniposix.c" | ||
14 | } | ||
15 | } | ||
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 @@ | |||
1 | #include <lua.h> | ||
2 | #include <lauxlib.h> | ||
3 | |||
4 | #include <errno.h> | ||
5 | #include <fcntl.h> | ||
6 | #include <pwd.h> | ||
7 | #include <stdlib.h> | ||
8 | #include <string.h> | ||
9 | #include <unistd.h> | ||
10 | #include <sys/stat.h> | ||
11 | #include <sys/types.h> | ||
12 | |||
13 | static int miniposix_chmod(lua_State* L) { | ||
14 | const char* filename = luaL_checkstring(L, 1); | ||
15 | const char* permissions = luaL_checkstring(L, 2); | ||
16 | mode_t mode = 0; | ||
17 | |||
18 | if (strlen(permissions) != 9) { | ||
19 | return luaL_error(L, "invalid permissions string (expected \"rwxrwxrwx\" format)"); | ||
20 | } | ||
21 | |||
22 | if (permissions[0] == 'r') { mode |= S_IRUSR; } | ||
23 | if (permissions[1] == 'w') { mode |= S_IWUSR; } | ||
24 | if (permissions[2] == 'x') { mode |= S_IXUSR; } | ||
25 | if (permissions[3] == 'r') { mode |= S_IRGRP; } | ||
26 | if (permissions[4] == 'w') { mode |= S_IWGRP; } | ||
27 | if (permissions[5] == 'x') { mode |= S_IXGRP; } | ||
28 | if (permissions[6] == 'r') { mode |= S_IROTH; } | ||
29 | if (permissions[7] == 'w') { mode |= S_IWOTH; } | ||
30 | if (permissions[8] == 'x') { mode |= S_IXOTH; } | ||
31 | |||
32 | int err = chmod(filename, mode); | ||
33 | |||
34 | if (err == 0) { | ||
35 | lua_pushboolean(L, 1); | ||
36 | return 1; | ||
37 | } | ||
38 | |||
39 | lua_pushboolean(L, 0); | ||
40 | lua_pushstring(L, strerror(errno)); | ||
41 | return 2; | ||
42 | } | ||
43 | |||
44 | static int miniposix_umask(lua_State* L) { | ||
45 | mode_t mode = umask(0); | ||
46 | mode = umask(mode); | ||
47 | |||
48 | lua_pushinteger(L, mode); | ||
49 | return 1; | ||
50 | } | ||
51 | |||
52 | static int miniposix_getpwuid(lua_State* L) { | ||
53 | const char* filename = luaL_checkstring(L, 1); | ||
54 | |||
55 | struct passwd* data = getpwnam(filename); | ||
56 | |||
57 | if (!data) { | ||
58 | lua_pushboolean(L, 0); | ||
59 | lua_pushstring(L, strerror(errno)); | ||
60 | return 2; | ||
61 | } | ||
62 | |||
63 | lua_newtable(L); | ||
64 | lua_pushstring(L, data->pw_name); | ||
65 | lua_setfield(L, -2, "pw_name"); | ||
66 | |||
67 | return 1; | ||
68 | } | ||
69 | |||
70 | static int miniposix_geteuid(lua_State* L) { | ||
71 | lua_pushinteger(L, geteuid()); | ||
72 | return 1; | ||
73 | } | ||
74 | |||
75 | static int miniposix_mkdtemp(lua_State* L) { | ||
76 | char* template = strdup(luaL_checkstring(L, 1)); | ||
77 | |||
78 | char* updated = mkdtemp(template); | ||
79 | |||
80 | if (!updated) { | ||
81 | free(template); | ||
82 | lua_pushboolean(L, 0); | ||
83 | lua_pushstring(L, strerror(errno)); | ||
84 | return 2; | ||
85 | } | ||
86 | |||
87 | lua_pushstring(L, updated); | ||
88 | free(updated); | ||
89 | return 1; | ||
90 | } | ||
91 | |||
92 | static struct luaL_Reg miniposix_lib[] = { | ||
93 | {"chmod", miniposix_chmod}, | ||
94 | {"umask", miniposix_umask}, | ||
95 | {"getpwuid", miniposix_getpwuid}, | ||
96 | {"geteuid", miniposix_geteuid}, | ||
97 | {"mkdtemp", miniposix_mkdtemp}, | ||
98 | {NULL, NULL} | ||
99 | }; | ||
100 | |||
101 | int luaopen_miniposix(lua_State* L) { | ||
102 | lua_newtable(L); | ||
103 | luaL_setfuncs(L, miniposix_lib, 0); | ||
104 | return 1; | ||
105 | } | ||
106 | |||