1 .\" $Id: mktemp.1,v 1.1 2003/12/20 03:31:53 bbbush Exp $
3 .\" Copyright (c) 1996, 2000, 2001 Todd C. Miller <Todd.Miller@courtesan.com>
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. The name of the author may not be used to endorse or promote products
15 .\" derived from this software without specific prior written permission.
17 .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19 .\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20 .\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 .\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 .\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 .\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 .\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 .TH MKTEMP 1 "30 September 2001"
30 \fBmktemp\fP \- make temporary filename (unique)
32 \fBmktemp\fP [\fB\-V\fP] | [\fB\-dqtu\fP] [\fB\-p\fP \fIdirectory\fP] [\fItemplate\fP]
36 utility takes the given filename
38 and overwrites a portion of it to create a unique filename.
41 may be any filename with some number of
42 `Xs' appended to it, for example
43 .I /tmp/tfile.XXXXXXXXXX.
46 is specified a default of
50 flag is implied (see below).
52 The trailing `Xs' are replaced with a combination
53 of the current process number and random letters.
54 The name chosen depends both on the number of `Xs' in the
56 and the number of collisions with pre\-existing files.
57 The number of unique filenames
59 can return depends on the number of
60 `Xs' provided; ten `Xs' will result in
62 testing roughly 26 ** 10 combinations.
66 can successfully generate a unique filename, the file (or directory)
67 is created with file permissions such that it is only readable and writable
68 by its owner (unless the
70 flag is given) and the filename is printed to standard output.
73 is provided to allow shell scripts to safely use temporary
74 files. Traditionally, many shell scripts take the name of the program with
75 the PID as a suffix and use that as a temporary filename.
76 This kind of naming scheme is predictable and the race condition it creates
77 is easy for an attacker to win.
78 A safer, though still inferior approach
79 is to make a temporary directory using the same naming scheme.
80 While this does allow one to guarantee that a temporary file will not be
81 subverted, it still allows a simple denial of service attack.
82 For these reasons it is suggested that
86 The options are as follows:
89 Print the version and exit.
92 Make a directory instead of a file.
97 as a prefix when generating the temporary filename.
100 will be overridden by the user's
102 environment variable if it is set.
103 This option implies the
108 Fail silently if an error occurs.
110 a script does not want error output to go to standard error.
113 Generate a path rooted in a temporary directory.
114 This directory is chosen as follows:
119 environment variable is set, the directory contained therein is used.
123 flag was given the specified directory is used.
125 If none of the above apply,
132 (if specified) should be a directory component (as opposed to a full path)
133 and thus should not contain any forward slashes.
136 Operate in ``unsafe'' mode.
137 The temp file will be unlinked before
139 exits. This is slightly better than mktemp(3)
140 but still introduces a race condition. Use of this
141 option is not encouraged.
146 exits with a value of 0 on success or 1 on failure.
149 fragment illustrates a simple use of
151 where the script should quit if it cannot get a safe
156 TMPFILE=`mktemp /tmp/example.XXXXXXXXXX` || exit 1
157 echo "program output" >> $TMPFILE
161 The same fragment with support for a user's
163 environment variable can be written as follows.
167 TMPFILE=`mktemp \-t example.XXXXXXXXXX` || exit 1
168 echo "program output" >> $TMPFILE
172 This can be further simplified if we don't care about the actual name of
173 the temporary file. In this case the
179 TMPFILE=`mktemp` || exit 1
180 echo "program output" >> $TMPFILE
184 In some cases, it may be desirable to use a default temporary directory
187 In this example the temporary file will be created in
191 environment variable specifies otherwise.
195 TMPFILE=`mktemp \-p /extra/tmp example.XXXXXXXXXX` || exit 1
196 echo "program output" >> $TMPFILE
200 In some cases, we want the script to catch the error.
201 For instance, if we attempt to create two temporary files and
202 the second one fails we need to remove the first before exiting.
206 TMP1=`mktemp \-t example.1.XXXXXXXXXX` || exit 1
207 TMP2=`mktemp \-t example.2.XXXXXXXXXX`
208 if [ $? \-ne 0 ]; then
215 Or perhaps you don't want to exit if
217 is unable to create the file.
218 In this case you can protect that part of the script thusly.
222 TMPFILE=`mktemp \-t example.XXXXXXXXXX` && {
223 # Safe to use $TMPFILE in this block
233 directory in which to place the temporary file when in
243 utility appeared in OpenBSD 2.1.