aboutsummaryrefslogtreecommitdiff
path: root/jim-aio.c
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2014-08-03 17:40:11 +1000
committerSteve Bennett <steveb@workware.net.au>2014-08-03 17:51:58 +1000
commit0b60827ba7bbbefd991a99dfcae4973d51387f82 (patch)
treef18569cddf2770bf8b108f60f1354c560c459c4e /jim-aio.c
parenta07760e95c68569b635ecc97465175d9a306ff37 (diff)
downloadjimtcl-0b60827ba7bbbefd991a99dfcae4973d51387f82.zip
jimtcl-0b60827ba7bbbefd991a99dfcae4973d51387f82.tar.gz
jimtcl-0b60827ba7bbbefd991a99dfcae4973d51387f82.tar.bz2
Temporary file creation should respect $TMPDIR
This applies to [exec] and [file tempfile] Reported-by: Jakub Wilk Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'jim-aio.c')
-rw-r--r--jim-aio.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/jim-aio.c b/jim-aio.c
index 22c77b9..70fc1e3 100644
--- a/jim-aio.c
+++ b/jim-aio.c
@@ -45,6 +45,7 @@
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
+#include <sys/stat.h>
#endif
#include "jim.h"
@@ -1411,6 +1412,54 @@ static int JimAioSockCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
}
#endif /* JIM_BOOTSTRAP */
+/**
+ * Returns the file descriptor of a writable, newly created temp file
+ * or -1 on error.
+ *
+ * On success, leaves the filename in the interpreter result, otherwise
+ * leaves an error message.
+ */
+int Jim_MakeTempFile(Jim_Interp *interp, const char *template)
+{
+#ifdef HAVE_MKSTEMP
+ int fd;
+ mode_t mask;
+ Jim_Obj *filenameObj;
+
+ if (template == NULL) {
+ const char *tmpdir = getenv("TMPDIR");
+ if (tmpdir == NULL || *tmpdir == '\0' || access(tmpdir, W_OK) != 0) {
+ tmpdir = "/tmp/";
+ }
+ filenameObj = Jim_NewStringObj(interp, tmpdir, -1);
+ if (tmpdir[0] && tmpdir[strlen(tmpdir) - 1] != '/') {
+ Jim_AppendString(interp, filenameObj, "/", 1);
+ }
+ Jim_AppendString(interp, filenameObj, "tcl.tmp.XXXXXX", -1);
+ }
+ else {
+ filenameObj = Jim_NewStringObj(interp, template, -1);
+ }
+
+ mask = umask(S_IXUSR | S_IRWXG | S_IRWXO);
+
+ /* Update the template name directly with the filename */
+ fd = mkstemp(filenameObj->bytes);
+ umask(mask);
+ if (fd < 0) {
+ Jim_SetResultString(interp, "Failed to create tempfile", -1);
+ Jim_FreeNewObj(interp, filenameObj);
+ return -1;
+ }
+
+ Jim_SetResult(interp, filenameObj);
+ return fd;
+#else
+ Jim_SetResultString(interp, "tempfile not supported", -1);
+ return -1;
+#endif
+}
+
FILE *Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *command)
{
Jim_Cmd *cmdPtr = Jim_GetCommand(interp, command, JIM_ERRMSG);