]> git.e2factory.org Git - e2factory.git/commitdiff
Add mkdtemp() wrapper.
authorTobias Ulmer <tu@emlix.com>
Tue, 12 Nov 2013 14:46:32 +0000 (15:46 +0100)
committerTobias Ulmer <tu@emlix.com>
Wed, 16 Nov 2016 14:41:17 +0000 (15:41 +0100)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/le2lib.c

index 5c99f3018dcef2e3eac434ac53fcb89c03f44c5e..fc5db0482486d9b72aadb1c371ae269f6f060453 100644 (file)
@@ -756,6 +756,34 @@ do_chmod(lua_State *L)
        return 1;
 }
 
+static int
+do_mkdtemp(lua_State *L)
+{
+       const char *template_in = luaL_checkstring(L, 1);
+       char template[PATH_MAX];
+
+       if (snprintf(template, PATH_MAX, "%s", template_in)
+           >= PATH_MAX) {
+               lua_pushboolean(L, 0);
+               lua_pushstring(L, "template does not fit in PATH_MAX");
+
+               return 2;
+       }
+
+       if (mkdtemp(template) == NULL) {
+               lua_pushboolean(L, 0);
+               lua_pushstring(L, strerror(errno));
+
+               return 2;
+       }
+
+       lua_pushboolean(L, 1);
+       lua_pushnil(L);
+       lua_pushstring(L, template);
+
+       return 3;
+}
+
 /*
  * Hook that gets called once an interrupt has been requested.
  * Calls e2lib.interrupt_hook() to deal with any cleanup that might be required.
@@ -807,6 +835,7 @@ static luaL_Reg lib[] = {
        { "hardlink", do_hardlink },
        { "kill", do_kill },
        { "mkdir", do_mkdir },
+       { "mkdtemp", do_mkdtemp },
        { "parse_mode", do_parse_mode },
        { "poll", poll_fd },
        { "rmdir", do_rmdir },