1.\" $NetBSD: mktemp.1,v 1.7 2002/02/08 01:36:29 ross 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. This file name is unique 56and suitable for use by the application. The template may be 57any file name with some number of 58.Ql X Ns s 59appended 60to it, for example 61.Pa /tmp/temp.XXXX . 62The trailing 63.Ql X Ns s 64are replaced with the current process number and/or a 65unique letter combination. 66The number of unique file names 67.Nm 68can return depends on the number of 69.Ql X Ns s 70provided; six 71.Ql X Ns s 72will 73result in 74.Nm 75testing roughly 26 ** 6 combinations. 76.Pp 77If 78.Nm 79can successfully generate a unique file name, the file 80is created with mode 0600 (unless the 81.Fl u 82flag is given) and the filename is printed 83to standard output. 84.Pp 85If the 86.Fl t Ar prefix 87option is given, 88.Nm 89will generate an template string based on the 90.Ar prefix 91and the 92.Ev TMPDIR 93environment variable if set. The default location if 94.Ev TMPDIR 95is not set is 96.Pa /tmp . 97Care should 98be taken to ensure that it is appropriate to use an environment variable 99potentially supplied by the user. 100.Pp 101Any number of temporary files may be created in a single invocation, 102including one based on the internal template resulting from the 103.Fl t 104flag. 105.Pp 106.Nm 107is provided to allow shell scripts to safely use temporary files. 108Traditionally, many shell scripts take the name of the program with 109the pid as a suffix and use that as a temporary file name. This 110kind of naming scheme is predictable and the race condition it creates 111is easy for an attacker to win. A safer, though still inferior, approach 112is to make a temporary directory using the same naming scheme. While 113this does allow one to guarantee that a temporary file will not be 114subverted, it still allows a simple denial of service attack. For these 115reasons it is suggested that 116.Nm 117be used instead. 118.Sh OPTIONS 119The available options are as follows: 120.Bl -tag -width indent 121.It Fl d 122Make a directory instead of a file. 123.It Fl q 124Fail silently if an error occurs. This is useful if 125a script does not want error output to go to standard error. 126.It Fl t Ar prefix 127Generate a template (using the supplied 128.Ar prefix 129and 130.Ev TMPDIR 131if set) to create a filename template. 132.It Fl u 133Operate in 134.Dq unsafe 135mode. The temp file will be unlinked before 136.Nm 137exits. This is slightly better than 138.Xr mktemp 3 139but still introduces a race condition. Use of this 140option is not encouraged. 141.El 142.Sh EXIT STATUS 143The 144.Nm 145utility 146exits with a value of 0 on success, and 1 on any failure. 147.Sh EXAMPLES 148The following 149.Xr sh 1 150fragment illustrates a simple use of 151.Nm 152where the script should quit if it cannot get a safe 153temporary file. 154.Bd -literal -offset indent 155TMPFILE=`mktemp /tmp/$0.XXXXXX` || exit 1 156echo "program output" \*[Gt]\*[Gt] $TMPFILE 157.Ed 158.Pp 159To allow the use of $TMPDIR: 160.Bd -literal -offset indent 161TMPFILE=`mktemp -t $0` || exit 1 162echo "program output" \*[Gt]\*[Gt] $TMPFILE 163.Ed 164.Pp 165In this case, we want the script to catch the error itself. 166.Bd -literal -offset indent 167TMPFILE=`mktemp -q /tmp/$0.XXXXXX` 168if [ $? -ne 0 ]; then 169 echo "$0: Can't create temp file, exiting..." 170 exit 1 171fi 172.Ed 173.Sh SEE ALSO 174.Xr mkdtemp 3 , 175.Xr mkstemp 3 , 176.Xr mktemp 3 , 177.Xr environ 7 178.Sh HISTORY 179The 180.Nm 181utility appeared in 182.Nx 1.5 . 183It has been imported from 184.Bx Free , 185the idea and the manual page were taken from 186.Bx Open . 187