mkstemp, mkstemps, mkostemp, mkostemps - make a unique file name from a template and open the file
#include <stdlib.h> int mkstemp(char *template);
int mkstemps(char *template, int slen);
#include <stdlib.h> #include <fcntl.h> int mkostemp(char *template, int flags);
int mkostemps(char *template, int slen, int flags);
The mkstemp() function replaces the contents of the string pointed to by template by a unique file name, and returns a file descriptor for the file open for reading and writing. The function thus prevents any possible race condition between testing whether the file exists and opening it for use. The string in template should look like a file name with six trailing 'X's; mkstemp() replaces each 'X' with a character from the portable file name character set. The characters are chosen such that the resulting name does not duplicate the name of an existing file.
The mkstemps() function behaves the same as mkstemp(), except it permits a suffix to exist in the template. The template should be of the form /tmp/tmpXXXXXXsuffix. The slen parameter specifies the length of the suffix string.
The mkostemp() and mkostemps() functions behave the same as mkstemp() and mkstemps() functions respectively, except that they take a flags parameter to specify flags to be passed to open(2) in addition to the default O_CREAT | O_EXCL | O_RDWR flags.
Upon successful completion, these functions return an open file descriptor. Otherwise −1 is returned if no suitable file could be created.
It is possible to run out of letters.
These functions do not check to determine whether the file name part of template exceeds the maximum allowable file name length.
These functions are frequently used to create a temporary file that will be removed by the application before the application terminates.
These functions have transitional interfaces for 64-bit file offsets. See lf64(7).
The mkostemp() and mkostemps() functions allow atomically applying the O_CLOEXEC and O_CLOFORK flags to the file descriptor when it is created, with no opportunity for other threads to call functions in the fork or exec families between the open() call and a call to fcntl() to set such flags, thus aiding in thread safety.
See attributes(7) for descriptions of the following attributes:
See standards(7) for descriptions of the following standards:
The mkstemp() function was added to SunOS in SunOS 4.0 and has been included in all Sun and Oracle releases of Solaris.
The mkstemps() function was defined in OpenBSD 2.4, and was added to Oracle Solaris in the Oracle Solaris 11.0 release.
The mkostemp() and mkostemps() functions were defined in GNU libc 2.11, and were added to Oracle Solaris in the Oracle Solaris 11.4.15 release.