util.pposix: Replace the unwieldy module table generation with luaL_register() call...
[prosody.git] / util-src / pposix.c
index ea15033d4110abff373f3bf4bcfd83821506be75..49521a163e73dddac2521c173f40122896bb3fd9 100644 (file)
 * POSIX support functions for Lua
 */
 
-#define MODULE_VERSION "0.3.1"
+#define MODULE_VERSION "0.3.2"
 
 #include <stdlib.h>
+#include <math.h>
 #include <unistd.h>
 #include <libgen.h>
 #include <sys/resource.h>
@@ -358,6 +359,18 @@ int lc_setgid(lua_State* L)
        return 2;
 }
 
+int lc_umask(lua_State* L)
+{
+       char old_mode_string[7];
+       mode_t old_mode = umask(strtoul(luaL_checkstring(L, 1), NULL, 8));
+
+       snprintf(old_mode_string, sizeof(old_mode_string), "%03o", old_mode);
+       old_mode_string[sizeof(old_mode_string)-1] = 0;
+       lua_pushstring(L, old_mode_string);
+
+       return 1;
+}
+
 /*     Like POSIX's setrlimit()/getrlimit() API functions.
  *
  *     Syntax:
@@ -463,53 +476,42 @@ int lc_getrlimit(lua_State *L) {
        return 3;
 }
 
-void lc_abort(lua_State* L)
+int lc_abort(lua_State* L)
 {
        abort();
+       return 0;
 }
 
 /* Register functions */
 
 int luaopen_util_pposix(lua_State *L)
 {
-       lua_newtable(L);
-
-       lua_pushcfunction(L, lc_abort);
-       lua_setfield(L, -2, "abort");
-
-       lua_pushcfunction(L, lc_daemonize);
-       lua_setfield(L, -2, "daemonize");
-
-       lua_pushcfunction(L, lc_syslog_open);
-       lua_setfield(L, -2, "syslog_open");
+       luaL_Reg exports[] = {
+               { "abort", lc_abort },
 
-       lua_pushcfunction(L, lc_syslog_close);
-       lua_setfield(L, -2, "syslog_close");
+               { "daemonize", lc_daemonize },
 
-       lua_pushcfunction(L, lc_syslog_log);
-       lua_setfield(L, -2, "syslog_log");
+               { "syslog_open", lc_syslog_open },
+               { "syslog_close", lc_syslog_close },
+               { "syslog_log", lc_syslog_log },
+               { "syslog_setminlevel", lc_syslog_setmask },
 
-       lua_pushcfunction(L, lc_syslog_setmask);
-       lua_setfield(L, -2, "syslog_setminlevel");
+               { "getpid", lc_getpid },
+               { "getuid", lc_getuid },
+               { "getgid", lc_getgid },
 
-       lua_pushcfunction(L, lc_getpid);
-       lua_setfield(L, -2, "getpid");
+               { "setuid", lc_setuid },
+               { "setgid", lc_setgid },
 
-       lua_pushcfunction(L, lc_getuid);
-       lua_setfield(L, -2, "getuid");
-       lua_pushcfunction(L, lc_getgid);
-       lua_setfield(L, -2, "getgid");
+               { "umask", lc_umask },
 
-       lua_pushcfunction(L, lc_setuid);
-       lua_setfield(L, -2, "setuid");
-       lua_pushcfunction(L, lc_setgid);
-       lua_setfield(L, -2, "setgid");
+               { "setrlimit", lc_setrlimit },
+               { "getrlimit", lc_getrlimit },
 
-       lua_pushcfunction(L, lc_setrlimit);
-       lua_setfield(L, -2, "setrlimit");
+               { NULL, NULL }
+       };
 
-       lua_pushcfunction(L, lc_getrlimit);
-       lua_setfield(L, -2, "getrlimit");
+       luaL_register(L, "pposix",  exports);
 
        lua_pushliteral(L, "pposix");
        lua_setfield(L, -2, "_NAME");