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