1.\" $NetBSD: mktemp.1,v 1.8 2002/09/30 11:09:06 grant Exp $ 2.\" From: $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.5 1999/08/28 01:04:13 peter Exp $ 3.\" From: $OpenBSD: mktemp.1,v 1.8 1998/03/19 06:13:37 millert Exp $ 4.\" 5.\" Copyright (c) 1989, 1991, 1993 6.\" The Regents of the University of California. All rights reserved. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. All advertising materials mentioning features or use of this software 17.\" must display the following acknowledgement: 18.\" This product includes software developed by the University of 19.\" California, Berkeley and its contributors. 20.\" 4. Neither the name of the University nor the names of its contributors 21.\" may be used to endorse or promote products derived from this software 22.\" without specific prior written permission. 23.\" 24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34.\" SUCH DAMAGE. 35.\" 36.\" $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.5 1999/08/28 01:04:13 peter Exp $ 37.\" 38.Dd November 20, 1996 39.Dt MKTEMP 1 40.Os 41.Sh NAME 42.Nm mktemp 43.Nd make temporary file name (unique) 44.Sh SYNOPSIS 45.Nm mktemp 46.Op Fl d 47.Op Fl q 48.Op Fl t Ar prefix 49.Op Fl u 50.Op Ar template ... 51.Sh DESCRIPTION 52The 53.Nm 54utility takes each of the given file name templates and overwrites a 55portion of it to create a file name. 56This file name is unique 57and suitable for use by the application. 58The template may be 59any file name with some number of 60.Ql X Ns s 61appended 62to it, for example 63.Pa /tmp/temp.XXXX . 64The trailing 65.Ql X Ns s 66are replaced with the current process number and/or a 67unique letter combination. 68The number of unique file names 69.Nm 70can return depends on the number of 71.Ql X Ns s 72provided; six 73.Ql X Ns s 74will 75result in 76.Nm 77testing roughly 26 ** 6 combinations. 78.Pp 79If 80.Nm 81can successfully generate a unique file name, the file 82is created with mode 0600 (unless the 83.Fl u 84flag is given) and the filename is printed 85to standard output. 86.Pp 87If the 88.Fl t Ar prefix 89option is given, 90.Nm 91will generate an template string based on the 92.Ar prefix 93and the 94.Ev TMPDIR 95environment variable if set. 96The default location if 97.Ev TMPDIR 98is not set is 99.Pa /tmp . 100Care should 101be taken to ensure that it is appropriate to use an environment variable 102potentially supplied by the user. 103.Pp 104Any number of temporary files may be created in a single invocation, 105including one based on the internal template resulting from the 106.Fl t 107flag. 108.Pp 109.Nm 110is provided to allow shell scripts to safely use temporary files. 111Traditionally, many shell scripts take the name of the program with 112the pid as a suffix and use that as a temporary file name. 113This 114kind of naming scheme is predictable and the race condition it creates 115is easy for an attacker to win. 116A safer, though still inferior, approach 117is to make a temporary directory using the same naming scheme. 118While 119this does allow one to guarantee that a temporary file will not be 120subverted, it still allows a simple denial of service attack. 121For these 122reasons it is suggested that 123.Nm 124be used instead. 125.Sh OPTIONS 126The available options are as follows: 127.Bl -tag -width indent 128.It Fl d 129Make a directory instead of a file. 130.It Fl q 131Fail silently if an error occurs. 132This is useful if 133a script does not want error output to go to standard error. 134.It Fl t Ar prefix 135Generate a template (using the supplied 136.Ar prefix 137and 138.Ev TMPDIR 139if set) to create a filename template. 140.It Fl u 141Operate in 142.Dq unsafe 143mode. 144The temp file will be unlinked before 145.Nm 146exits. 147This is slightly better than 148.Xr mktemp 3 149but still introduces a race condition. 150Use of this 151option is not encouraged. 152.El 153.Sh EXIT STATUS 154The 155.Nm 156utility 157exits with a value of 0 on success, and 1 on any failure. 158.Sh EXAMPLES 159The following 160.Xr sh 1 161fragment illustrates a simple use of 162.Nm 163where the script should quit if it cannot get a safe 164temporary file. 165.Bd -literal -offset indent 166TMPFILE=`mktemp /tmp/$0.XXXXXX` || exit 1 167echo "program output" \*[Gt]\*[Gt] $TMPFILE 168.Ed 169.Pp 170To allow the use of $TMPDIR: 171.Bd -literal -offset indent 172TMPFILE=`mktemp -t $0` || exit 1 173echo "program output" \*[Gt]\*[Gt] $TMPFILE 174.Ed 175.Pp 176In this case, we want the script to catch the error itself. 177.Bd -literal -offset indent 178TMPFILE=`mktemp -q /tmp/$0.XXXXXX` 179if [ $? -ne 0 ]; then 180 echo "$0: Can't create temp file, exiting..." 181 exit 1 182fi 183.Ed 184.Sh SEE ALSO 185.Xr mkdtemp 3 , 186.Xr mkstemp 3 , 187.Xr mktemp 3 , 188.Xr environ 7 189.Sh HISTORY 190The 191.Nm 192utility appeared in 193.Nx 1.5 . 194It has been imported from 195.Bx Free , 196the idea and the manual page were taken from 197.Bx Open . 198