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