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