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. 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
36 .Nd make temporary file name (unique)
42 .Fn mktemp "char *template"
44 .Fn mkstemp "char *template"
46 .Fn mkostemp "char *template" "int oflags"
48 .Fn mkostemps "char *template" "int suffixlen" "int oflags"
50 .Fn mkostempsat "int dfd" "char *template" "int suffixlen" "int oflags"
52 .Fn mkdtemp "char *template"
55 .Fn mkstemps "char *template" "int suffixlen"
60 takes the given file name template and overwrites a portion of it
61 to create a file name.
62 This file name is guaranteed not to exist at the time of function invocation
63 and is suitable for use
65 The template may be any file name with some number of
69 .Pa /tmp/temp.XXXXXX .
73 unique alphanumeric combination.
74 The number of unique file names
76 can return depends on the number of
83 selecting one of 56800235584 (62 ** 6) possible temporary file names.
88 makes the same replacement to the template and creates the template file,
89 mode 0600, returning a file descriptor opened for reading and writing.
90 This avoids the race between testing for a file's existence and opening it
98 but allows specifying additional
102 The permitted flags are
115 functions act the same as
120 except they permit a suffix to exist in the template.
121 The template should be of the form
122 .Pa /tmp/tmpXXXXXXsuffix .
128 are told the length of the suffix string.
132 function acts the same as
134 but takes an additional directory descriptor as a parameter.
135 The temporary file is created relative to the corresponding
136 directory, or to the current working directory if the special
140 If the template path is an absolute path, the
142 parameter is ignored and the behavior is identical to
147 function makes the same replacement to the template as in
149 and creates the template directory, mode 0700.
155 functions return a pointer to the template on success and
165 return \-1 if no suitable file could be created.
166 If either call fails an error code is placed in the global variable
179 to one of the following values:
182 The pathname portion of the template is not an existing directory.
192 to the following value:
210 to any value specified by the
223 to any value specified by the
232 to any value specified by the
236 A common problem that results in a core dump is that the programmer
237 passes in a read-only string to
243 This is common with programs that were developed before
245 compilers were common.
249 .Qq /tmp/tempfile.XXXXXX
250 will result in a core dump due to
252 attempting to modify the string constant that was given.
259 function prototypes are also available from
272 functions are expected to conform to
276 function is expected to conform to
278 and is not specified by
286 functions do not conform to any standard.
298 function first appeared in
304 function first appeared in
312 functions appeared in
319 This family of functions produces filenames which can be guessed,
320 though the risk is minimized when large numbers of
323 increase the number of possible temporary filenames.
324 This makes the race in
326 between testing for a file's existence (in the
329 and opening it for use
330 (later in the user application)
331 particularly dangerous from a security perspective.
332 Whenever it is possible,
337 should be used instead, since they do not have the race condition.
340 cannot be used, the filename created by
342 should be created using the
346 and the return status of the call should be tested for failure.
347 This will ensure that the program does not continue blindly
348 in the event that an attacker has already created the file
349 with the intention of manipulating or reading its contents.