Annotation of src/usr.bin/mktemp/mktemp.1, Revision 1.21
1.21 ! millert 1: .\" $OpenBSD: mktemp.1,v 1.20 2001/10/11 00:05:55 millert Exp $
1.1 millert 2: .\"
1.19 millert 3: .\" Copyright (c) 1996, 2000, 2001 Todd C. Miller <Todd.Miller@courtesan.com>
1.1 millert 4: .\"
1.21 ! millert 5: .\" Permission to use, copy, modify, and distribute this software for any
! 6: .\" purpose with or without fee is hereby granted, provided that the above
! 7: .\" copyright notice and this permission notice appear in all copies.
1.1 millert 8: .\"
1.21 ! millert 9: .\" THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
! 10: .\" WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
! 11: .\" OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
! 12: .\" FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
! 13: .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
! 14: .\" OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
! 15: .\" CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 millert 16: .\"
1.19 millert 17: .Dd September 30, 2001
1.1 millert 18: .Dt MKTEMP 1
19: .Os
20: .Sh NAME
21: .Nm mktemp
1.20 millert 22: .Nd make temporary filename (unique)
1.1 millert 23: .Sh SYNOPSIS
24: .Nm mktemp
1.19 millert 25: .Op Fl dqtu
26: .Op Fl p Ar directory
1.20 millert 27: .Op Ar template
1.1 millert 28: .Sh DESCRIPTION
29: The
30: .Nm mktemp
1.20 millert 31: utility takes the given filename
32: .Ar template
33: and overwrites a portion of it to create a unique filename.
34: The
35: .Ar template
36: may be any filename with some number of
1.1 millert 37: .Ql X Ns s
38: appended
39: to it, for example
1.20 millert 40: .Pa /tmp/tfile.XXXXXXXXXX .
41: If no
42: .Ar template
43: is specified a default of
44: .Pa tmp.XXXXXXXXXX
45: is used and the
46: .Fl t
47: flag is implied (see below).
1.15 aaron 48: .Pp
1.1 millert 49: The trailing
50: .Ql X Ns s
1.16 aaron 51: are replaced with a combination of the current process number and
1.15 aaron 52: random letters.
53: The name chosen depends both on the number of
1.13 millert 54: .Ql X Ns s
1.20 millert 55: in the
56: .Ar template
57: and the number of collisions with pre-existing files.
58: The number of unique filenames
1.1 millert 59: .Nm
60: can return depends on the number of
61: .Ql X Ns s
1.13 millert 62: provided; ten
1.1 millert 63: .Ql X Ns s
64: will
65: result in
66: .Nm
1.13 millert 67: testing roughly 26 ** 10 combinations.
1.1 millert 68: .Pp
69: If
70: .Nm
1.20 millert 71: can successfully generate a unique filename, the file (or directory)
1.13 millert 72: is created with file permissions such that it is only readable and writable
73: by its owner (unless the
1.1 millert 74: .Fl u
1.13 millert 75: flag is given) and the filename is printed to standard output.
1.8 millert 76: .Pp
1.10 aaron 77: .Nm mktemp
1.8 millert 78: is provided to allow shell scripts to safely use temporary files.
79: Traditionally, many shell scripts take the name of the program with
1.20 millert 80: the PID as a suffix and use that as a temporary filename.
1.15 aaron 81: This kind of naming scheme is predictable and the race condition it creates
82: is easy for an attacker to win.
83: A safer, though still inferior approach
84: is to make a temporary directory using the same naming scheme.
85: While this does allow one to guarantee that a temporary file will not be
86: subverted, it still allows a simple denial of service attack.
87: For these reasons it is suggested that
1.8 millert 88: .Nm
89: be used instead.
1.14 aaron 90: .Pp
91: The options are as follows:
1.18 aaron 92: .Bl -tag -width Ds
1.5 millert 93: .It Fl d
94: Make a directory instead of a file.
1.19 millert 95: .It Fl p Ar directory
96: Use the specified
97: .Ar directory
1.20 millert 98: as a prefix when generating the temporary filename.
1.19 millert 99: The
100: .Ar directory
101: will be overridden by the user's
102: .Ev TMPDIR
103: environment variable if it is set.
104: This option implies the
105: .Fl t
106: flag (see below).
1.5 millert 107: .It Fl q
1.15 aaron 108: Fail silently if an error occurs.
109: This is useful if
1.5 millert 110: a script does not want error output to go to standard error.
1.19 millert 111: .It Fl t
1.20 millert 112: Generate a path rooted in a temporary directory.
113: This directory is chosen as follows:
1.19 millert 114: .Bl -bullet
115: .It
116: If the user's
117: .Ev TMPDIR
118: environment variable is set, the directory contained therein is used.
119: .It
120: Otherwise, if the
121: .Fl p
122: flag was given the specified directory is used.
123: .It
124: If none of the above apply,
125: .Pa /tmp
126: is used.
127: .El
128: .Pp
129: In this mode, the
130: .Ar template
1.20 millert 131: (if specified) should be a directory component (as opposed to a full path)
132: and thus should not contain any forward slashes.
1.1 millert 133: .It Fl u
134: Operate in
135: .Dq unsafe
1.15 aaron 136: mode.
137: The temp file will be unlinked before
1.1 millert 138: .Nm
1.15 aaron 139: exits.
140: This is slightly better than
1.1 millert 141: .Fn mktemp 3
1.15 aaron 142: but still introduces a race condition.
143: Use of this option is not encouraged.
1.6 millert 144: .El
1.15 aaron 145: .Pp
1.1 millert 146: The
147: .Nm
148: utility
1.10 aaron 149: exits with a value of 0 on success or 1 on failure.
1.1 millert 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
1.20 millert 158: TMPFILE=`mktemp /tmp/example.XXXXXXXXXX` || exit 1
1.1 millert 159: echo "program output" >> $TMPFILE
160: .Ed
161: .Pp
1.19 millert 162: The same fragment with support for a user's
163: .Ev TMPDIR
164: environment variable can be written as follows.
1.1 millert 165: .Bd -literal -offset indent
1.20 millert 166: TMPFILE=`mktemp -t example.XXXXXXXXXX` || exit 1
167: echo "program output" >> $TMPFILE
168: .Ed
169: .Pp
170: This can be further simplified if we don't care about the actual name of
171: the temporary file. In this case the
172: .Fl t
173: flag is implied.
174: .Bd -literal -offset indent
175: TMPFILE=`mktemp` || exit 1
1.19 millert 176: echo "program output" >> $TMPFILE
177: .Ed
178: .Pp
179: In some cases, it may be desirable to use a default temporary directory
180: other than
181: .Pa /tmp .
182: In this example the temporary file will be created in
183: .Pa /extra/tmp
184: unless the user's
185: .Ev TMPDIR
186: environment variable specifies otherwise.
187: .Bd -literal -offset indent
1.20 millert 188: TMPFILE=`mktemp -p /extra/tmp example.XXXXXXXXXX` || exit 1
1.19 millert 189: echo "program output" >> $TMPFILE
190: .Ed
191: .Pp
192: In some cases, we want the script to catch the error.
193: For instance, if we attempt to create two temporary files and
194: the second one fails we need to remove the first before exiting.
195: .Bd -literal -offset indent
1.20 millert 196: TMP1=`mktemp -t example.1.XXXXXXXXXX` || exit 1
197: TMP2=`mktemp -t example.2.XXXXXXXXXX`
1.2 millert 198: if [ $? -ne 0 ]; then
1.19 millert 199: rm -f $TMP1
1.1 millert 200: exit 1
201: fi
1.12 millert 202: .Ed
203: .Pp
204: Or perhaps you don't want to exit if
205: .Nm
1.15 aaron 206: is unable to create the file.
1.19 millert 207: In this case you can protect that part of the script thusly.
1.12 millert 208: .Bd -literal -offset indent
1.20 millert 209: TMPFILE=`mktemp -q -t example.XXXXXXXXXX` && {
1.12 millert 210: # Safe to use $TMPFILE in this block
211: echo data > $TMPFILE
212: ...
213: rm -f $TMPFILE
214: }
1.1 millert 215: .Ed
1.19 millert 216: .Sh ENVIRONMENT
217: .Bl -tag -width TMPDIR
218: .It Ev TMPDIR
219: directory in which to place the temporary file when in
220: .Fl t
221: mode
222: .El
1.1 millert 223: .Sh SEE ALSO
1.8 millert 224: .Xr mkdtemp 3 ,
1.10 aaron 225: .Xr mkstemp 3 ,
226: .Xr mktemp 3
1.1 millert 227: .Sh HISTORY
228: The
229: .Nm
230: utility appeared in
1.7 millert 231: .Ox 2.1 .