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