From f7707ad3ff279fe09e8885b3a2ea2b01f629950b Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Mon, 20 Oct 2008 11:32:43 +0300 Subject: Add posix.mkstemp() to Lua posix lib - lifted from apt-rpm --- luaext/lposix.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'luaext') diff --git a/luaext/lposix.c b/luaext/lposix.c index 7f3051449..e7e0335a6 100644 --- a/luaext/lposix.c +++ b/luaext/lposix.c @@ -761,6 +761,37 @@ static int Psysconf(lua_State *L) /** sysconf([selector]) */ return doselection(L, 1, Ssysconf, Fsysconf, NULL); } +static int Pmkstemp(lua_State *L) +{ + const char *path; + char *dynpath; + int fd; + FILE **f; + + path = luaL_checkstring(L, 1); + if (path == NULL) + return 0; + dynpath = strdup(path); + fd = mkstemp(dynpath); + f = (FILE**)lua_newuserdata(L, sizeof(FILE*)); + if (f == NULL) { + close(fd); + free(dynpath); + return 0; + } + *f = fdopen(fd, "a+"); + lua_pushstring(L, dynpath); + free(dynpath); + luaL_getmetatable(L, "FILE*"); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + luaL_error(L, "FILE* metatable not available " + "(io not loaded?)"); + } else { + lua_setmetatable(L, -3); + } + return 2; +} static const luaL_reg R[] = { @@ -784,6 +815,7 @@ static const luaL_reg R[] = {"link", Plink}, {"mkdir", Pmkdir}, {"mkfifo", Pmkfifo}, + {"mkstemp", Pmkstemp}, {"pathconf", Ppathconf}, {"putenv", Pputenv}, {"readlink", Preadlink}, -- cgit v1.2.3