\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#431: Add 'tempdir' Subcommand to 'file'}
\date{September 10, 2014}
\author{Kevin Pasko}
\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 \#431}&\textbf{Add 'tempdir' Subcommand to 'file'}\\\hline
Author&

Kevin Pasko \tipmail{kevin.a.pasko}{gmail.com}
\\
Created&Wednesday, \(\text{10}^{\text{th}}\) September 2014\\
Type&Project\\
Tcl Version&\(8.6.4\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.2 $\\
Keywords&Tcl, directory, file\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes adding a new \textbf{tempdir} subcommand to the \textbf{file} command, simplifying the effort required in creating uniquely named temporary directories at the scripting level.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

Due to the non-atomic nature of the \textbf{file mkdir} command it is currently impossible to create uniquely named temporary directories at the script level without the possibility of race conditions.

\section{Specification}

The \textbf{file tempdir} command shall implement the functionality of the POSIX standard mkdtemp() function. With no arguments \textbf{file tempdir} shall create a uniquely named temporary directory in the native operating system's temporary directory, with naming convention ``\textbf{tcl\_}\textit{XXXXXX}'' where each \textit{X} is a randomly selected character (following the \textbf{file tempfile} naming convention). Successful completion of \textbf{file tempdir} shall return the absolute path of the created directory, otherwise an error shall be thrown.

\textbf{file tempdir} shall have an optional argument, \textit{template}, to modify the created directory's path and name. The \textit{template} shall be decomposed into (up to) two parts: the directory's path and rootname. If either part is absent, relevant defaults (e.g., according to the native operating system) shall be used. The entire temporary name shall then be formed from the path, the root, and a generated unique string of (typically) six characters.

The command syntax should be defined as:

\begin{quote}
\textbf{file tempdir} ?\textit{template}?
\end{quote}

\section{Considerations}
\begin{itemize}
\item{}
The subcommand \textbf{tempdir} could be a candidate, later, for returning the native file system's temporary directory. Naming the subcommand something else such as \textbf{mktempdir} is another option, though strays from the \textbf{file tempfile} naming convention.
\item{}
For future extensibility the \textbf{template} argument to \textbf{file tempdir} (since it is optional) could be specified in the key / value format, \textbf{-template}, changing the command syntax to:

\textbf{file tempdir} ?\textit{options...}?
\end{itemize}

\section{Reference Implementation}

An example of temporary directory creation has already been developed into the Tcl core, at the C level, within the platform specific layers of the \textbf{load} command. The principal work remaining is to expose this via a Tcl command.

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

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