<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://www.tcl.tk/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Tue Jun 18 05:19:54 GMT 2013 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='210'>
<header><title>Add &apos;tempfile&apos; Subcommand to &apos;file&apos;</title><author address="mailto:techentin.robert@mayo.edu">Bob Techentin</author><author address="mailto:donal.k.fellows@manchester.ac.uk">Donal K. Fellows</author><status type='project' state='final' tclversion="8.6" vote='after'>$Revision: 1.11 $</status><history></history><created day='19' month='jul' year='2004' /><keyword>Tcl filename</keyword></header>
<abstract>Programmers often need to create temporary files. This TIP proposes adding the new subcommand <emph style="bold">tempfile</emph> to the <emph style="bold">file</emph> command, simplifying programmer effort in creating a unique temporary file name.</abstract>
<body><section title="Rationale">
<para>Creating temporary files is a primitive and common enough operation that the ANSI C standard defines several subroutines to assist programmers. The <emph style="italic">tmpnam()</emph> function generates a string that is a valid file name that is not the name of an existing file. The <emph style="italic">tmpfile()</emph> 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.</para>
<para>Tcl has not supported temporary files, so programmers have had to write code to generate unique file names and choose locations like <emph style="italic">/tmp</emph> or <emph style="italic">C:\temp</emph>. The <emph style="italic">fileutil</emph> module of tcllib provides a relatively sophisticated implementation of <emph style="italic">tempfile</emph>, 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.</para>
<para>This TIP proposes to extend the <emph style="bold">file</emph> 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.</para>
</section>
<section title="Specification">
<para>This TIP proposes an extension to the <emph style="bold">file</emph> command, implementing the functionality of the POSIX standard mkstemp() function. The new subcommand will be called <emph style="bold">tempfile</emph>. It will return an open file channel, and (optionally) the name of the temporary file.</para>
</section>
<section title="Synopsis">
<quote><emph style="bold">file tempfile</emph> ?<emph style="italic">namevar</emph>? ?<emph style="italic">template</emph>?</quote>
<para>Opens a unique temporary file and returns an open file channel. If <emph style="italic">namevar</emph> is specified, then the command will set the variable to the name of the temporary file. If the <emph style="italic">template</emph> 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).</para>
<para>The returned channel will have been opened in read-write mode (but not append mode) using the defaults for EOL translation, encoding, etc.</para>
</section>
<section title="Reference Implementation">
<para>Available as Tcl Patch #999162 on SourceForge [<url ref="https://sourceforge.net/support/tracker.php?aid=999162"/>]. <emph style="italic">(That patch is for an old version of the TIP.)</emph></para>
</section>
<section title="Copyright">
<para>This document has been placed in the public domain.</para>
</section>
</body></TIP>
