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