diff options
| author | dkf <donal.k.fellows@manchester.ac.uk> | 2019-04-30 07:44:20 (GMT) |
|---|---|---|
| committer | dkf <donal.k.fellows@manchester.ac.uk> | 2019-04-30 07:44:20 (GMT) |
| commit | c0c8a1cbd0b9842eb58c4bad983e58138506306b (patch) | |
| tree | c64270043649f162f58e2a21d02b31fd07701916 /unix/tclUnixFCmd.c | |
| parent | b21bc1e84f40c6e09c7d3fc3766a4106eab719d8 (diff) | |
| download | tcl-c0c8a1cbd0b9842eb58c4bad983e58138506306b.zip tcl-c0c8a1cbd0b9842eb58c4bad983e58138506306b.tar.gz tcl-c0c8a1cbd0b9842eb58c4bad983e58138506306b.tar.bz2 | |
Starting to implement a temporary directory creator.
Diffstat (limited to 'unix/tclUnixFCmd.c')
| -rw-r--r-- | unix/tclUnixFCmd.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/unix/tclUnixFCmd.c b/unix/tclUnixFCmd.c index e963589..0089d91 100644 --- a/unix/tclUnixFCmd.c +++ b/unix/tclUnixFCmd.c @@ -2273,6 +2273,84 @@ 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; + Tcl_Obj *resultingNameObj; + +#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 */ + } + + 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 |
