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