From f896ada0a729b1d09cd13f961d9dce5655852a5d Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Fri, 2 Sep 2016 10:59:46 +0100 Subject: Add mkstemp() support --- luxio.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'luxio.c') diff --git a/luxio.c b/luxio.c index ec5a999..94aacef 100644 --- a/luxio.c +++ b/luxio.c @@ -1062,6 +1062,42 @@ luxio_readlink(lua_State *L) /* POSIX.1-2001, Unknown location */ return 2; } +/*** Create a unique temporary file. + +@tparam string|nil pattern Pattern to use, or nil for default pattern. +@treturn number FD of temporary file (-1 on error) +@treturn string|errno Filename of temporary file or errno on error +@function mkstemp +*/ +static int +luxio_mkstemp(lua_State *L) +{ + char fnamebuf[PATH_MAX]; + size_t infname_len; + const char *infname = luaL_optlstring(L, 1, "lux_XXXXXX", &infname_len); + int fd, saved_errno; + + if (infname_len > (PATH_MAX - 1)) { + lua_pushinteger(L, -1); + lua_pushinteger(L, EINVAL); + return 2; + } + + strcpy(fnamebuf, infname); /* Safe because of above test */ + + fd = mkstemp(fnamebuf); + if (fd == -1) { + saved_errno = errno; + lua_pushnumber(L, -1); + lua_pushnumber(L, saved_errno); + return 2; + } + + lua_pushnumber(L, fd); + lua_pushstring(L, fnamebuf); + return 2; +} + /*** Special file creation @section specfile */ @@ -3777,6 +3813,7 @@ luxio_functions[] = { { "unlink", luxio_unlink }, { "symlink", luxio_symlink }, { "readlink", luxio_readlink }, + { "mkstemp", luxio_mkstemp }, #ifdef HAVE_SENDFILE { "sendfile", luxio_sendfile }, #endif -- cgit v1.2.1