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
43 .Nd make temporary file name (unique)
98 takes the given file name template and overwrites a portion of it
99 to create a file name.
100 This file name is guaranteed not to exist at the time of function invocation
101 and is suitable for use
103 The template may be any file name with some number of
107 .Pa /tmp/temp.XXXXXX .
111 unique alphanumeric combination.
112 The number of unique file names
114 can return depends on the number of
121 selecting one of 56800235584 (62 ** 6) possible temporary file names.
126 makes the same replacement to the template and creates the template file,
127 mode 0600, returning a file descriptor opened for reading and writing.
128 This avoids the race between testing for a file's existence and opening it
133 function makes the same replacement to the template as in
135 and creates the template directory, mode 0700.
142 but allows specifying additional
146 The permitted flags are
157 functions act the same as
162 except they permit a suffix to exist in the template.
163 The template should be of the form
164 .Pa /tmp/tmpXXXXXXsuffix .
170 are told the length of the suffix string.
184 except in the case where the
186 specifies a relative path.
187 In this case, path is treated as relative to the directory associated with the file descriptor
189 instead of the current working directory.
195 functions return a pointer to the template on success and
205 return \-1 if no suitable file could be created.
206 If either call fails an error code is placed in the global variable
219 to one of the following values:
222 The pathname portion of the template is not an existing directory.
232 to the following value:
250 to any value specified by the
263 to any value specified by the
272 to any value specified by the
276 A common problem that results in a core dump is that the programmer
277 passes in a read-only string to
283 This is common with programs that were developed before
285 compilers were common.
289 .Qq /tmp/tempfile.XXXXXX
290 will result in a core dump due to
292 attempting to modify the string constant that was given.
294 .Fd #include <unistd.h>
298 is necessary and sufficient for all functions.
311 functions are expected to conform to
315 function is expected to conform to
317 and is not specified by
324 functions do not conform to any standard.
336 function first appeared in
342 function first appeared in
350 functions appeared in OS X 10.12.
352 This family of functions produces filenames which can be guessed,
353 though the risk is minimized when large numbers of
356 increase the number of possible temporary filenames.
357 This makes the race in
359 between testing for a file's existence (in the
362 and opening it for use
363 (later in the user application)
364 particularly dangerous from a security perspective.
365 Whenever it is possible,
369 should be used instead, since it does not have the race condition.
372 cannot be used, the filename created by
374 should be created using the
378 and the return status of the call should be tested for failure.
379 This will ensure that the program does not continue blindly
380 in the event that an attacker has already created the file
381 with the intention of manipulating or reading its contents.