xref: /freebsd/usr.bin/mktemp/mktemp.1 (revision 380a989b3223d455375b4fae70fd0b9bdd43bafb)
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.Dd November, 20, 1996
36.Dt MKTEMP 1
37.Os
38.Sh NAME
39.Nm mktemp
40.Nd make temporary file name (unique)
41.Sh SYNOPSIS
42.Nm mktemp
43.Op Fl d
44.Op Fl q
45.Op Fl t Ar prefix
46.Op Fl u
47.Op Ar template ...
48.Sh DESCRIPTION
49The
50.Nm
51utility takes each of the given file name templates and overwrites a
52portion of it to create a file name.  This file name is unique
53and suitable for use by the application.  The template may be
54any file name with some number of
55.Ql X Ns s
56appended
57to 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
70result in
71.Nm
72testing roughly 26 ** 6 combinations.
73.Pp
74If
75.Nm
76can successfully generate a unique file name, the file
77is created with mode 0600 (unless the
78.Fl u
79flag is given) and the filename is printed
80to standard output.
81.Pp
82If the
83.Fl t Ar prefix
84option is given,
85.Nm
86will generate an template string based on the
87.Ar prefix
88and the
89.Ev TMPDIR
90environment variable if set. The default location if
91.Ev TMPDIR
92is not set is
93.Pa /tmp .
94Care should
95be taken to ensure that it is appropriate to use an environment variable
96potentially supplied by the user.
97.Pp
98Any number of temporary files may be created in a single invocation,
99including one based on the internal template resulting from the
100.Fl t
101flag.
102.Pp
103.Nm Mktemp
104is provided to allow shell scripts to safely use temporary files.
105Traditionally, many shell scripts take the name of the program with
106the pid as a suffix and use that as a temporary file name.  This
107kind of naming scheme is predictable and the race condition it creates
108is easy for an attacker to win.  A safer, though still inferior, approach
109is to make a temporary directory using the same naming scheme.  While
110this does allow one to guarantee that a temporary file will not be
111subverted, it still allows a simple denial of service attack.  For these
112reasons it is suggested that
113.Nm
114be used instead.
115.Sh OPTIONS
116The available options are as follows:
117.Bl -tag -width indent
118.It Fl d
119Make a directory instead of a file.
120.It Fl q
121Fail silently if an error occurs.  This is useful if
122a script does not want error output to go to standard error.
123.It Fl t Ar prefix
124Generate a template (using the supplied
125.Ar prefix
126and
127.Ev TMPDIR
128if set) to create a filename template.
129.It Fl u
130Operate in
131.Dq unsafe
132mode.  The temp file will be unlinked before
133.Nm
134exits.  This is slightly better than
135.Xr mktemp 3
136but still introduces a race condition.  Use of this
137option is not encouraged.
138.El
139.Sh RETURN VALUES
140The
141.Nm
142utility
143exits with a value of 0 on success, and 1 on any failure.
144.Sh EXAMPLES
145The following
146.Xr sh 1
147fragment illustrates a simple use of
148.Nm
149where the script should quit if it cannot get a safe
150temporary file.
151.Bd -literal -offset indent
152TMPFILE=`mktemp /tmp/$0.XXXXXX` || exit 1
153echo "program output" >> $TMPFILE
154.Ed
155.Pp
156To allow the use of $TMPDIR:
157.Bd -literal -offset indent
158TMPFILE=`mktemp -t $0` || exit 1
159echo "program output" >> $TMPFILE
160.Ed
161.Pp
162In this case, we want the script to catch the error itself.
163.Bd -literal -offset indent
164TMPFILE=`mktemp -q /tmp/$0.XXXXXX`
165if [ $? -ne 0 ]; then
166	echo "$0: Can't create temp file, exiting..."
167	exit 1
168fi
169.Ed
170.Sh SEE ALSO
171.Xr mkdtemp 3 ,
172.Xr mkstemp 3 ,
173.Xr mktemp 3 ,
174.Xr environ 7
175.Sh HISTORY
176A
177.Nm
178utility appeared in
179.Ox 2.1 .
180This implementation has been written independently based on the man page.
181This man page is taken from
182.Bx Open .
183.\" Our stupid .Ox macro won't allow me to use .Ox alone.
184