NAME
mkdtemp mkostemp
mkstemp — create a unique
directory or file
SYNOPSIS
char *mkdtemp(char *template); int mkostemp(char *template, int flag); int mkstemp(char *template);
DESCRIPTION
The mkdtemp () function shall create a
directory with a unique name derived from template. The
application shall ensure that the string provided in
template is a pathname ending with at least six trailing
‘X’ characters. The
mkdtemp () function shall modify the contents of
template by replacing six or more
‘X’ characters at the end of the
pathname with the same number of characters from the portable filename
character set. The characters shall be chosen such that the resulting
pathname does not duplicate the name of an existing file at the time of the
call to mkdtemp (). The
mkdtemp () function shall use the resulting pathname
to create the new directory as if by a call to:
mkdir(pathname, S_IRWXU)
The mkstemp () function shall create a
regular file with a unique name derived from template and
return a file descriptor for the file open for reading and writing. The
application shall ensure that the string provided in
template is a pathname ending with at least six trailing
‘X’ characters. The
mkstemp () function shall modify the contents of
template by replacing six or more
‘X’ characters at the end of the
pathname with the same number of characters from the portable filename
character set. The characters shall be chosen such that the resulting
pathname does not duplicate the name of an existing file at the time of the
call to mkstemp (). The
mkstemp () function shall use the resulting pathname
to create the file, and obtain a file descriptor for it, as if by a call
to:
open(pathname, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR)
By behaving as if the O_EXCL flag for open(3) is set, the function prevents any possible race condition between testing whether the file exists and opening it for use.
The mkostemp () function shall be equivalent to
the mkstemp () function, except that the
flag argument can contain additional flags to be used as
if by open(3). Behavior is unspecified if the
flag argument contains more than the following flags:
- O_APPEND
- Set append mode.
- O_CLOEXEC
- Set the FD_CLOEXEC file descriptor flag.
- O_CLOFORK
- Set the FD_CLOFORK file descriptor flag.
- O_DSYNC
- [SIO] Write according to the synchronized I/O data integrity completion.
- O_RSYNC
- [SIO] Synchronized read I/O operations.
- O_SYNC
- [XSI|SIO] Write according to synchronized I/O file integrity completion.
RETURN VALUE
Upon successful completion, the mkdtemp ()
function shall return the value of template. Otherwise, it
shall return a null pointer and shall set errno to
indicate the error.
Upon successful completion, the mkstemp ()
function shall return an open file descriptor. Otherwise, it shall return -1
and shall set errno to indicate the error.
ERRORS
These functions shall fail if:
- [EINVAL]
- The string pointed to by template does not end in "XXXXXX" .
The mkostemp () function may fail if:
- [EINVAL]
- The value of the flag argument is invalid.
Additional error conditions for the
mkdtemp () function are defined in
mkdir(3). Additional error conditions for the
mkstemp () and mkostemp ()
functions are defined in open(3).
EXAMPLES
Generating a Pathname
The following example creates a file with a 10-character name
beginning with the characters "file" and
opens the file for reading and writing. The value returned as the value of
fd is a file
descriptor that identifies the file.
#include <stdlib.h> ... char template[] = "/tmp/fileXXXXXX"; int fd; fd = mkstemp(template);
APPLICATION USAGE
It is possible to run out of letters.
Portable applications should pass exactly six trailing
‘X’ s in the template and no more;
implementations may treat any additional trailing
‘X’ s as either a fixed or replaceable
part of the template. To be sure of only passing six, a fixed string of at
least one non- ‘X’ character should
precede the six ‘X’ s.
Since ‘X’ is in the portable
filename character set, some of the replacement characters can be
‘X’ s, leaving part (or even all) of
the template effectively unchanged.
RATIONALE
The O_CLOEXEC and O_CLOFORK flags of mkostemp ()
are necessary to avoid a data race in multi-threaded applications. Without
O_CLOFORK, a file descriptor is leaked into a child process created by one
thread in the window between another thread creating a temporary file
descriptor with mkstemp () and then using
fcntl(3) to set the FD_CLOFORK flag. Without O_CLOEXEC, a
temporary file descriptor intentionally inherited by child processes is
similarly leaked into an executed program if FD_CLOEXEC is not set
atomically.
Implementations are encouraged to have
mkdtemp (), mkostemp (), and
mkstemp () report an [EILSEQ] error if the last
component of the pathname in template contains any bytes
that have the encoded value of a <newline> character.
FUTURE DIRECTIONS
None.
SEE ALSO
getpid(3), mkdir(3), open(3), tmpfile(3), tmpnam(3)
XBD <stdlib.h>
CHANGE HISTORY
First released in Issue 4, Version 2.
Issue 5
Moved from X/OPEN UNIX extension to BASE.
Issue 7
Austin Group Interpretation 1003.1-2001 #143 is applied.
SD5-XSH-ERN-168 is applied, clarifying file permissions upon creation.
The mkstemp () function is moved from the
XSI option to the Base.
The mkdtemp () function is added from The
Open Group Technical Standard, 2006, Extended API Set Part 1.
POSIX.1-2008, Technical Corrigendum 1, XSH/TC1-2008/0380 [291], XSH/TC1-2008/0381 [324], and XSH/TC1-2008/0382 [291] are applied.
POSIX.1-2008, Technical Corrigendum 2, XSH/TC2-2008/0215 [567,669] is applied.
Issue 8
Austin Group Defect 251 is applied, encouraging implementations to disallow the creation of filenames containing any bytes that have the encoded value of a <newline> character.
Austin Group Defects 411, 1318, and 1350 are applied, adding mkostemp ().
Austin Group Defect 652 is applied, adding the [EINVAL] error for
mkstemp ().
Austin Group Defect 1734 is applied, replacing the error
conditions specified only for mkdtemp () with a
reference to mkdir(3).