summaryrefslogtreecommitdiff
path: root/luxio.c
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2016-09-02 10:59:46 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2016-09-02 10:59:46 +0100
commitf896ada0a729b1d09cd13f961d9dce5655852a5d (patch)
treed1782fb8375ad2dd262d018bc6570ff3b23eac40 /luxio.c
parent532682487af1e74ef9866c1895f4924c8700dcda (diff)
downloadluxio-f896ada0a729b1d09cd13f961d9dce5655852a5d.tar.bz2
Add mkstemp() support
Diffstat (limited to 'luxio.c')
-rw-r--r--luxio.c37
1 files changed, 37 insertions, 0 deletions
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