Clarify which part of the path name is considered for substitution.
Also use "name" instead of "file name" is some places since the
result could be either a file or a directory. Work done with jmc@
···11-.\" $OpenBSD: mktemp.1,v 1.33 2024/03/01 23:37:42 millert Exp $
11+.\" $OpenBSD: mktemp.1,v 1.34 2024/03/03 15:24:45 millert Exp $
22.\"
33.\" Copyright (c) 1996, 2000, 2001, 2003, 2010, 2013, 2024
44.\" Todd C. Miller <millert@openbsd.org>
···1515.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1616.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1717.\"
1818-.Dd $Mdocdate: March 1 2024 $
1818+.Dd $Mdocdate: March 3 2024 $
1919.Dt MKTEMP 1
2020.Os
2121.Sh NAME
2222.Nm mktemp
2323-.Nd make temporary filename (unique)
2323+.Nd make temporary file or directory (unique)
2424.Sh SYNOPSIS
2525.Nm mktemp
2626.Op Fl dqtu
···2929.Sh DESCRIPTION
3030The
3131.Nm mktemp
3232-utility takes the given filename
3232+utility takes the specified file name
3333.Ar template
3434-and overwrites a portion of it to create a unique filename.
3434+and overwrites a portion of it to create a unique file name.
3535The
3636.Ar template
3737-may be any filename with at least six
3737+may be any file name containing at least six
3838.Ql X Ns s
3939-in the last component of the filename, for example
3939+in the last component of the path, for example
4040.Pa /tmp/tfile.XXXXXXXXXX
4141or
4242.Pa /tmp/editor.XXXXXXXXXX.txt .
···4747only the last one will be considered.
4848If no
4949.Ar template
5050-is specified, a default of
5050+is specified, a default value of
5151.Pa tmp.XXXXXXXXXX
5252is used and the
5353.Fl t
···6060.Ql X Ns s
6161in the
6262.Ar template
6363-and the number of collisions with pre-existing files.
6464-The number of unique filenames
6363+and the number of collisions with pre-existing entries.
6464+The number of unique names
6565.Nm
6666can return depends on the number of
6767.Ql X Ns s
···7474.Pp
7575If
7676.Nm
7777-can successfully generate a unique filename, the file (or directory)
7878-is created with file permissions such that it is only readable and writable
7777+successfully generates a unique name, the file (or directory)
7878+is created with permissions such that it is only readable and writable
7979by its owner (unless the
8080.Fl u
8181-flag is given) and the filename is printed to standard output.
8181+flag is given) and the name is printed to the standard output.
8282.Pp
8383.Nm mktemp
8484is provided to allow shell scripts to safely use temporary files.
8585Traditionally, many shell scripts take the name of the program with
8686-the PID as a suffix and use that as a temporary filename.
8686+the PID as a suffix and use that as a temporary file name.
8787This kind of naming scheme is predictable and the race condition it creates
8888is easy for an attacker to win.
8989-A safer, though still inferior approach
8989+A safer, though still inferior approach,
9090is to make a temporary directory using the same naming scheme.
9191While this does allow one to guarantee that a temporary file will not be
9292subverted, it still allows a simple denial of service attack.
···101101.It Fl p Ar directory
102102Use the specified
103103.Ar directory
104104-as a prefix when generating the temporary filename.
104104+as a prefix when generating the temporary name.
105105The
106106.Ar directory
107107will be overridden by the user's
···163163fragment illustrates a simple use of
164164.Nm
165165where the script should quit if it cannot get a safe
166166-temporary file.
166166+temporary file:
167167.Bd -literal -offset indent
168168TMPFILE=`mktemp /tmp/example.XXXXXXXXXX` || exit 1
169169echo "program output" >> $TMPFILE
···171171.Pp
172172The same fragment with support for a user's
173173.Ev TMPDIR
174174-environment variable can be written as follows.
174174+environment variable can be written as follows:
175175.Bd -literal -offset indent
176176TMPFILE=`mktemp -t example.XXXXXXXXXX` || exit 1
177177echo "program output" >> $TMPFILE
···181181the temporary file.
182182In this case the
183183.Fl t
184184-flag is implied.
184184+flag is implied:
185185.Bd -literal -offset indent
186186TMPFILE=`mktemp` || exit 1
187187echo "program output" >> $TMPFILE
···194194.Pa /extra/tmp
195195unless the user's
196196.Ev TMPDIR
197197-environment variable specifies otherwise.
197197+environment variable specifies otherwise:
198198.Bd -literal -offset indent
199199TMPFILE=`mktemp -p /extra/tmp example.XXXXXXXXXX` || exit 1
200200echo "program output" >> $TMPFILE
···202202.Pp
203203In other cases, we want the script to catch the error.
204204For instance, if we attempt to create two temporary files and
205205-the second one fails we need to remove the first before exiting.
205205+the second one fails we need to remove the first before exiting:
206206.Bd -literal -offset indent
207207-TMP1=`mktemp -t example.1.XXXXXXXXXX` || exit 1
208208-TMP2=`mktemp -t example.2.XXXXXXXXXX`
207207+TMP1=`mktemp -t example.XXXXXXXXXX.1` || exit 1
208208+TMP2=`mktemp -t example.XXXXXXXXXX.2`
209209if [ $? -ne 0 ]; then
210210 rm -f $TMP1
211211 exit 1
···215215Or perhaps you don't want to exit if
216216.Nm
217217is unable to create the file.
218218-In this case you can protect that part of the script thusly.
218218+In this case you can protect that part of the script thusly:
219219.Bd -literal -offset indent
220220TMPFILE=`mktemp -q -t example.XXXXXXXXXX` && {
221221 # Safe to use $TMPFILE in this block