* 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>
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;
+}
+
+int lc_mkdir(lua_State* L)
+{
+ int ret = mkdir(luaL_checkstring(L, 1), S_IRUSR | S_IWUSR | S_IXUSR
+ | S_IRGRP | S_IWGRP | S_IXGRP
+ | S_IROTH | S_IXOTH); /* mode 775 */
+
+ lua_pushboolean(L, ret==0);
+ if(ret)
+ {
+ lua_pushstring(L, strerror(errno));
+ return 2;
+ }
+ return 1;
+}
+
/* Like POSIX's setrlimit()/getrlimit() API functions.
*
* Syntax:
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");
+ luaL_Reg exports[] = {
+ { "abort", lc_abort },
- lua_pushcfunction(L, lc_syslog_open);
- lua_setfield(L, -2, "syslog_open");
+ { "daemonize", lc_daemonize },
- lua_pushcfunction(L, lc_syslog_close);
- lua_setfield(L, -2, "syslog_close");
+ { "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_log);
- lua_setfield(L, -2, "syslog_log");
+ { "getpid", lc_getpid },
+ { "getuid", lc_getuid },
+ { "getgid", lc_getgid },
- lua_pushcfunction(L, lc_syslog_setmask);
- lua_setfield(L, -2, "syslog_setminlevel");
+ { "setuid", lc_setuid },
+ { "setgid", lc_setgid },
- lua_pushcfunction(L, lc_getpid);
- lua_setfield(L, -2, "getpid");
+ { "umask", lc_umask },
- lua_pushcfunction(L, lc_getuid);
- lua_setfield(L, -2, "getuid");
- lua_pushcfunction(L, lc_getgid);
- lua_setfield(L, -2, "getgid");
+ { "mkdir", lc_mkdir },
- 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");