Go to main content

man pages section 3: Basic Library Functions

Exit Print View

Updated: Wednesday, July 27, 2022



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.

Return Values

Upon successful completion, these functions return an open file descriptor. Otherwise −1 is returned if no suitable file could be created.


These functions can set errno to the same values as lstat(2) and open(2).


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.

The tmpfile(3C) function is preferred over the mkstemp() function for creating temporary files when control is not needed over the path, name, and flags used to open(2) the file.

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:

Interface Stability
See below.

See standards(7) for descriptions of the following standards:

  • mkstemp()
  • POSIX.1-2001 through 2008,
  • SUS through SUSv4,
  • XPG4v2 through XPG7
  • mkstemps()
  • mkostemp()
  • mkostemps()

See Also

getpid(2), open(2), mktemp(3C), tmpfile(3C), attributes(7), lf64(7), standards(7)


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.