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