version 1.19, 2001/10/01 17:08:30 |
version 1.20, 2001/10/11 00:05:55 |
|
|
.Os |
.Os |
.Sh NAME |
.Sh NAME |
.Nm mktemp |
.Nm mktemp |
.Nd make temporary file name (unique) |
.Nd make temporary filename (unique) |
.Sh SYNOPSIS |
.Sh SYNOPSIS |
.Nm mktemp |
.Nm mktemp |
.Op Fl dqtu |
.Op Fl dqtu |
.Op Fl p Ar directory |
.Op Fl p Ar directory |
.Ar template |
.Op Ar template |
.Sh DESCRIPTION |
.Sh DESCRIPTION |
The |
The |
.Nm mktemp |
.Nm mktemp |
utility takes the given file name template and overwrites a |
utility takes the given filename |
portion of it to create a unique file name. |
.Ar template |
The template may be any file name with some number of |
and overwrites a portion of it to create a unique filename. |
|
The |
|
.Ar template |
|
may be any filename with some number of |
.Ql X Ns s |
.Ql X Ns s |
appended |
appended |
to it, for example |
to it, for example |
.Pa /tmp/temp.XXXXXXXXXX . |
.Pa /tmp/tfile.XXXXXXXXXX . |
|
If no |
|
.Ar template |
|
is specified a default of |
|
.Pa tmp.XXXXXXXXXX |
|
is used and the |
|
.Fl t |
|
flag is implied (see below). |
.Pp |
.Pp |
The trailing |
The trailing |
.Ql X Ns s |
.Ql X Ns s |
|
|
random letters. |
random letters. |
The name chosen depends both on the number of |
The name chosen depends both on the number of |
.Ql X Ns s |
.Ql X Ns s |
in the template and the number of collisions with pre-existing files. |
in the |
The number of unique file names |
.Ar template |
|
and the number of collisions with pre-existing files. |
|
The number of unique filenames |
.Nm |
.Nm |
can return depends on the number of |
can return depends on the number of |
.Ql X Ns s |
.Ql X Ns s |
|
|
.Pp |
.Pp |
If |
If |
.Nm |
.Nm |
can successfully generate a unique file name, the file (or directory) |
can successfully generate a unique filename, the file (or directory) |
is created with file permissions such that it is only readable and writable |
is created with file permissions such that it is only readable and writable |
by its owner (unless the |
by its owner (unless the |
.Fl u |
.Fl u |
|
|
.Nm mktemp |
.Nm mktemp |
is provided to allow shell scripts to safely use temporary files. |
is provided to allow shell scripts to safely use temporary files. |
Traditionally, many shell scripts take the name of the program with |
Traditionally, many shell scripts take the name of the program with |
the PID as a suffix and use that as a temporary file name. |
the PID as a suffix and use that as a temporary filename. |
This kind of naming scheme is predictable and the race condition it creates |
This kind of naming scheme is predictable and the race condition it creates |
is easy for an attacker to win. |
is easy for an attacker to win. |
A safer, though still inferior approach |
A safer, though still inferior approach |
|
|
.It Fl p Ar directory |
.It Fl p Ar directory |
Use the specified |
Use the specified |
.Ar directory |
.Ar directory |
as a prefix when generating the temporary file name. |
as a prefix when generating the temporary filename. |
The |
The |
.Ar directory |
.Ar directory |
will be overridden by the user's |
will be overridden by the user's |
|
|
This is useful if |
This is useful if |
a script does not want error output to go to standard error. |
a script does not want error output to go to standard error. |
.It Fl t |
.It Fl t |
Generate a temporary file rooted in a temporary directory. |
Generate a path rooted in a temporary directory. |
The temporary directory is chosen as follows: |
This directory is chosen as follows: |
.Bl -bullet |
.Bl -bullet |
.It |
.It |
If the user's |
If the user's |
|
|
.Pp |
.Pp |
In this mode, the |
In this mode, the |
.Ar template |
.Ar template |
should be a directory component (as opposed to a full path) and thus |
(if specified) should be a directory component (as opposed to a full path) |
should not contain any forward slashes. |
and thus should not contain any forward slashes. |
.It Fl u |
.It Fl u |
Operate in |
Operate in |
.Dq unsafe |
.Dq unsafe |
|
|
where the script should quit if it cannot get a safe |
where the script should quit if it cannot get a safe |
temporary file. |
temporary file. |
.Bd -literal -offset indent |
.Bd -literal -offset indent |
CMD=`basename $0` |
TMPFILE=`mktemp /tmp/example.XXXXXXXXXX` || exit 1 |
TMPFILE=`mktemp /tmp/$CMD.XXXXXXXXXX` || exit 1 |
|
echo "program output" >> $TMPFILE |
echo "program output" >> $TMPFILE |
.Ed |
.Ed |
.Pp |
.Pp |
|
|
.Ev TMPDIR |
.Ev TMPDIR |
environment variable can be written as follows. |
environment variable can be written as follows. |
.Bd -literal -offset indent |
.Bd -literal -offset indent |
CMD=`basename $0` |
TMPFILE=`mktemp -t example.XXXXXXXXXX` || exit 1 |
TMPFILE=`mktemp -t $CMD.XXXXXXXXXX` || exit 1 |
|
echo "program output" >> $TMPFILE |
echo "program output" >> $TMPFILE |
.Ed |
.Ed |
.Pp |
.Pp |
|
This can be further simplified if we don't care about the actual name of |
|
the temporary file. In this case the |
|
.Fl t |
|
flag is implied. |
|
.Bd -literal -offset indent |
|
TMPFILE=`mktemp` || exit 1 |
|
echo "program output" >> $TMPFILE |
|
.Ed |
|
.Pp |
In some cases, it may be desirable to use a default temporary directory |
In some cases, it may be desirable to use a default temporary directory |
other than |
other than |
.Pa /tmp . |
.Pa /tmp . |
|
|
.Ev TMPDIR |
.Ev TMPDIR |
environment variable specifies otherwise. |
environment variable specifies otherwise. |
.Bd -literal -offset indent |
.Bd -literal -offset indent |
CMD=`basename $0` |
TMPFILE=`mktemp -p /extra/tmp example.XXXXXXXXXX` || exit 1 |
TMPFILE=`mktemp -t -p /extra/tmp $CMD.XXXXXXXXXX` || exit 1 |
|
echo "program output" >> $TMPFILE |
echo "program output" >> $TMPFILE |
.Ed |
.Ed |
.Pp |
.Pp |
|
|
For instance, if we attempt to create two temporary files and |
For instance, if we attempt to create two temporary files and |
the second one fails we need to remove the first before exiting. |
the second one fails we need to remove the first before exiting. |
.Bd -literal -offset indent |
.Bd -literal -offset indent |
CMD=`basename $0` |
TMP1=`mktemp -t example.1.XXXXXXXXXX` || exit 1 |
TMP1=`mktemp -t $CMD.1.XXXXXXXXXX` || exit 1 |
TMP2=`mktemp -t example.2.XXXXXXXXXX` |
TMP2=`mktemp -t $CMD.2.XXXXXXXXXX` |
|
if [ $? -ne 0 ]; then |
if [ $? -ne 0 ]; then |
rm -f $TMP1 |
rm -f $TMP1 |
exit 1 |
exit 1 |
|
|
is unable to create the file. |
is unable to create the file. |
In this case you can protect that part of the script thusly. |
In this case you can protect that part of the script thusly. |
.Bd -literal -offset indent |
.Bd -literal -offset indent |
CMD=`basename $0` |
TMPFILE=`mktemp -q -t example.XXXXXXXXXX` && { |
TMPFILE=`mktemp -q -t $CMD.XXXXXXXXXX` && { |
|
# Safe to use $TMPFILE in this block |
# Safe to use $TMPFILE in this block |
echo data > $TMPFILE |
echo data > $TMPFILE |
... |
... |