mktemp
, mkstemp
, mkstemps
—generate unused file name#include <stdlib.h> char *mktemp(char *path); char *mkdtemp(char *path); int mkstemp(char *path); int mkstemps(char *path, int suffixlen); char *_mktemp_r(struct _reent *reent, char *path); char *_mkdtemp_r(struct _reent *reent, char *path); int *_mkstemp_r(struct _reent *reent, char *path); int *_mkstemps_r(struct _reent *reent, char *path, int len);
Description
mktemp
, mkstemp
, and mkstemps
attempt to generate a file name
that is not yet in use for any existing file. mkstemp
and mkstemps
create the file and open it for reading and writing; mktemp
simply
generates the file name (making mktemp
a security risk). mkdtemp
attempts to create a directory instead of a file, with a permissions
mask of 0700.
You supply a simple pattern for the generated file name, as the string
at path. The pattern should be a valid filename (including path
information if you wish) ending with at least six `X
'
characters. The generated filename will match the leading part of the
name you supply, with the trailing `X
' characters replaced by some
combination of digits and letters. With mkstemps
, the `X
'
characters end suffixlen bytes before the end of the string.
The alternate functions _mktemp_r
, _mkdtemp_r
, _mkstemp_r
,
and _mkstemps_r
are reentrant versions. The extra argument reent
is a pointer to a reentrancy structure.
Returns
mktemp
returns the pointer path to the modified string
representing an unused filename, unless it could not generate one, or
the pattern you provided is not suitable for a filename; in that case,
it returns NULL
.
mkdtemp
returns the pointer path to the modified string if the
directory was created, otherwise it returns NULL
.
mkstemp
and mkstemps
return a file descriptor to the newly created
file, unless it could not generate an unused filename, or the pattern you
provided is not suitable for a filename; in that case, it returns
-1
.
Portability
ANSI C does not require either mktemp
or mkstemp
; the System
V Interface Definition requires mktemp
as of Issue 2. POSIX 2001
requires mkstemp
, and POSIX 2008 requires mkdtemp
, but
mkstemps
is not standardized.
Supporting OS subroutines required: getpid
, mkdir
, open
, stat
.