Annotation of src/usr.bin/mktemp/mktemp.1, Revision 1.10
1.10 ! aaron 1: .\" $OpenBSD: mktemp.1,v 1.9 1998/04/13 17:22:10 millert Exp $
1.1 millert 2: .\"
3: .\" Copyright (c) 1989, 1991, 1993
4: .\" The Regents of the University of California. All rights reserved.
5: .\"
6: .\" Redistribution and use in source and binary forms, with or without
7: .\" modification, are permitted provided that the following conditions
8: .\" are met:
9: .\" 1. Redistributions of source code must retain the above copyright
10: .\" notice, this list of conditions and the following disclaimer.
11: .\" 2. Redistributions in binary form must reproduce the above copyright
12: .\" notice, this list of conditions and the following disclaimer in the
13: .\" documentation and/or other materials provided with the distribution.
14: .\" 3. All advertising materials mentioning features or use of this software
15: .\" must display the following acknowledgement:
16: .\" This product includes software developed by the University of
17: .\" California, Berkeley and its contributors.
18: .\" 4. Neither the name of the University nor the names of its contributors
19: .\" may be used to endorse or promote products derived from this software
20: .\" without specific prior written permission.
21: .\"
22: .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23: .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24: .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25: .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26: .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27: .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28: .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29: .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30: .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31: .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32: .\" SUCH DAMAGE.
33: .\"
1.4 deraadt 34: .Dd November, 20, 1996
1.1 millert 35: .Dt MKTEMP 1
36: .Os
37: .Sh NAME
38: .Nm mktemp
39: .Nd make temporary file name (unique)
40: .Sh SYNOPSIS
41: .Nm mktemp
1.5 millert 42: .Op Fl d
43: .Op Fl q
1.1 millert 44: .Op Fl u
45: .Ar template
46: .Sh DESCRIPTION
47: The
48: .Nm mktemp
49: utility takes the given file name template and overwrites a
50: portion of it to create a file name. This file name is unique
51: and suitable for use by the application. The template may be
52: any file name with some number of
53: .Ql X Ns s
54: appended
55: to it, for example
1.9 millert 56: .Pa /tmp/temp.XXXXXX .
1.1 millert 57: The trailing
58: .Ql X Ns s
59: are replaced with the current process number and/or a
60: unique letter combination.
61: The number of unique file names
62: .Nm
63: can return depends on the number of
64: .Ql X Ns s
65: provided; six
66: .Ql X Ns s
67: will
68: result in
69: .Nm
70: testing roughly 26 ** 6 combinations.
71: .Pp
72: If
73: .Nm
74: can successfully generate a unique file name, the file
75: is created with mode 0600 (unless the
76: .Fl u
77: flag is given) and the filename is printed
78: to standard output.
1.8 millert 79: .Pp
1.10 ! aaron 80: .Nm mktemp
1.8 millert 81: is provided to allow shell scripts to safely use temporary files.
82: Traditionally, many shell scripts take the name of the program with
1.10 ! aaron 83: the PID as a suffix and use that as a temporary file name. This
1.8 millert 84: kind of naming scheme is predictable and the race condition it creates
1.10 ! aaron 85: is easy for an attacker to win. A safer, though still inferior approach
1.8 millert 86: is to make a temporary directory using the same naming scheme. While
87: this does allow one to guarantee that a temporary file will not be
88: subverted, it still allows a simple denial of service attack. For these
89: reasons it is suggested that
90: .Nm
91: be used instead.
1.1 millert 92: .Sh OPTIONS
93: .Bl -tag -width indent
94: The available options are as follows:
1.5 millert 95: .It Fl d
96: Make a directory instead of a file.
97: .It Fl q
98: Fail silently if an error occurs. This is useful if
99: a script does not want error output to go to standard error.
1.1 millert 100: .It Fl u
101: Operate in
102: .Dq unsafe
103: mode. The temp file will be unlinked before
104: .Nm
105: exits. This is slightly better than
106: .Fn mktemp 3
107: but still introduces a race condition. Use of this
108: option is not encouraged.
1.6 millert 109: .El
1.1 millert 110: .Sh RETURN VALUES
111: The
112: .Nm
113: utility
1.10 ! aaron 114: exits with a value of 0 on success or 1 on failure.
1.1 millert 115: .Sh EXAMPLES
116: The following
117: .Xr sh 1
118: fragment illustrates a simple use of
119: .Nm
120: where the script should quit if it cannot get a safe
121: temporary file.
122: .Bd -literal -offset indent
123: TMPFILE=`mktemp /tmp/$0.XXXXXX` || exit 1
124: echo "program output" >> $TMPFILE
125: .Ed
126: .Pp
127: In this case, we want the script to catch the error itself.
128: .Bd -literal -offset indent
1.3 millert 129: TMPFILE=`mktemp -q /tmp/$0.XXXXXX`
1.2 millert 130: if [ $? -ne 0 ]; then
1.1 millert 131: echo "$0: Can't create temp file, exiting..."
132: exit 1
133: fi
134: .Ed
135: .Sh SEE ALSO
1.8 millert 136: .Xr mkdtemp 3 ,
1.10 ! aaron 137: .Xr mkstemp 3 ,
! 138: .Xr mktemp 3
1.1 millert 139: .Sh HISTORY
140: The
141: .Nm
142: utility appeared in
1.7 millert 143: .Ox 2.1 .