xref: /netbsd-src/usr.bin/mktemp/mktemp.1 (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1.\" $NetBSD: mktemp.1,v 1.19 2009/08/15 20:44:56 wiz 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. 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.\" $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.5 1999/08/28 01:04:13 peter Exp $
33.\"
34.Dd August 15, 2009
35.Dt MKTEMP 1
36.Os
37.Sh NAME
38.Nm mktemp
39.Nd make temporary file name (unique)
40.Sh SYNOPSIS
41.Nm mktemp
42.Op Fl dqu
43.Op Fl p Ar tmpdir
44.Bro
45.Fl t Ar prefix
46.No |
47.Ar template ...
48.Brc
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 and suitable for use by the application.
55The template may be any file name with some number of
56.Ql X Ns s
57appended to it, for example
58.Pa /tmp/temp.XXXX .
59The trailing
60.Ql X Ns s
61are replaced with the current process number and/or a
62unique letter combination.
63The number of unique file names
64.Nm
65can return depends on the number of
66.Ql X Ns s
67provided; six
68.Ql X Ns s
69will result in
70.Nm
71testing roughly 26 ** 6 combinations.
72.Pp
73If
74.Nm
75can successfully generate a unique file name, the file
76is created with mode 0600 (unless the
77.Fl u
78flag is given) and the filename is printed to standard output.
79.Pp
80If the
81.Fl t Ar prefix
82option is given,
83.Nm
84will generate a template string based on the
85.Ar prefix
86and the
87.Ev TMPDIR
88environment variable, if set.
89The default location if
90.Ev TMPDIR
91is not set is
92.Pa /tmp .
93The default location of the temporary directory can be overridden with the
94.Fl p Ar tmpdir
95option.
96The template string created will consist of the
97.Ar prefix
98followed by a
99.So . Sc
100and an eight character unique letter combination.
101.Ql X Ns s
102in the
103.Ar prefix
104string will be treated as literal.
105If an additional
106.Ar template
107argument is passed, a second file will be created.
108Care should be taken to ensure that it is appropriate to use an
109environment variable potentially supplied by the user.
110.Pp
111Any number of temporary files may be created in a single invocation
112using multiple
113.Ar template
114arguments, also a single one based on the internal template with the
115.Fl t
116option value as filename prefix.
117.Pp
118At least one
119.Ar template
120argument or the
121.Fl t
122option must be present.
123.Pp
124.Nm
125is provided to allow shell scripts to safely use temporary files.
126Traditionally, many shell scripts take the name of the program with
127the pid as a suffix and use that as a temporary file name.
128This kind of naming scheme is predictable and the race condition
129it creates is easy for an attacker to win.
130A safer, though still inferior, approach
131is to make a temporary directory using the same naming scheme.
132While this does allow one to guarantee that a temporary file will
133not be subverted, it still allows a simple denial of service attack.
134For these reasons it is suggested that
135.Nm
136be used instead.
137.Sh OPTIONS
138The available options are as follows:
139.Bl -tag -width indent
140.It Fl d
141Make a directory instead of a file.
142.It Fl q
143Fail silently if an error occurs.
144This is useful if
145a script does not want error output to go to standard error.
146.It Fl t Ar prefix
147Generate a template (using the supplied
148.Ar prefix
149and
150.Ev TMPDIR
151if set) to create a filename template.
152If
153.Fl t Ar prefix
154and
155.Ar template
156are both given,
157.Ar prefix
158will not apply to
159.Ar template .
160.It Fl u
161Operate in
162.Dq unsafe
163mode.
164The temp file will be unlinked before
165.Nm
166exits.
167This is slightly better than
168.Xr mktemp 3
169but still introduces a race condition.
170Use of this option is not encouraged.
171.El
172.Sh EXIT STATUS
173The
174.Nm
175utility exits with a value of 0 on success, and 1 on any failure.
176.Sh EXAMPLES
177The following
178.Xr sh 1
179fragment illustrates a simple use of
180.Nm
181where the script should quit if it cannot get a safe
182temporary file.
183.Bd -literal -offset indent
184TMPFILE=`mktemp /tmp/${0##*/}.XXXXXX` || exit 1
185echo "program output" \*[Gt]\*[Gt] $TMPFILE
186.Ed
187.Pp
188To allow the use of $TMPDIR:
189.Bd -literal -offset indent
190TMPFILE=`mktemp -t ${0##*/}` || exit 1
191echo "program output" \*[Gt]\*[Gt] $TMPFILE
192.Ed
193.Pp
194In this case, we want the script to catch the error itself.
195.Bd -literal -offset indent
196TMPFILE=`mktemp -q /tmp/${0##*/}.XXXXXX`
197if [ $? -ne 0 ]; then
198	echo "$0: Can't create temp file, exiting..."
199	exit 1
200fi
201.Ed
202.Sh SEE ALSO
203.Xr mkdtemp 3 ,
204.Xr mkstemp 3 ,
205.Xr mktemp 3 ,
206.Xr environ 7
207.Sh HISTORY
208The
209.Nm
210utility appeared in
211.Nx 1.5 .
212It has been imported from
213.Fx ,
214the idea and the manual page were taken from
215.Ox .
216