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 .\" 4. Neither the name of the University nor the names of its contributors
13 .\" may be used to endorse or promote products derived from this software
14 .\" without specific prior written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" @(#)mktemp.3 8.1 (Berkeley) 6/4/93
29 .\" $FreeBSD: src/lib/libc/stdio/mktemp.3,v 1.22 2007/01/09 00:28:07 imp Exp $
39 .Nd make temporary file name (unique)
66 takes the given file name template and overwrites a portion of it
67 to create a file name.
68 This file name is guaranteed not to exist at the time of function invocation
69 and is suitable for use
71 The template may be any file name with some number of
75 .Pa /tmp/temp.XXXXXX .
79 unique alphanumeric combination.
80 The number of unique file names
82 can return depends on the number of
89 selecting one of 56800235584 (62 ** 6) possible temporary file names.
94 makes the same replacement to the template and creates the template file,
95 mode 0600, returning a file descriptor opened for reading and writing.
96 This avoids the race between testing for a file's existence and opening it
101 function acts the same as
103 except it permits a suffix to exist in the template.
104 The template should be of the form
105 .Pa /tmp/tmpXXXXXXsuffix .
109 is told the length of the suffix string.
113 function makes the same replacement to the template as in
115 and creates the template directory, mode 0700.
121 functions return a pointer to the template on success and
129 return \-1 if no suitable file could be created.
130 If either call fails an error code is placed in the global variable
141 to one of the following values:
144 The pathname portion of the template is not an existing directory.
155 to any value specified by the
166 to any value specified by the
175 to any value specified by the
179 A common problem that results in a core dump is that the programmer
180 passes in a read-only string to
186 This is common with programs that were developed before
188 compilers were common.
192 .Qq /tmp/tempfile.XXXXXX
193 will result in a core dump due to
195 attempting to modify the string constant that was given.
196 If the program in question makes heavy use of that type
197 of function call, you do have the option of compiling the program
198 so that it will store string constants in a writable segment of memory.
201 for more information.
203 .Fd #include <unistd.h>
207 is necessary and sufficient for all functions.
226 function first appeared in
232 function first appeared in
237 This family of functions produces filenames which can be guessed,
238 though the risk is minimized when large numbers of
241 increase the number of possible temporary filenames.
242 This makes the race in
244 between testing for a file's existence (in the
247 and opening it for use
248 (later in the user application)
249 particularly dangerous from a security perspective.
250 Whenever it is possible,
252 should be used instead, since it does not have the race condition.
255 cannot be used, the filename created by
257 should be created using the
261 and the return status of the call should be tested for failure.
262 This will ensure that the program does not continue blindly
263 in the event that an attacker has already created the file
264 with the intention of manipulating or reading its contents.