\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#210: Add 'tempfile' Subcommand to 'file'}
\date{July 19, 2004}
\author{Bob Techentin, Donal K. Fellows}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#210}&\textbf{Add 'tempfile' Subcommand to 'file'}\\\hline
Author&

Bob Techentin \tipmail{techentin.robert}{mayo.edu}
\par
Donal K. Fellows \tipmail{donal.k.fellows}{manchester.ac.uk}
\\
Created&Monday, \(\text{19}^{\text{th}}\) July 2004\\
Type&Project\\
Tcl Version&\(8.6\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.11 $\\
Keywords&Tcl, filename\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

Programmers often need to create temporary files. This TIP proposes adding the new subcommand \textbf{tempfile} to the \textbf{file} command, simplifying programmer effort in creating a unique temporary file name.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

Creating temporary files is a primitive and common enough operation that the ANSI C standard defines several subroutines to assist programmers. The \textit{tmpnam()} function generates a string that is a valid file name that is not the name of an existing file. The \textit{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 \textit{/tmp} or \textit{C:\(\backslash\)temp}. The \textit{fileutil} module of tcllib provides a relatively sophisticated implementation of \textit{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 \textbf{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.

\section{Specification}

This TIP proposes an extension to the \textbf{file} command, implementing the functionality of the POSIX standard mkstemp() function. The new subcommand will be called \textbf{tempfile}. It will return an open file channel, and (optionally) the name of the temporary file.

\section{Synopsis}

\begin{quote}
\textbf{file tempfile} ?\textit{namevar}? ?\textit{template}?
\end{quote}

Opens a unique temporary file and returns an open file channel. If \textit{namevar} is specified, then the command will set the variable to the name of the temporary file. If the \textit{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.

\section{Reference Implementation}

Available as Tcl Patch \#999162 on SourceForge [\url{https://sourceforge.net/support/tracker.php?aid=999162}]. \textit{(That patch is for an old version of the TIP.)}

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
