1 .\" Copyright (c) 1989, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by the University of
15 .\" California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)mktemp.3 8.1 (Berkeley) 6/4/93
33 .\" $FreeBSD: src/lib/libc/stdio/mktemp.3,v 1.17 2001/10/01 16:08:59 ru Exp $
40 .Nd make temporary file name (unique)
46 .Fn mktemp "char *template"
48 .Fn mkstemp "char *template"
50 .Fn mkstemps "char *template" "int suffixlen"
52 .Fn mkdtemp "char *template"
57 takes the given file name template and overwrites a portion of it
58 to create a file name.
59 This file name is guaranteed not to exist at the time of function invocation
60 and is suitable for use
62 The template may be any file name with some number of
66 .Pa /tmp/temp.XXXXXX .
70 unique alphanumeric combination.
71 The number of unique file names
73 can return depends on the number of
80 selecting one of 56800235584 (62 ** 6) possible temporary file names.
85 makes the same replacement to the template and creates the template file,
86 mode 0600, returning a file descriptor opened for reading and writing.
87 This avoids the race between testing for a file's existence and opening it
92 function acts the same as
94 except it permits a suffix to exist in the template.
95 The template should be of the form
96 .Pa /tmp/tmpXXXXXXsuffix .
98 is told the length of the suffix string.
102 function makes the same replacement to the template as in
104 and creates the template directory, mode 0700.
110 functions return a pointer to the template on success and
118 return \-1 if no suitable file could be created.
119 If either call fails an error code is placed in the global variable
130 to one of the following values:
133 The pathname portion of the template is not an existing directory.
144 to any value specified by the
155 to any value specified by the
164 to any value specified by the
168 A common problem that results in a core dump is that the programmer
169 passes in a read-only string to
175 This is common with programs that were developed before
177 compilers were common.
181 .Qq /tmp/tempfile.XXXXXX
182 will result in a core dump due to
184 attempting to modify the string constant that was given.
185 If the program in question makes heavy use of that type
186 of function call, you do have the option of compiling the program
187 so that it will store string constants in a writable segment of memory.
190 for more information.
192 This family of functions produces filenames which can be guessed,
193 though the risk is minimized when large numbers of
196 increase the number of possible temporary filenames.
197 This makes the race in
199 between testing for a file's existence (in the
202 and opening it for use
203 (later in the user application)
204 particularly dangerous from a security perspective.
205 Whenever it is possible,
207 should be used instead, since it does not have the race condition.
210 cannot be used, the filename created by
212 should be created using the
216 and the return status of the call should be tested for failure.
217 This will ensure that the program does not continue blindly
218 in the event that an attacker has already created the file
219 with the intention of manipulating or reading its contents.
237 function first appeared in
243 function first appeared in