diff options
-rw-r--r-- | luxio.c | 37 |
1 files changed, 37 insertions, 0 deletions
@@ -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 |