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