summaryrefslogtreecommitdiff
path: root/luaext
diff options
context:
space:
mode:
authorPanu Matilainen <pmatilai@redhat.com>2008-10-20 11:32:43 +0300
committerPanu Matilainen <pmatilai@redhat.com>2008-10-20 11:32:43 +0300
commitf7707ad3ff279fe09e8885b3a2ea2b01f629950b (patch)
treeeab4ab0d848d9cc6629c5ed5a6f6fedaea374723 /luaext
parentd34646ab10a992092c9d2662cbbe573945c22332 (diff)
downloadrpm-f7707ad3ff279fe09e8885b3a2ea2b01f629950b.tar.gz
rpm-f7707ad3ff279fe09e8885b3a2ea2b01f629950b.tar.bz2
rpm-f7707ad3ff279fe09e8885b3a2ea2b01f629950b.zip
Add posix.mkstemp() to Lua posix lib
- lifted from apt-rpm
Diffstat (limited to 'luaext')
-rw-r--r--luaext/lposix.c32
1 files changed, 32 insertions, 0 deletions
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},