summaryrefslogtreecommitdiffstats
path: root/unix/tclUnixFCmd.c
diff options
context:
space:
mode:
authordkf <donal.k.fellows@manchester.ac.uk>2019-05-25 08:17:42 (GMT)
committerdkf <donal.k.fellows@manchester.ac.uk>2019-05-25 08:17:42 (GMT)
commit815f8822018df40f65d4dc2ce7029a9223da2ab6 (patch)
treee1b6ac62c11e8c4b03a5255b3ce9aec0641ba2d2 /unix/tclUnixFCmd.c
parente09e63e37e690f36ffc3b95ea85e966809c4b305 (diff)
parent9290e6c1ace5bd8e3074900948ea3ad677f73832 (diff)
downloadtcl-815f8822018df40f65d4dc2ce7029a9223da2ab6.zip
tcl-815f8822018df40f65d4dc2ce7029a9223da2ab6.tar.gz
tcl-815f8822018df40f65d4dc2ce7029a9223da2ab6.tar.bz2
Merge 8.7
Diffstat (limited to 'unix/tclUnixFCmd.c')
-rw-r--r--unix/tclUnixFCmd.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/unix/tclUnixFCmd.c b/unix/tclUnixFCmd.c
index 6dfc772..f36aecd 100644
--- a/unix/tclUnixFCmd.c
+++ b/unix/tclUnixFCmd.c
@@ -2274,6 +2274,85 @@ DefaultTempDir(void)
return TCL_TEMPORARY_FILE_DIRECTORY;
}
+/*
+ *----------------------------------------------------------------------
+ *
+ * TclpCreateTemporaryDirectory --
+ *
+ * Creates a temporary directory, possibly based on the supplied bits and
+ * pieces of template supplied in the arguments.
+ *
+ * Results:
+ * An object (refcount 0) containing the name of the newly-created
+ * directory, or NULL on failure.
+ *
+ * Side effects:
+ * Accesses the native filesystem. Makes a directory.
+ *
+ *----------------------------------------------------------------------
+ */
+
+Tcl_Obj *
+TclpCreateTemporaryDirectory(
+ Tcl_Obj *dirObj,
+ Tcl_Obj *basenameObj)
+{
+ Tcl_DString template, tmp;
+ const char *string;
+
+#define DEFAULT_TEMP_DIR_PREFIX "tcl"
+
+ /*
+ * Build the template in writable memory from the user-supplied pieces and
+ * some defaults.
+ */
+
+ if (dirObj) {
+ string = TclGetString(dirObj);
+ Tcl_UtfToExternalDString(NULL, string, dirObj->length, &template);
+ } else {
+ Tcl_DStringInit(&template);
+ Tcl_DStringAppend(&template, DefaultTempDir(), -1); /* INTL: native */
+ }
+
+ if (Tcl_DStringValue(&template)[Tcl_DStringLength(&template) - 1] != '/') {
+ TclDStringAppendLiteral(&template, "/");
+ }
+
+ if (basenameObj) {
+ string = TclGetString(basenameObj);
+ if (basenameObj->length) {
+ Tcl_UtfToExternalDString(NULL, string, basenameObj->length, &tmp);
+ TclDStringAppendDString(&template, &tmp);
+ Tcl_DStringFree(&tmp);
+ } else {
+ TclDStringAppendLiteral(&template, DEFAULT_TEMP_DIR_PREFIX);
+ }
+ } else {
+ TclDStringAppendLiteral(&template, DEFAULT_TEMP_DIR_PREFIX);
+ }
+
+ TclDStringAppendLiteral(&template, "_XXXXXX");
+
+ /*
+ * Make the temporary directory.
+ */
+
+ if (mkdtemp(Tcl_DStringValue(&template)) == NULL) {
+ Tcl_DStringFree(&template);
+ return NULL;
+ }
+
+ /*
+ * The template has been updated. Tell the caller what it was.
+ */
+
+ Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(&template),
+ Tcl_DStringLength(&template), &tmp);
+ Tcl_DStringFree(&template);
+ return TclDStringToObj(&tmp);
+}
+
#if defined(__CYGWIN__)
static void