# TIP 210: Add 'tempfile' Subcommand to 'file'
Author: Bob Techentin <[email protected]>
Author: Donal K. Fellows <[email protected]>
State: Final
Type: Project
Vote: Done
Created: 19-Jul-2004
Post-History:
Keywords: Tcl,filename
Tcl-Version: 8.6
Tcl-Ticket: 999162
-----
# Abstract
Programmers often need to create temporary files. This TIP proposes adding the
new subcommand **tempfile** to the **file** command, simplifying
programmer effort in creating a unique temporary file name.
# Rationale
Creating temporary files is a primitive and common enough operation that the
ANSI C standard defines several subroutines to assist programmers. The
_tmpnam\(\)_ function generates a string that is a valid file name that is not
the name of an existing file. The _tmpfile\(\)_ function returns a valid file
pointer to a temporary file that is removed automatically when it is closed or
at program termination. Both functions are commonly used by programmers,
without worrying about the optimal location for temporary files or schemes
required to create unique file names.
Tcl has not supported temporary files, so programmers have had to write code
to generate unique file names and choose locations like _/tmp_ or
_C:\\temp_. The _fileutil_ module of tcllib provides a relatively
sophisticated implementation of _tempfile_, but extension writers have not
been able to rely on this code. Modules in Iwidgets, the Tcl plugin, tcllib,
and TclX all appear to have this sort of code, and not all are correct.
This TIP proposes to extend the **file** command with one subcommand to
generate temporary files and file names. By extending the core command,
application programmers and extension writers will be able to depend on this
functionality for their code, without resorting to copying or depending on
tcllib.
# Specification
This TIP proposes an extension to the **file** command, implementing the
functionality of the POSIX standard mkstemp\(\) function. The new subcommand
will be called **tempfile**. It will return an open file channel, and
\(optionally\) the name of the temporary file.
# Synopsis
> **file tempfile** ?_namevar_? ?_template_?
Opens a unique temporary file and returns an open file channel. If _namevar_
is specified, then the command will set the variable to the name of the
temporary file. If the _template_ string is specified, it will be decomposed
into \(up to\) three parts: the path, the root, and the extension. Any part may
be absent. If the path or root are absent, system-dependent defaults will be
used. If the extension is absent, no extension will be used. The temporary
name will then be formed from the path, the root, some unique string, and the
extension \(if defined\).
The returned channel will have been opened in read-write mode \(but not append
mode\) using the defaults for EOL translation, encoding, etc.
# Reference Implementation
Available as Tcl Patch \#999162 on SourceForge
<https://sourceforge.net/support/tracker.php?aid=999162> . _\(That
patch is for an old version of the TIP.\)_
# Copyright
This document has been placed in the public domain.