diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2016-09-02 10:59:46 +0100 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2016-09-02 10:59:46 +0100 |
commit | f896ada0a729b1d09cd13f961d9dce5655852a5d (patch) | |
tree | d1782fb8375ad2dd262d018bc6570ff3b23eac40 /luxio.c | |
parent | 532682487af1e74ef9866c1895f4924c8700dcda (diff) | |
download | luxio-f896ada0a729b1d09cd13f961d9dce5655852a5d.tar.bz2 |
Add mkstemp() support
Diffstat (limited to 'luxio.c')
-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 |