diff options
author | dkf <donal.k.fellows@manchester.ac.uk> | 2012-11-14 13:01:46 (GMT) |
---|---|---|
committer | dkf <donal.k.fellows@manchester.ac.uk> | 2012-11-14 13:01:46 (GMT) |
commit | 2e074de52a82c12f60c06c55d7d61afa28018bc5 (patch) | |
tree | 50025dfed29a1aad376792dac26c592de41f9d6a /unix/tclUnixPipe.c | |
parent | 2b18d7d0193a3510b0739b88b05e46337d1a8571 (diff) | |
download | tcl-2e074de52a82c12f60c06c55d7d61afa28018bc5.zip tcl-2e074de52a82c12f60c06c55d7d61afa28018bc5.tar.gz tcl-2e074de52a82c12f60c06c55d7d61afa28018bc5.tar.bz2 |
* unix/tclUnixFCmd.c (TclUnixOpenTemporaryFile): [Bug 2933003]: Factor
out all the code to do temporary file creation so that it is possible
to make it correct in one place. Allow overriding of the back-stop
default temporary file location at compile time by setting the
TCL_TEMPORARY_FILE_DIRECTORY #def to a string containing the directory
name (defaults to "/tmp" as that is the most common default).
Diffstat (limited to 'unix/tclUnixPipe.c')
-rw-r--r-- | unix/tclUnixPipe.c | 43 |
1 files changed, 11 insertions, 32 deletions
diff --git a/unix/tclUnixPipe.c b/unix/tclUnixPipe.c index 654c9d8..9c21b28 100644 --- a/unix/tclUnixPipe.c +++ b/unix/tclUnixPipe.c @@ -188,28 +188,16 @@ TclFile TclpCreateTempFile( const char *contents) /* String to write into temp file, or NULL. */ { - char fileName[L_tmpnam + 9]; - const char *native; - Tcl_DString dstring; - int fd; + int fd = TclUnixOpenTemporaryFile(NULL, NULL, NULL, NULL); - /* - * We should also check against making more then TMP_MAX of these. - */ - - strcpy(fileName, P_tmpdir); /* INTL: Native. */ - if (fileName[strlen(fileName) - 1] != '/') { - strcat(fileName, "/"); /* INTL: Native. */ - } - strcat(fileName, "tclXXXXXX"); - fd = mkstemp(fileName); /* INTL: Native. */ if (fd == -1) { return NULL; } fcntl(fd, F_SETFD, FD_CLOEXEC); - unlink(fileName); /* INTL: Native. */ - if (contents != NULL) { + Tcl_DString dstring; + char *native; + native = Tcl_UtfToExternalDString(NULL, contents, -1, &dstring); if (write(fd, native, Tcl_DStringLength(&dstring)) == -1) { close(fd); @@ -241,29 +229,20 @@ TclpCreateTempFile( Tcl_Obj * TclpTempFileName(void) { - char fileName[L_tmpnam + 9]; - Tcl_Obj *result = NULL; + Tcl_Obj *nameObj = Tcl_NewObj(); int fd; - /* - * We should also check against making more then TMP_MAX of these. - */ - - strcpy(fileName, P_tmpdir); /* INTL: Native. */ - if (fileName[strlen(fileName) - 1] != '/') { - strcat(fileName, "/"); /* INTL: Native. */ - } - strcat(fileName, "tclXXXXXX"); - fd = mkstemp(fileName); /* INTL: Native. */ + Tcl_IncrRefCount(nameObj); + fd = TclUnixOpenTemporaryFile(NULL, NULL, NULL, nameObj); if (fd == -1) { + Tcl_DecrRefCount(nameObj); return NULL; } - fcntl(fd, F_SETFD, FD_CLOEXEC); - unlink(fileName); /* INTL: Native. */ - result = TclpNativeToNormalized(fileName); + fcntl(fd, F_SETFD, FD_CLOEXEC); + TclpObjDeleteFile(nameObj); close(fd); - return result; + return nameObj; } /* |