2009-08-16 00:44:56 +04:00
|
|
|
.\" $NetBSD: mktemp.1,v 1.19 2009/08/15 20:44:56 wiz Exp $
|
1999-09-05 00:36:15 +04:00
|
|
|
.\" From: $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.5 1999/08/28 01:04:13 peter Exp $
|
1999-09-03 00:46:17 +04:00
|
|
|
.\" From: $OpenBSD: mktemp.1,v 1.8 1998/03/19 06:13:37 millert Exp $
|
|
|
|
.\"
|
|
|
|
.\" Copyright (c) 1989, 1991, 1993
|
|
|
|
.\" The Regents of the University of California. All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
.\" 3. Neither the name of the University nor the names of its contributors
|
1999-09-03 00:46:17 +04:00
|
|
|
.\" may be used to endorse or promote products derived from this software
|
|
|
|
.\" without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
|
|
|
.\" $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.5 1999/08/28 01:04:13 peter Exp $
|
|
|
|
.\"
|
2009-08-16 00:02:28 +04:00
|
|
|
.Dd August 15, 2009
|
1999-09-03 00:46:17 +04:00
|
|
|
.Dt MKTEMP 1
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm mktemp
|
|
|
|
.Nd make temporary file name (unique)
|
|
|
|
.Sh SYNOPSIS
|
|
|
|
.Nm mktemp
|
2006-09-19 23:00:17 +04:00
|
|
|
.Op Fl dqu
|
2009-08-16 00:02:28 +04:00
|
|
|
.Op Fl p Ar tmpdir
|
2006-09-19 18:22:51 +04:00
|
|
|
.Bro
|
2006-09-19 23:01:44 +04:00
|
|
|
.Fl t Ar prefix
|
2006-09-19 18:22:51 +04:00
|
|
|
.No |
|
|
|
|
.Ar template ...
|
|
|
|
.Brc
|
1999-09-03 00:46:17 +04:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
The
|
|
|
|
.Nm
|
|
|
|
utility takes each of the given file name templates and overwrites a
|
2002-09-30 15:08:56 +04:00
|
|
|
portion of it to create a file name.
|
2006-09-19 23:00:17 +04:00
|
|
|
This file name is unique and suitable for use by the application.
|
|
|
|
The template may be any file name with some number of
|
1999-09-03 00:46:17 +04:00
|
|
|
.Ql X Ns s
|
2006-09-19 23:00:17 +04:00
|
|
|
appended to it, for example
|
1999-09-03 00:46:17 +04:00
|
|
|
.Pa /tmp/temp.XXXX .
|
|
|
|
The trailing
|
|
|
|
.Ql X Ns s
|
|
|
|
are replaced with the current process number and/or a
|
|
|
|
unique letter combination.
|
|
|
|
The number of unique file names
|
|
|
|
.Nm
|
|
|
|
can return depends on the number of
|
|
|
|
.Ql X Ns s
|
|
|
|
provided; six
|
|
|
|
.Ql X Ns s
|
2006-09-19 23:00:17 +04:00
|
|
|
will result in
|
1999-09-03 00:46:17 +04:00
|
|
|
.Nm
|
|
|
|
testing roughly 26 ** 6 combinations.
|
|
|
|
.Pp
|
|
|
|
If
|
|
|
|
.Nm
|
|
|
|
can successfully generate a unique file name, the file
|
|
|
|
is created with mode 0600 (unless the
|
|
|
|
.Fl u
|
2006-09-19 23:00:17 +04:00
|
|
|
flag is given) and the filename is printed to standard output.
|
1999-09-03 00:46:17 +04:00
|
|
|
.Pp
|
|
|
|
If the
|
|
|
|
.Fl t Ar prefix
|
|
|
|
option is given,
|
|
|
|
.Nm
|
2006-05-20 00:03:30 +04:00
|
|
|
will generate a template string based on the
|
1999-09-03 00:46:17 +04:00
|
|
|
.Ar prefix
|
|
|
|
and the
|
|
|
|
.Ev TMPDIR
|
2006-09-19 23:00:17 +04:00
|
|
|
environment variable, if set.
|
2002-09-30 15:08:56 +04:00
|
|
|
The default location if
|
1999-09-03 00:46:17 +04:00
|
|
|
.Ev TMPDIR
|
|
|
|
is not set is
|
|
|
|
.Pa /tmp .
|
2009-08-16 00:44:56 +04:00
|
|
|
The default location of the temporary directory can be overridden with the
|
2009-08-16 00:02:28 +04:00
|
|
|
.Fl p Ar tmpdir
|
|
|
|
option.
|
2006-01-13 00:39:45 +03:00
|
|
|
The template string created will consist of the
|
|
|
|
.Ar prefix
|
|
|
|
followed by a
|
|
|
|
.So . Sc
|
|
|
|
and an eight character unique letter combination.
|
2006-01-13 00:54:06 +03:00
|
|
|
.Ql X Ns s
|
2006-01-13 00:39:45 +03:00
|
|
|
in the
|
|
|
|
.Ar prefix
|
2006-01-13 00:54:06 +03:00
|
|
|
string will be treated as literal.
|
|
|
|
If an additional
|
2006-01-13 00:39:45 +03:00
|
|
|
.Ar template
|
|
|
|
argument is passed, a second file will be created.
|
2006-09-19 23:00:17 +04:00
|
|
|
Care should be taken to ensure that it is appropriate to use an
|
|
|
|
environment variable potentially supplied by the user.
|
1999-09-03 00:46:17 +04:00
|
|
|
.Pp
|
2006-09-19 18:22:51 +04:00
|
|
|
Any number of temporary files may be created in a single invocation
|
|
|
|
using multiple
|
|
|
|
.Ar template
|
|
|
|
arguments, also a single one based on the internal template with the
|
|
|
|
.Fl t
|
|
|
|
option value as filename prefix.
|
|
|
|
.Pp
|
|
|
|
At least one
|
|
|
|
.Ar template
|
|
|
|
argument or the
|
1999-09-03 00:46:17 +04:00
|
|
|
.Fl t
|
2006-09-19 18:22:51 +04:00
|
|
|
option must be present.
|
1999-09-03 00:46:17 +04:00
|
|
|
.Pp
|
2000-11-07 09:43:24 +03:00
|
|
|
.Nm
|
1999-09-03 00:46:17 +04:00
|
|
|
is provided to allow shell scripts to safely use temporary files.
|
|
|
|
Traditionally, many shell scripts take the name of the program with
|
2002-09-30 15:08:56 +04:00
|
|
|
the pid as a suffix and use that as a temporary file name.
|
2006-09-19 23:00:17 +04:00
|
|
|
This kind of naming scheme is predictable and the race condition
|
|
|
|
it creates is easy for an attacker to win.
|
2002-09-30 15:08:56 +04:00
|
|
|
A safer, though still inferior, approach
|
|
|
|
is to make a temporary directory using the same naming scheme.
|
2006-09-19 23:00:17 +04:00
|
|
|
While this does allow one to guarantee that a temporary file will
|
|
|
|
not be subverted, it still allows a simple denial of service attack.
|
|
|
|
For these reasons it is suggested that
|
1999-09-03 00:46:17 +04:00
|
|
|
.Nm
|
|
|
|
be used instead.
|
|
|
|
.Sh OPTIONS
|
|
|
|
The available options are as follows:
|
|
|
|
.Bl -tag -width indent
|
|
|
|
.It Fl d
|
|
|
|
Make a directory instead of a file.
|
|
|
|
.It Fl q
|
2002-09-30 15:08:56 +04:00
|
|
|
Fail silently if an error occurs.
|
|
|
|
This is useful if
|
1999-09-03 00:46:17 +04:00
|
|
|
a script does not want error output to go to standard error.
|
|
|
|
.It Fl t Ar prefix
|
|
|
|
Generate a template (using the supplied
|
|
|
|
.Ar prefix
|
|
|
|
and
|
|
|
|
.Ev TMPDIR
|
2006-09-19 23:00:17 +04:00
|
|
|
if set) to create a filename template.
|
|
|
|
If
|
2006-09-19 18:22:51 +04:00
|
|
|
.Fl t Ar prefix
|
|
|
|
and
|
|
|
|
.Ar template
|
|
|
|
are both given,
|
|
|
|
.Ar prefix
|
|
|
|
will not apply to
|
2006-09-19 23:00:17 +04:00
|
|
|
.Ar template .
|
1999-09-03 00:46:17 +04:00
|
|
|
.It Fl u
|
|
|
|
Operate in
|
|
|
|
.Dq unsafe
|
2002-09-30 15:08:56 +04:00
|
|
|
mode.
|
|
|
|
The temp file will be unlinked before
|
1999-09-03 00:46:17 +04:00
|
|
|
.Nm
|
2002-09-30 15:08:56 +04:00
|
|
|
exits.
|
|
|
|
This is slightly better than
|
1999-09-03 00:46:17 +04:00
|
|
|
.Xr mktemp 3
|
2002-09-30 15:08:56 +04:00
|
|
|
but still introduces a race condition.
|
2006-09-19 23:00:17 +04:00
|
|
|
Use of this option is not encouraged.
|
1999-09-03 00:46:17 +04:00
|
|
|
.El
|
2000-09-04 11:35:15 +04:00
|
|
|
.Sh EXIT STATUS
|
1999-09-03 00:46:17 +04:00
|
|
|
The
|
|
|
|
.Nm
|
2006-09-19 23:00:17 +04:00
|
|
|
utility exits with a value of 0 on success, and 1 on any failure.
|
1999-09-03 00:46:17 +04:00
|
|
|
.Sh EXAMPLES
|
|
|
|
The following
|
|
|
|
.Xr sh 1
|
|
|
|
fragment illustrates a simple use of
|
|
|
|
.Nm
|
|
|
|
where the script should quit if it cannot get a safe
|
|
|
|
temporary file.
|
|
|
|
.Bd -literal -offset indent
|
2004-04-02 14:44:22 +04:00
|
|
|
TMPFILE=`mktemp /tmp/${0##*/}.XXXXXX` || exit 1
|
2002-02-08 04:36:18 +03:00
|
|
|
echo "program output" \*[Gt]\*[Gt] $TMPFILE
|
1999-09-03 00:46:17 +04:00
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
To allow the use of $TMPDIR:
|
|
|
|
.Bd -literal -offset indent
|
2004-04-02 14:44:22 +04:00
|
|
|
TMPFILE=`mktemp -t ${0##*/}` || exit 1
|
2002-02-08 04:36:18 +03:00
|
|
|
echo "program output" \*[Gt]\*[Gt] $TMPFILE
|
1999-09-03 00:46:17 +04:00
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
In this case, we want the script to catch the error itself.
|
|
|
|
.Bd -literal -offset indent
|
2004-04-02 14:44:22 +04:00
|
|
|
TMPFILE=`mktemp -q /tmp/${0##*/}.XXXXXX`
|
1999-09-03 00:46:17 +04:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "$0: Can't create temp file, exiting..."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
.Ed
|
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr mkdtemp 3 ,
|
|
|
|
.Xr mkstemp 3 ,
|
|
|
|
.Xr mktemp 3 ,
|
|
|
|
.Xr environ 7
|
|
|
|
.Sh HISTORY
|
1999-09-05 00:36:15 +04:00
|
|
|
The
|
1999-09-03 00:46:17 +04:00
|
|
|
.Nm
|
|
|
|
utility appeared in
|
1999-09-05 00:36:15 +04:00
|
|
|
.Nx 1.5 .
|
|
|
|
It has been imported from
|
2006-01-13 00:39:45 +03:00
|
|
|
.Fx ,
|
1999-09-05 00:53:05 +04:00
|
|
|
the idea and the manual page were taken from
|
2006-01-13 00:39:45 +03:00
|
|
|
.Ox .
|