Next: perror, Previous: getw, Up: Stdio

### 4.30 `mktemp`, `mkstemp`—generate unused file name

Synopsis

         #include <stdio.h>
         char *mktemp(char *path);
         int mkstemp(char *path);

         char *_mktemp_r(struct _reent *reent, char *path);
         int *_mkstemp_r(struct _reent *reent, char *path);

Description
`mktemp` and `mkstemp` attempt to generate a file name that is not yet in use for any existing file. `mkstemp` creates the file and opens it for reading and writing; `mktemp` simply generates the file name.

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 some number of ``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.

The alternate functions `_mktemp_r` and `_mkstemp_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`.

`mkstemp` returns 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.

Supporting OS subroutines required: `getpid`, `open`, `stat`.